获得查询结果的平均值

时间:2010-11-03 19:13:31

标签: sql sql-server-2008

我有这样的查询:

select distinct UserID, Count(UserId) as MyNumber from  dbo.User2User group by UserId

现在我想扩展此查询以获取MyNumber的MIN,MAX和AVG

不知道如何从另一个select或sth ...

获取此值

感谢您的帮助

1 个答案:

答案 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。