我有一个包含教程的表(tutorial_id,user_id,tutorial_date),我的任务是找出有多少用户完成了特定数量的教程。
所以,如果有5个用户做了6个教程,3个用户做了2个教程,我想要以下输出:
Tutorial_Count (unique) Number of Students With this Tutorial Count
6 5
2 3
我可以得到不同的计数(第一栏):
SELECT DISTINCT COUNT(user_id) from tutorials group by user_id
但我不知道如何计算适合这些计数括号的用户数量。
答案 0 :(得分:1)
未经测试,但试试这个:
;WITH cte AS
(
SELECT COUNT(user_id) AS Tutorial_Count
FROM tutorials
GROUP BY user_id
)
SELECT DISTINCT
Tutorial_Count,
COUNT(*) OVER (PARTITION BY Tutorial_Count) AS [Number of Students With this Tutorial Count]
FROM
cte;