SQL - Restrict the number of rows returned based on a count of rows

后端 未结 3 803
别跟我提以往
别跟我提以往 2021-01-13 06:04

Here is what my data looks like:

item_ID | group_ID | count_of_items_in_group

2|ABC|3

5|ABC|3

9|ABC|3

29|DEF|3

3|DEF|3

4|DEF|3

200|XYZ|2

300|XYZ|2

6         


        
3条回答
  •  借酒劲吻你
    2021-01-13 06:31

    What about using ROW_NUMBER() to enum the number of fields of your data set, partitioned by each group_id.

    And after that just return when is just less or equal to 2? or any number that you want

    SELECT * FROM 
    (
      select item_id, group_id, count_of_items, 
      ROW_NUMBER() OVER(PARTITION BY group_id ORDER BY count_of_items DESC) 
      AS RN
      from items_in_groups
    ) A
    WHERE RN <= 2
    

    Here is the Sql Fiddle

提交回复
热议问题