分组查询的分组结果

时间:2013-07-03 13:55:01

标签: sql sql-server ssms

我想知道特定用户在同一天进行活动的频率

SELECT COUNT(*) FROM Activity
GROUP BY entity_uid, date

这会返回很长的计数列表:即

1
1
2
1

然后,我想将此查询的结果分组,告诉我每个数字在结果集中出现的频率,例如

#    Count(*)
1    3
2    1

这是我拥有的,但它不起作用

Select x = (
            SELECT COUNT(*) FROM Activity
            GROUP BY entity_uid, date
           ),
       Count(*)
GROUP BY x

如何在变量中选择子查询的结果?

2 个答案:

答案 0 :(得分:2)

尝试以下

select daily_user_count, count(*) from (
SELECT COUNT(*) as daily_user_count FROM Activity
GROUP BY entity_uid, date
) Z
group by daily_user_count

答案 1 :(得分:1)

尝试

SELECT Frequency, COUNT(*) [Count]
FROM (
    SELECT COUNT(*) Frequency 
    FROM Activity
    GROUP BY entity_uid, date
) t
GROUP BY Frequency