Home Forums SQL Server 2012 SQL 2012 - General Is there a fast way for SQL to do a Group By Count on items within a group? RE: Is there a fast way for SQL to do a Group By Count on items within a group?

  • isuckatsql (5/1/2014)


    select top 15 count(*) as cnt, state from table

    group by state

    order by cnt desc

    select top 15 count(*) as cnt, city from table

    group by city

    order by cnt desc

    select top 15 count(*) as cnt, company from table

    group by company

    order by cnt desc

    Can the above three queries be combined into one and still be fast, if so how?

    What i am trying to go is an item count, by group, similar to ones Inbox in Outlook.

    Thanks

    I want to see your expected output. If New York City is the most populous city but California is the most populous state, how would a single output with the respective columns and counts portray that? NYC is in NY, not CA...

    Best,
    Kevin G. Boles
    SQL Server Consultant
    SQL MVP 2007-2012
    TheSQLGuru on googles mail service