tableb中的tablea中值数量?

发布于 2025-02-07 23:16:20 字数 1374 浏览 1 评论 0原文

我有两个具有以下模式的表格:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ペ泪落弦音 2025-02-14 23:16:20

如果我正确理解您的问题,那么应该有很多简单的方法。
怎么样:

select count(distinct accountNumber)
from tableA
where not exists(select 1 from tableB where tableA.accountNumber = tableB.accountNumber)

If I understand your question correctly, than there should be much easier ways.
How about:

select count(distinct accountNumber)
from tableA
where not exists(select 1 from tableB where tableA.accountNumber = tableB.accountNumber)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文