COUNT WHERE 在同一列中满足两个条件
COUNT WHERE two conditions are met in the same column
我确定以前有人问过这个问题,但我找不到类似的情况:
给定一个 table 'activity'
user_ID | contact_channel_ID
123456 email
123456 email
555555 SEM
555555 SEM
995959 phone
995959 SEM
696969 email
696969 email
我需要计算拥有 'SEM' 和至少另一个频道 contact_channel_ID
的所有用户
您可以使用聚合和 having
:
select user_id
from activity
group by user_id
having sum(case when contact_channel_ID = 'SEM' then 1 else 0 end) > 0 and
sum(case when contact_channel_ID <> 'SEM' then 1 else 0 end) > 0;
在MySQL中,having
子句可以缩写为:
having sum(contact_channel_ID = 'SEM') > 0 and
sum(contact_channel_ID <> 'SEM') > 0;
您可以使用聚合:
select user_id
from activity
group by user_id
having max(contact_channel_id = 'SEM') = 1
and max(contact_channel_id <> 'SEM') = 1
如果要统计这样的用户,再增加一层聚合:
select count(*) as cnt
from (
select 1
from activity
group by user_id
having max(contact_channel_id = 'SEM') = 1
and max(contact_channel_id <> 'SEM') = 1
) t
按用户分组并在HAVING子句中设置条件:
SELECT user_id
FROM activity
GROUP BY user_id
HAVING COUNT(DISTINCT contact_channel_id) > 1
AND SUM(contact_channel_id = 'SEM') > 0
或使用 EXISTS:
SELECT DISTINCT a.user_id
FROM activity a
WHERE a.contact_channel_id = 'SEM'
AND EXISTS (SELECT 1 FROM activity WHERE user_id = a.user_id AND contact_channel_id <> a.contact_channel_id)
如果你想计算用户数,请将上面的查询更改为:
SELECT COUNT(DISTINCT a.user_id) counter
FROM activity a
WHERE a.contact_channel_id = 'SEM'
AND EXISTS (SELECT 1 FROM activity WHERE user_id = a.user_id AND contact_channel_id <> a.contact_channel_id)
参见demo。
结果:
> | user_id |
> | ------: |
> | 995959 |
和:
> | counter |
> | ------: |
> | 1 |
我确定以前有人问过这个问题,但我找不到类似的情况:
给定一个 table 'activity'
user_ID | contact_channel_ID
123456 email
123456 email
555555 SEM
555555 SEM
995959 phone
995959 SEM
696969 email
696969 email
我需要计算拥有 'SEM' 和至少另一个频道 contact_channel_ID
您可以使用聚合和 having
:
select user_id
from activity
group by user_id
having sum(case when contact_channel_ID = 'SEM' then 1 else 0 end) > 0 and
sum(case when contact_channel_ID <> 'SEM' then 1 else 0 end) > 0;
在MySQL中,having
子句可以缩写为:
having sum(contact_channel_ID = 'SEM') > 0 and
sum(contact_channel_ID <> 'SEM') > 0;
您可以使用聚合:
select user_id
from activity
group by user_id
having max(contact_channel_id = 'SEM') = 1
and max(contact_channel_id <> 'SEM') = 1
如果要统计这样的用户,再增加一层聚合:
select count(*) as cnt
from (
select 1
from activity
group by user_id
having max(contact_channel_id = 'SEM') = 1
and max(contact_channel_id <> 'SEM') = 1
) t
按用户分组并在HAVING子句中设置条件:
SELECT user_id
FROM activity
GROUP BY user_id
HAVING COUNT(DISTINCT contact_channel_id) > 1
AND SUM(contact_channel_id = 'SEM') > 0
或使用 EXISTS:
SELECT DISTINCT a.user_id
FROM activity a
WHERE a.contact_channel_id = 'SEM'
AND EXISTS (SELECT 1 FROM activity WHERE user_id = a.user_id AND contact_channel_id <> a.contact_channel_id)
如果你想计算用户数,请将上面的查询更改为:
SELECT COUNT(DISTINCT a.user_id) counter
FROM activity a
WHERE a.contact_channel_id = 'SEM'
AND EXISTS (SELECT 1 FROM activity WHERE user_id = a.user_id AND contact_channel_id <> a.contact_channel_id)
参见demo。
结果:
> | user_id |
> | ------: |
> | 995959 |
和:
> | counter |
> | ------: |
> | 1 |