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.
Hi the following works in MSSQL2000
The inner
Select Count(*)query works by counting how many records are above the record it is currently looking at – in this case you want there to b either 0 (1st place) or 1 (2nd place).Note that because of this, if you have more than 2 values sharing the top spot (e.g 3 A/1’s all with sales of 15) you will get more than 2 results back.
However, for your test set it returns the correct results and the use of
DISTINCTwill help if you’d rather get less instead of more results in this instance. Additionally if your records have a distinct recordid this may help you to decide between them.