Author |
Topic |
jkbeau1
Starting Member
12 Posts |
Posted - 2015-04-20 : 08:50:29
|
The following contains the temporary table on the top that I want to work on and the bottom is the result I am looking for:Basically I'd like to use CTE to get the sort order numbering to be sequential, as you can see in the second table above. The CTGY_Rec_Num is always unique and a group of CTGY_Rec_Num numbers belongs to a specific DATB_Rec_Num. As you will note, the CTGY_Sort_Order is sorted but there are gaps and it can start back at 1 between DATB_Rec_Num groupings.I've messed with a bunch of different CTE attempts with no luck. I'm kind of new to CTE and don't want to have to resort to a loop or a cursor as I am sure this can be done with the recursive nature of CTE.Thanks! |
|
gbritton
Master Smack Fu Yak Hacker
2780 Posts |
Posted - 2015-04-20 : 09:06:46
|
Can't view your image. Please post your CTE query. |
|
|
Ifor
Aged Yak Warrior
700 Posts |
Posted - 2015-04-20 : 09:23:56
|
You should post test data in a consumable format.I suspect you want something like:SELECT * ,ROW_NUMBER() OVER (PARTITION BY DATB_Rec_Num ORDER BY CTGY_Sort_Order) AS NEW_Sort_NumFROM YourTable; If you want to update CTGY_Sort_Order:WITH NewOrderAS( SELECT * ,ROW_NUMBER() OVER (PARTITION BY DATB_Rec_Num ORDER BY CTGY_Sort_Order) AS NEW_Sort_Num FROM YourTable)UPDATE NewOrderSET CTGY_Sort_Order = NEW_Sort_NumWHERE CTGY_Sort_Order <> NEW_Sort_Num;-- see resultsselect * from YourTable; |
|
|
jkbeau1
Starting Member
12 Posts |
Posted - 2015-04-20 : 10:23:46
|
Fairly new to the forum so when you say "consumable format", what would be the best way to do that within this forum?The CTE provided doesn't quite work as expected as for simplicity sake I left out the fact that within each DATB_Rec_Num, CTGY_Rec_Num grouping, there are items...So for DATB_Rec_Num = 1, CTGY_Rec_Num = 1, there could be items 10000, 10001, 10002, 10003, 10004, 10005, etc. The CTE provided would generate a sort number for each item whereas if the new sort number is 1, it would be 1 for each item until the CTGY_Rec_Num changes.Probably shouldn't have left this detail out...sorry! |
|
|
gbritton
Master Smack Fu Yak Hacker
2780 Posts |
Posted - 2015-04-20 : 10:30:36
|
Follow this guide:http://www.sqlservercentral.com/articles/Best+Practices/61537/ |
|
|
jkbeau1
Starting Member
12 Posts |
Posted - 2015-04-20 : 11:03:24
|
OK, try this to create the test data...create table #Table(CTGY_Rec_Num int,CTGY_Sort_Order int,DATB_Rec_Num int,Original_Item_Number int,NEW_Sort_Order int);insert into #Table(CTGY_Rec_Num, CTGY_Sort_Order, DATB_Rec_Num, Original_Item_Number)select 1, 1, 1, 10000 union allselect 1, 1, 1, 10001 union allselect 2, 2, 1, 10002 union allselect 2, 2, 1, 10003 union allselect 3, 4, 1, 10004 union allselect 3, 4, 1, 10005 union allselect 4, 7, 1, 10006 union allselect 4, 7, 1, 10007 union allselect 5, 8, 1, 10008 union allselect 5, 8, 1, 10009 union allselect 6, 10, 1, 10010 union allselect 6, 10, 1, 10011 union allselect 7, 15, 1, 10012 union allselect 10, 1, 2, 20000 union allselect 10, 1, 2, 20001 union allselect 12, 2, 2, 20002 union allselect 12, 2, 2, 20003 union allselect 13, 3, 2, 20004 union allselect 13, 3, 2, 20005 union allselect 14, 4, 2, 20006 union allselect 14, 4, 2, 20007 union allselect 15, 5, 2, 20008 union allselect 15, 5, 2, 20009 union allselect 16, 7, 2, 20010 union allselect 16, 7, 2, 20011 union allselect 17, 10, 2, 20012;So the result of the correct CTE would populate the NEW_Sort_Order column like:1, 1, 1, 10000, 11, 1, 1, 10001, 12, 2, 1, 10002, 22, 2, 1, 10003, 23, 4, 1, 10004, 33, 4, 1, 10005, 34, 7, 1, 10006, 44, 7, 1, 10007, 45, 8, 1, 10008, 55, 8, 1, 10009, 56, 10, 1, 10010, 66, 10, 1, 10011, 67, 15, 1, 10012, 710, 1, 2, 20000, 110, 1, 2, 20001, 112, 2, 2, 20002, 212, 2, 2, 20003, 2 13, 3, 2, 20004, 313, 3, 2, 20005, 314, 4, 2, 20006, 414, 4, 2, 20007, 415, 5, 2, 20008, 515, 5, 2, 20009, 516, 7, 2, 20010, 616, 7, 2, 20011, 617, 10, 2, 20012, 7Hopefully that makes my question and expected results a little clearer? |
|
|
Ifor
Aged Yak Warrior
700 Posts |
Posted - 2015-04-20 : 11:16:13
|
[code],DENSE_RANK() OVER (PARTITION BY DATB_Rec_Num ORDER BY CTGY_Sort_Order) AS NEW_Sort_Num[/code] |
|
|
jkbeau1
Starting Member
12 Posts |
Posted - 2015-04-20 : 14:10:01
|
That will work...thanks! |
|
|
|
|
|