Peter
Peter

Reputation: 9113

Grouping query results by count

I am trying to get results from a table grouped and sorted by count. I've got the following table:

┌───────────┬──────────┐
│ [sub_key] │ [sub_id] │
├───────────┼──────────┤
│  subkey   │ abc      │
├───────────┼──────────┤
│  subkey   │ abd      │
├───────────┼──────────┤
│  subkey   │ abe      │
├───────────┼──────────┤
│  subkey   │ abc      │
├───────────┼──────────┤
│  subkey   │ abc      │
├───────────┼──────────┤
│  subkey   │ abd      │
└───────────┴──────────┘

What I'm trying to achieve:

subkey | abc | 3
subkey | abd | 2
subkey | abe | 1

Any suggestions on what my query should be?

Upvotes: 1

Views: 18

Answers (2)

Rubin Porwal
Rubin Porwal

Reputation: 3845

As a solution please try executing the following SQL query

select sub_key,sub_id,count(sub_key) as counter 
from TABLE_NAME group by sub_key,sub_id order by counter desc

Upvotes: 1

Mattia Caputo
Mattia Caputo

Reputation: 969

SELECT [sub_key], [sub_id], count(*)
FROM Table
GROUP BY [sub_key], [sub_id]
ORDER BY count(*) DESC

Upvotes: 3

Related Questions