How to get the average of a count field in a SQL query

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Chris Rich

    How to get the average of a count field in a SQL query

    I have this query which gives a count of how many times a specific client_name shows up in a table and creates of count of that and calls it "Hits"....

    SELECT client_name, COUNT(1) AS Hits FROM All_Data GROUP BY client_name

    I would like to take this one step further and just return the average "Hits" for this one query as one result. Can I do this all on one line? If so, could someone help me do this? Thanks, Chris
  • ck9663
    Recognized Expert Specialist
    • Jun 2007
    • 2878

    #2
    There's an AVG() function in SQL Server.

    Good Luck!!!

    ~~ CK

    Comment

    • Chris Rich

      #3
      I think I figured it out:

      select sum(hits)/count(*) as AverageHits
      from (SELECT client_name, COUNT(1) AS Hits FROM All_Data GROUP BY client_name) AS Hitquery

      Comment

      Working...