我有这样的查询:
select distinct UserID, Count(UserId) as MyNumber from dbo.User2User group by UserId
现在我想扩展此查询以获取MyNumber的MIN,MAX和AVG
不知道如何从另一个select或sth ...
获取此值感谢您的帮助
答案 0 :(得分:5)
您可以将自己的查询用作子查询:
SELECT MIN(MyNumber) AS minNumber, MAX(MyNumber) AS maxNumber, AVG(myNumber) AS avgNumber
FROM (SELECT UserID, COUNT(UserID) AS MyNumber
FROM dbo.User2User
GROUP BY UserID) TMP;
请注意,DISTINCT关键字不是必需的:GROUP BY已经选择了不同的UserID。