我有一个这样的表:
PalmId | UserId | CreatedDate
1 | 1 | 2018-03-08 14:18:27.077
1 | 2 | 2018-03-08 14:18:27.077
1 | 3 | 2018-03-08 14:18:27.077
1 | 1 | 2018-03-08 14:18:27.077
我想知道为Palm 1创建了多少个日期,也想知道有多少用户为Palm 1创建了那些日期。因此,第一个结果为4
,第二个结果为{{ 1}}
我想知道是否可以在单个查询中做到这一点,而不必像下面的示例那样对子查询和自身进行联接。
3
答案 0 :(得分:2)
根据第一个表,您可以执行以下操作:
select count(distinct uerid) as N_Users,
count(created_date) as created_date, -- if you use count(*) you consider also rows with 'NULL'
palmid
from your_table
group by palmid
答案 1 :(得分:0)
如果您想要“ 4”和“ 3”,那么我认为您想要:
SELECT MT.PalmId, COUNT(*) AS NumRows, COUNT(DISTINCT mt.UserId) as NumUsers
FROM MyTable MT
GROUP BY MT.PalmId