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)
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With