SELECT TOP N With Two Variables

Posted by Ricardo Deano on Stack Overflow See other posts from Stack Overflow or by Ricardo Deano
Published on 2010-04-13T08:22:58Z Indexed on 2010/04/13 9:13 UTC
Read the original article Hit count: 362

Filed under:
|
|

Hello all.

It's Tuesday morning and I am being thick as (I'm blaming my daughter waking up early this morning!)

I have the following example in a SQL table

Cust Group Sales
A   1   15
A   1   10
A   1   5
A   2   15      
A   2   10
A   2   5
B   1   15
B   1   10
B   1   5
B   2   15
B   2   10  
B   2   5

What I would like to show is the top 2 products per customer, per group sorted descending by Sales i.e.

Cust Group Sales
A   1   15
A   1   10
A   2   15      
A   2   10
B   1   15
B   1   10
B   2   15
B   2   10

I'm assuming I need to declare two variables, Cust and Group, I'm just not sure how to complete this in one fell swoop.

Apologies for the thick question...no excuse. Thanks for any help.

© Stack Overflow or respective owner

Related posts about tsql

Related posts about sql