Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

600|GHI|1

SQL Filddle: http://sqlfiddle.com/#!2/dfe09/1

For each group, I want to limit the number of item_IDs returned to a max of 2. I do not care which 2 are returned. If the group has less than 2 rows return only the 1 row.

I can't write a select top * 2 for each group and union the selects because I have a few hundred groups.

I'm not sure where to start, your help is greatly appreciated.

Using MS SQL 2005

(Table layout is exactly as it is in the sample, it is a view based on a number of queries)

like image 540
AfterWorkGuinness Avatar asked Sep 17 '13 17:09

AfterWorkGuinness


People also ask

How do I limit the number of rows returned in SQL?

If you don't need to omit any rows, you can use SQL Server's TOP clause to limit the rows returned. It is placed immediately after SELECT. The TOP keyword is followed by integer indicating the number of rows to return. In our example, we ordered by price and then limited the returned rows to 3.

What SQL clause is used to restrict the rows returned by a query?

LIMIT clause in the SQL is used to restrict the number of records. In simple words, it is used to set an upper limit on the number of tuples returned for any given query.

What does limit 1 1 do in SQL?

SELECT column_list FROM table_name ORDER BY expression LIMIT n-1, 1; In this syntax, the LIMIT n-1, 1 clause returns 1 row that starts at the row n. For example, the following query returns the employee information who has the second-highest income: SELECT emp_name, city, income FROM employees.


1 Answers

Use the ROW_NUMBER() function for this:

SELECT *
FROM (select *,ROW_NUMBER() OVER (PARTITION BY group_id ORDER BY item_id) as RowRank 
      from items_in_groups
      )sub
WHERE RowRank <=2

Demo: SQL Fiddle

The ROW_NUMBER() function assigns a number to each row. PARTITION BY is optional, but used to start the numbering over for each value in that group, ie: if you PARTITION BY group_id then for each unique group_id value the numbering would start over at 1. ORDER BY of course is used to define how the counting should go, and is required in the ROW_NUMBER() function.

like image 87
Hart CO Avatar answered Nov 15 '22 07:11

Hart CO