gpt4 book ai didi

mysql - SQL : 2 distinct counts with different where clauses on same field

转载 作者:行者123 更新时间:2023-11-30 21:22:46 24 4
gpt4 key购买 nike

如何将下面的 2 个查询合并为一个查询?

select count(distinct user_id) as action_1_count, date  
from table
where action = "action_1"
group by date;

select count(distinct user_id) as action_2_count, date
from table
where action = "action_2"
group by date;

最佳答案

像这样合并上面的查询:

SELECT date,
CASE WHEN action = "action_1" THEN COUNT(user_id) END AS Action1Count,
CASE WHEN action = "action_2" THEN COUNT(user_id) END AS Action2Count
FROM table
WHERE action IN ("action_1", "action_2")
GROUP BY date

关于mysql - SQL : 2 distinct counts with different where clauses on same field,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11974491/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com