gpt4 book ai didi

sql - COUNT DISTINCT WITH CONDITION 和 GROUP BY

转载 作者:行者123 更新时间:2023-12-05 01:35:31 25 4
gpt4 key购买 nike

我想根据特定条件计算列中不同项目的数量。例如,如果表格是这样的:

ID | name   |    date    | status
---+--------+------------+--------
1 | Andrew | 2020-04-12 | true
2 | John | 2020-03-22 | null
3 | Mary | 2020-04-13 | null
4 | John | 2020-05-27 | false
5 | Mary | 2020-02-08 | true
6 | Andrew | 2020-02-08 | null

如果我想将最后日期的状态不为空的不同名称的数量统计为“名称计数”,并按状态对它们进行分组,我应该怎么做?

结果应该是:

status | name_count
-------+-----------
true | 1 ---> Only counts Andrew (ID 1 has the last date)
false | 1 ---> Only counts John (ID 4 has the last date)

最佳答案

你可以试试下面的查询

SELECT COUNT(DISTINCT Name), Status 
FROM Table
WHERE Status IS NOT NULL
GROUP BY Status;

关于sql - COUNT DISTINCT WITH CONDITION 和 GROUP BY,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62827949/

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