2

当前 SQL 语句:

SELECT count(installUserID) as userCount
  FROM installUsers
  group by installationID

电流输出:

userCount
4
2
1
1
1
2
4
1
3
1

我正在尝试计算userCount1 和 2 等的次数...

所需输出的示例:

userCount = 1, 5 times
userCount = 2, 2 times
userCount = 3, 1 times
userCount = 4, 2 times

我环顾了互联网,我想我可以用聚合函数做一些事情,也许用grouping,但我仍然不清楚如何用纯 sql 来实现它。

4

2 回答 2

3

看来您想要计数。为此,您可以使用外部选择:

SELECT userCount, COUNT(*) AS userCountcount
FROM
(
    SELECT COUNT(installUserID) as userCount
    FROM installUsers
    GROUP BY installationID
) AS T1
GROUP BY userCount
ORDER BY userCount
于 2012-08-03T18:00:54.533 回答
3

或者,您可以使用 CTE。也许为此而过度杀戮,但非常方便。

WITH count_cte(usercount)
AS
(
  SELECT COUNT(installUserID) AS userCount
  FROM installUsers
  GROUP BY installationID
)

SELECT  usercount  ,COUNT(usercount) AS times  FROM count_cte GROUP BY usercount
于 2012-08-03T18:10:54.010 回答