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 |
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.
(06 Dec '11, 16:00)
TimC
Replies hidden
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".
(06 Dec '11, 16:13)
Volker Barth
1
That's what the WHERE clause is for, in your case WHERE ranked.entry_rank <= 2.
(06 Dec '11, 16:14)
Breck Carter
ROW_NUMBER() resolved it. Top answer by Breck and Volker. Many thanks.
(06 Dec '11, 17:18)
TimC
|