get average of query result
Ive got query like this:
select distinct UserID, Count(UserId) as MyNumber from dbo.User2User gro开发者_如何学Cup by UserId
now I would like extend this query to get MIN, MAX and AVG of MyNumber
no idea how to get to this value from another select or sth ...
thanks for help
You can use your own query as a subquery :
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;
Note that the DISTINCT keyword isn't necessary : GROUP BY already selects distinct UserID.
精彩评论