tableb中的tablea中值数量?
我有两个具有以下模式的表格:
CREATE TABLE tableA
(
accountNumber, --has colons in between values... need to be removed on join
type, --(1,2,3,4,5,6,7,8,9,10)
status1,
status2
);
CREATE TABLE tableB
(
accountNumber,
usage, -- [0,inf)
day_id
);
我想知道TableB中没有多少个帐户名称。我已经构建了两种方法: 方式#1
with cteA as (
Select upper(replace(accountNumber, ':',''))) as accountNumber,
type
from tableA
where status1 = 'Active'
and status2 = 'Active'
and type in ('1','2','3','4')
),
cteB as (
Select distinct accountNumber
from tableB
where usage > 0
and day_id > '2022-01-01'
),
cte as (
Select a.accountNumber,
b.accountNumber,
a.type
from tableA a
left join tableB b
on a.accountNumber = b.accountNumber
where b.accountNumber is null)
select type, count(*)
from cte
group by 1
order by 2 desc;
#2
select type,
count(distinct accountNumber),
count(distinct accountNumber) filter (where upper(replace(accountNumber,':','')) in (select accountNumber from tableB where day_id > '2022-01-01' and usage > 0))
from tableA
where status1 = 'Active'
and status2 = 'Active'
and type in ('1','2','3','4')
group by 1
就我所知,这些应该给我相同的答案。当然,方法2要求我按下减法(在Excel或我的脑海中手动)...但是我不明白为什么这些查询会给我带来不同的价值吗?我想念什么吗?
I have two tables with the following schema:
CREATE TABLE tableA
(
accountNumber, --has colons in between values... need to be removed on join
type, --(1,2,3,4,5,6,7,8,9,10)
status1,
status2
);
CREATE TABLE tableB
(
accountNumber,
usage, -- [0,inf)
day_id
);
I would like to know how many accountNumbers are in tableA that are NOT in tableB. I have constructed two ways to do this:
Way #1
with cteA as (
Select upper(replace(accountNumber, ':',''))) as accountNumber,
type
from tableA
where status1 = 'Active'
and status2 = 'Active'
and type in ('1','2','3','4')
),
cteB as (
Select distinct accountNumber
from tableB
where usage > 0
and day_id > '2022-01-01'
),
cte as (
Select a.accountNumber,
b.accountNumber,
a.type
from tableA a
left join tableB b
on a.accountNumber = b.accountNumber
where b.accountNumber is null)
select type, count(*)
from cte
group by 1
order by 2 desc;
Way #2
select type,
count(distinct accountNumber),
count(distinct accountNumber) filter (where upper(replace(accountNumber,':','')) in (select accountNumber from tableB where day_id > '2022-01-01' and usage > 0))
from tableA
where status1 = 'Active'
and status2 = 'Active'
and type in ('1','2','3','4')
group by 1
As far as I can tell, these should give me the same answer. Granted, way 2 requires me doing a subtraction (manually in excel or in my head)... but I don't see why these queries would give me a different value? Am I missing something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我正确理解您的问题,那么应该有很多简单的方法。
怎么样:
If I understand your question correctly, than there should be much easier ways.
How about: