cancel
Showing results for 
Search instead for 
Did you mean: 

Top n sales for each person

Former Member
2,395

I'd like a query that returns the first two sales for EACH salesperson ordered by date from a main sales table. Each person may have made many sales but I just want the first n when ordered by date.

E.g.

salesperson_id, salesperson_name, sale_id, saledate
1,Tim,1,2011-01-01
1,Tim,2,2011-01-02
3,Fred,1,2011-05-01
3,Fred,2,2011-05-01
4,Julie,1,2011-05-10
4,Julie,2,2011-05-10

Accepted Solutions (1)

Accepted Solutions (1)

Breck_Carter
Participant
Former Member
0 Kudos

Thanks Breck, I've used Rank in other reports but find it can return more records than required when multiple results have the same value. In this instance, I require 2 for each sales person. No more.

VolkerBarth
Contributor
0 Kudos

IMHO, given Breck's sample, that would mean that a single sales person has more than 2 sales on the same date - how are you going to treat that?

If this doesn't occur in your data, then I wouldn't expect problems with RANK() - as it should be used with a partition on each sales person...

BTW, ROWNUMBER() should resolve "ties".

Breck_Carter
Participant

That's what the WHERE clause is for, in your case WHERE ranked.entry_rank <= 2.

Former Member
0 Kudos

ROW_NUMBER() resolved it.

Top answer by Breck and Volker. Many thanks.

Answers (0)