Monday, January 27, 2014

Getting the top row for a group of records (T-SQL)

From time to time I find my self in need for writing queries that show top record for any group of records. Here is a simple method for grouping and displaying the desired output.

Let's say that our table consist of the following recordset:

Name     Price     Product
--------  -------  ----------
Joe         5           Car
Mike      8           Shoes
John       12         Theatre ticket
Joe         15         Theatre ticket
Joe         32         Shoes
Mike      7           Boat

Now, let's see how we can get every guy's most expensive purchase.

;with cte as (
   select row_number() over (partition by Name order by Price desc) as Row, Name, Price, Product
   from purchases
)
select Name, Price, Product from cte where Row = 1;

This will give us the desired response, i.e. a recordset that consist only the most expensive purchases for each guy

Name     Price     Product
--------  -------  ----------
Mike      8           Shoes
John       12         Theatre ticket
Joe         32         Shoes

And now for a short explanation:
The row_number() adds a row number for each row (surprise surprise...). The real deal here is the partitioning of the content. Here we partition the content (or group in other words) by the Name and order it by the price descending. So in fact what we get is the following recordset:

Row    Name     Price     Product
------  --------  -------  ----------
1         Joe         32         Shoes
2         Joe         15         Theatre ticket
3         Joe         5           Car
1         Mike      8           Shoes
2         Mike      7           Boat
1         John       12         Theatre ticket

So what we need now is to select only the first row for each group and that's that.

No comments:

Post a Comment