Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
ts_giri
Starting Member
3 Posts |
Posted - 2010-03-25 : 07:59:51
|
| I have a table #demo like below Create table #demo(slno int, linecount int, pageno int, colno int, startrow int)Insert into #demo(Slno, linecount)Select 1,9 union allSelect 2,12 union allSelect 3,12 union allSelect 4,14 union allSelect 5,12 union allSelect 6,10 union allSelect 7,10 union allSelect 8,11 union allSelect 9,9 union allSelect 10,10 union allSelect 11,7 union allSelect 12,11 union allSelect 13,11 union allSelect 14,12 union allSelect 15,9 union allSelect 16,11 union allSelect 17,11 union allSelect 18,11 union allSelect 19,10 union allSelect 20,10 union allSelect 21,10 union allSelect 22,11 union allSelect 23,11 union allSelect 24,14 union allSelect 25,9 Without using cursor or while loop I am trying to populate the pageno, colno and startrow fieldsRules are that the First record will have the pageno as 1, colno as 1 and startrow as 1The colno of each record will move between 1 , 2 , 3 and 4 The startrow will be the sum of startrow + linecount of the previous row with the same colno and same pagenoThe colno of the current record (identified by the slno column) will be one more than the colno of previous record (subject to a maximum of 4)as long as the sum of the startrow + linecount of the current record is not more 50. If it is more than 50 then the colno will move one more (again subject to the maximum of 4 and startrow + linecount is not more than 50) and so on......If all colno have reached the 50 limit than the pageno will increment by 1 and the colno and the startrow will get reset to 1Please let me know if any of you have any solution to this problem. |
|
|
haroon2k9
Constraint Violating Yak Guru
328 Posts |
Posted - 2010-03-25 : 08:03:26
|
| could you please show us the output expected? |
 |
|
|
ts_giri
Starting Member
3 Posts |
Posted - 2010-03-25 : 08:11:27
|
| The output will be like below Slno Linecount PageNo Colno Startrow1 9 1 1 12 12 1 2 13 12 1 3 14 14 1 4 15 12 1 1 106 10 1 2 137 10 1 3 138 11 1 4 159 9 1 1 2210 10 1 2 2311 7 1 3 2312 11 1 4 2613 11 1 1 3114 12 1 2 3315 9 1 3 3016 11 1 4 3717 11 2 1 118 11 2 2 119 10 2 3 120 10 2 4 121 10 2 1 1222 11 2 2 1223 11 2 3 1124 14 2 4 1125 9 2 1 22 |
 |
|
|
ts_giri
Starting Member
3 Posts |
Posted - 2010-03-25 : 08:18:02
|
| Small correction to the expected output -Slno Linecount PageNo Colno Startrow1 9 1 1 12 12 1 2 13 12 1 3 14 14 1 4 15 12 1 1 106 10 1 2 137 10 1 3 138 11 1 4 159 9 1 1 2210 10 1 2 2311 7 1 3 2312 11 1 4 2613 11 1 1 3114 12 1 2 3315 9 1 3 3016 11 1 4 3717 11 1 3 3918 11 2 1 119 10 2 2 120 10 2 3 121 10 2 4 122 11 2 1 1223 11 2 2 1124 14 2 3 1125 9 2 4 11 |
 |
|
|
DBA in the making
Aged Yak Warrior
638 Posts |
Posted - 2010-03-25 : 08:41:52
|
| I'm not sure you'll be able to do this without a cursor type statement, because the values you put in later rows depend on the values you put in earlier rows. eg, the values updated in row 5 depend on the values updated in row 1. I believe this means that you have to issues separate update statements, because using a single update statement, there's no guarantee which row would be updated first.There are 10 types of people in the world, those that understand binary, and those that don't. |
 |
|
|
|
|
|
|
|