Can someone advise or show me a working example of how you select the 1st, 2nd, 3rd, etc items from a product, then the second, third or fourth item from product B, etc.
example, the first item in A, the second item in A, the third item in A, and then B, and C etc. These items are not 1, 2, 3, etc they are only there for representation purpose but the first, second, third, record of product A, first, second, third record of product B, etc. Hope you know what I mean. How do we do that, if is even possible ?
drop table #items
go
create table #items
(
list int,
product varchar(1),
Item int
)
go
insert into #items select 1,'A',1
insert into #items select 2,'A',2
insert into #items select 3,'A',3
insert into #items select 4,'B',1
insert into #items select 5,'B',1
insert into #items select 6,'C',2
insert into #items select 7,'C',1
insert into #items select 8,'C',2
insert into #items select 9,'C',3
go
select * from #items
go
SQL
SELECT a.*
FROM (SELECT Row_number()
OVER(
partition BY product
ORDER BY list) AS rn,
*
FROM #items) a
ORDER BY rn,
product