gruber
gruber

Reputation: 29789

get average of query result

Ive got query like this:

select distinct UserID, Count(UserId) as MyNumber from  dbo.User2User group 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

Upvotes: 3

Views: 5655

Answers (1)

Vincent Savard
Vincent Savard

Reputation: 35947

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.

Upvotes: 6

Related Questions