根据您的要求,以下是翻译好的内容: 计算具有两个条件的唯一值数量。

huangapple go评论131阅读模式
英文:

Count distinct with two conditions

问题

我想要计算在"Code"列中具有值'A'和'B'的不同ID的数量。

ID Code
1 A
1 B
1 C
1 D
2 A
2 B
2 C

数据在fiddle中。

我尝试过以下两种方法,但都返回0:

SELECT COUNT(DISTINCT ID)
FROM tab
WHERE CODE = 'A' AND CODE = 'B';

以及

SELECT COUNT(DISTINCT ID) 
FROM tab AS t1 
WHERE EXISTS (
        SELECT 1
        FROM tab 
        WHERE ID = t1.ID AND CODE = 'A' AND CODE = 'B'
        );

为什么会这样?感谢回答。

英文:

I want to count how many distinct ids have values of 'A' AND 'B' in column Code.

ID Code
1 A
1 B
1 C
1 D
2 A
2 B
2 C

Data as fiddle

I tried using

SELECT COUNT(DISTINCT ID)
FROM tab
WHERE CODE = 'A' AND CODE = 'B';

and

SELECT COUNT(DISTINCT ID) 
FROM tab AS t1 
WHERE EXISTS (
        SELECT 1
        FROM tab 
        WHERE ID = t1.ID AND CODE = 'A' AND CODE = 'B'
        );

Both methods count 0

Why is that?

Thank you

答案1

得分: 1

如果您想统计不同代码的数量:

SELECT COUNT(DISTINCT ID)
FROM tab
WHERE CODE IN ('A', 'B')
GROUP BY CODE;

如果您想统计与字母 AB 中的任何一个配对的不同 ID 的常见计数,请使用以下查询:

SELECT COUNT(DISTINCT ID)
FROM tab
WHERE CODE IN ('A', 'B');
英文:

If you want count for different codes:

SELECT COUNT(DISTINCT ID)
FROM tab
WHERE CODE IN ('A', 'B')
GROUP BY CODE ;

If you want common count of distinct ID paired with any of letters A or B use this:

SELECT COUNT(DISTINCT ID)
FROM tab
WHERE CODE IN ('A', 'B');

答案2

得分: 0

I want to count how many distinct ids have values 'A' and 'B' in column code.

我想要统计在列code中值为'A''B'的不同id的数量。

I would recommend group by and having; this gives you the list of ids that have both codes (assuming no duplicate id/code).

我建议使用group byhaving;这将为您提供同时具有这两个代码的id列表(假设没有重复的idcode)。

select id
from tab
where code in ('A', 'B') -- rows that have any of the two codes
group by id
having count(*) = 2 -- both codes are present in the group

从表中选择id,其中code'A''B'中的任何一个 - 具有这两个代码的行
id分组
具有count(*) = 2having - 组中同时存在两个代码

Now we can just count how many rows the above query returns to get the output you wanted:

现在,我们只需计算上面查询返回的行数,以获得您想要的输出:

select count() as cnt
from (
select id
from tab
where code in ('A', 'B')
group by id
having count(
) = 2
) t

从上面的子查询中计算返回的行数,以获得所需的输出:

I would expect this approach to perform better than the solution using intersect, because it scans the table only once.

我期望这种方法的性能会比使用intersect的解决方案更好,因为它只扫描表一次。

Note that it is also possible to use exists, as you intended in your second attempt (although that's one more table scan); we would just need to fix the filtering:

请注意,还可以使用exists,正如您在第二次尝试中打算的那样(尽管这需要再次扫描表);我们只需要修复过滤条件:

select count(*)
from tab as t1
where t1.code = 'A' and exists (
select 1
from tab t2
where t2.id = t1.id and t2.code = 'B'
)

t1表中选择code'A'的行,并确保具有另一行,其中code'B'

英文:

> I want to count how many distinct ids have values 'A' and 'B' in column code.

I would recommend group by and having; this gives you the list of ids that have both codes (assuming no duplicate id/code).

select id
from tab
where code in ('A', 'B')  -- rows that have any of the two codes
group by id
having count(*) = 2       -- both codes are present in the group

Now we can just count how many rows the above query returns to get the output you wanted:

select count(*) as cnt
from (
    select id
    from tab
    where code in ('A', 'B')
    group by id
    having count(*) = 2
) t

I would expect this approachto perform better than the solution using intersect, because it scans the table only once.


Note that it is also possible to use exists, as you intended in yuour second attempt (although that's one more table scan); we would just need to fix the filtering:

select count(*) 
from tab as t1 
where t1.code = 'A' and exists (
    select 1
    from tab t2
    where t2.id = t1.id and t2.code = 'B'
)

This selects rows of code A and ensure that the given id has another row with code B.

huangapple
  • 本文由 发表于 2023年3月31日 20:57:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/75898811.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定