【发布时间】:2020-10-30 19:42:28
【问题描述】:
我想根据特定条件计算列中不同项目的数量。例如如果表是这样的:
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)
【问题讨论】:
-
你的 dbms 是什么
-
我正在使用 PostgreSQL
标签: sql if-statement group-by count distinct