【发布时间】:2019-03-09 04:33:34
【问题描述】:
我有一个如下所示的表格:
app_id supplier_reached creation_date platform
10001 1 9/11/2018 iOS
10001 2 9/18/2018 iOS
10002 1 5/16/2018 android
10003 1 5/6/2018 android
10004 1 10/1/2018 android
10004 1 2/3/2018 android
10004 2 2/2/2018 web
10005 4 1/5/2018 web
10005 2 5/1/2018 android
10006 3 10/1/2018 iOS
10005 4 1/1/2018 iOS
目标是找出每个月提交的 app_id 的唯一数量。
如果我只是做一个count(distinct app_id),我会得到以下结果:
Group by month count(app number)
Jan 1
Feb 1
may 3
september 1
october 2
但是,基于其他字段的组合,应用程序也被认为是唯一的。例如,对于 1 月份,the app_id 相同,但 app_id、supplier_reached 和 platform 的组合显示不同的值,因此 app_id 应计算两次。
按照相同的模式,期望的结果应该是:
Group by month Desired answer
Jan 2
Feb 2
may 3
september 2
october 2
最后,表中可能有许多其他列可能会或可能不会影响应用程序的唯一性。
有没有办法在 SQL 中进行这种类型的计数?
我正在使用 Redshift。
【问题讨论】:
标签: sql postgresql count amazon-redshift