SQL Order By Count

前端 未结 8 2163
北海茫月
北海茫月 2020-12-01 02:38

If I have a table and data like this:

ID |  Name  |  Group   

1    Apple     A    

2    Boy       A

3    Cat       B

4    Dog       C

5    Elep      C

         


        
相关标签:
8条回答
  • 2020-12-01 03:17

    Q. List the name of each show, and the number of different times it has been held. List the show which has been held most often first.

    event_id show_id event_name judge_id
    0101    01  Dressage        01
    0102    01  Jumping         02
    0103    01  Led in          01
    0201    02  Led in          02
    0301    03  Led in          01
    0401    04  Dressage        04
    0501    05  Dressage        01
    0502    05  Flag and Pole   02
    

    Ans:

    select event_name, count(show_id) as held_times from event 
    group by event_name 
    order by count(show_id) desc
    
    0 讨论(0)
  • 2020-12-01 03:23

    You need to aggregate the data first, this can be done using the GROUP BY clause:

    SELECT Group, COUNT(*)
    FROM table
    GROUP BY Group
    ORDER BY COUNT(*) DESC
    

    The DESC keyword allows you to show the highest count first, ORDER BY by default orders in ascending order which would show the lowest count first.

    0 讨论(0)
提交回复
热议问题