larrydavid
Programmer
Basically, the solution for this is to order the cat (catalog) sequence sequentially with the next group being renumbered 1, 2, 3, etc. But as you can see here, if the previous value stops at say 12 and the next value only has 1, the following group picks up with 13, 14, 15 and so on.
198879 HISPANIC BEVERAGE 1
198880 HISPANIC BEVERAGE 2
198881 HISPANIC BEVERAGE 3
198882 HISPANIC BEVERAGE 4
198883 HISPANIC BEVERAGE 5
198884 HISPANIC BEVERAGE 6
198885 HISPANIC BEVERAGE 7
198886 HISPANIC BEVERAGE 8
198887 HISPANIC BEVERAGE 9
198888 HISPANIC BEVERAGE 10
198889 HISPANIC BEVERAGE 11
198890 HISPANIC BEVERAGE 12
198891 HISPANIC SAUCES & SALSA 1
198892 HISPANIC BEVERAGE 13
198893 HISPANIC BEVERAGE 14
198894 HISPANIC BEVERAGE 15
198895 HISPANIC BEVERAGE 16
198896 HISPANIC BEVERAGE 17
My solution at first was to do this which gave the output above where it skips renumbering and picks up from the previous group:
SELECT * FROM
(
select excel_item_id, level1, rank() OVER
(
PARTITION BY Level1
order by excel_item_id
) as cat
from [ImportExcel] where product_offering_id = 31372
) Level
order by excel_item_id
But then this seemed to do the trick with adding a rownum column:
with cte as
(select excel_item_id,level1,rownum,'test' change
from
[ImportExcel]
where
rownum = 1
union all
select
r.excel_item_id,r.level1,r.rownum
,case when r.level1 <> r1.level1 then 'test'
else '' end change
from
[ImportExcel] r
join cte r1 on r.rownum - 1 = r1.rownum)
select * from cte
option (maxrecursion 10000);
But for performance reasons due to querying huge recordsets, I would like to make the rank() OVER/PARTITION functions work for this. So if anyone could suggest this or even another solution I would greatly appreciate it.
Thanks,
Larry
198879 HISPANIC BEVERAGE 1
198880 HISPANIC BEVERAGE 2
198881 HISPANIC BEVERAGE 3
198882 HISPANIC BEVERAGE 4
198883 HISPANIC BEVERAGE 5
198884 HISPANIC BEVERAGE 6
198885 HISPANIC BEVERAGE 7
198886 HISPANIC BEVERAGE 8
198887 HISPANIC BEVERAGE 9
198888 HISPANIC BEVERAGE 10
198889 HISPANIC BEVERAGE 11
198890 HISPANIC BEVERAGE 12
198891 HISPANIC SAUCES & SALSA 1
198892 HISPANIC BEVERAGE 13
198893 HISPANIC BEVERAGE 14
198894 HISPANIC BEVERAGE 15
198895 HISPANIC BEVERAGE 16
198896 HISPANIC BEVERAGE 17
My solution at first was to do this which gave the output above where it skips renumbering and picks up from the previous group:
SELECT * FROM
(
select excel_item_id, level1, rank() OVER
(
PARTITION BY Level1
order by excel_item_id
) as cat
from [ImportExcel] where product_offering_id = 31372
) Level
order by excel_item_id
But then this seemed to do the trick with adding a rownum column:
with cte as
(select excel_item_id,level1,rownum,'test' change
from
[ImportExcel]
where
rownum = 1
union all
select
r.excel_item_id,r.level1,r.rownum
,case when r.level1 <> r1.level1 then 'test'
else '' end change
from
[ImportExcel] r
join cte r1 on r.rownum - 1 = r1.rownum)
select * from cte
option (maxrecursion 10000);
But for performance reasons due to querying huge recordsets, I would like to make the rank() OVER/PARTITION functions work for this. So if anyone could suggest this or even another solution I would greatly appreciate it.
Thanks,
Larry