| Author |
Topic |
|
SamC
White Water Yakist
3467 Posts |
Posted - 2005-12-27 : 22:02:56
|
| I've solved this with a WHILE loop.I was wondering if there was an elegant non-WHILE solution.A column contains integers, all positive, non-zero.Resequence the integers so the minimimum is 1, the next minimum is 2 and so on.Just so you have some names to work with...CREATE TABLE MyTable (MyInt INT NOT NULL)Source data might be... 5, 9, 15, 20Result data should be... 1, 2, 3, 4 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2005-12-27 : 22:26:14
|
I don't know how you define "elegant" but it is non-WHILE at least...use pubsset nocount ongoCREATE TABLE MyTable (MyInt INT NOT NULL)goinsert myTable(myint)select 5 unionselect 9 unionselect 15 unionselect 20update c set c.myInt = b.seqfrom myTable cjoin ( select myint, seq = (select count(*) from myTable where myint <= a.myint) from myTable a ) b on b.myint = c.myintselect * from myTablegodrop table myTable or how about this:declare @rc intselect @rc = count(*) from myTabledrop table myTableset rowcount @rcselect number myint into myTable from numbers where number > 0 order by numberset rowcount 0 Be One with the OptimizerTG |
 |
|
|
SamC
White Water Yakist
3467 Posts |
Posted - 2005-12-27 : 23:29:49
|
TG, I like the 1st solution. The 2nd makes my head hurt  Thanks! |
 |
|
|
SamC
White Water Yakist
3467 Posts |
Posted - 2005-12-27 : 23:59:43
|
Soooooo much better than a WHILE loop...UPDATE A SET Qnum = B.NewModuleSequence FROM #Answers A INNER JOIN ( SELECT AA.ModuleSequence, COUNT(*) AS NewModuleSequence FROM ( SELECT DISTINCT ModuleSequence FROM #Answers ) AA LEFT OUTER JOIN ( SELECT DISTINCT ModuleSequence FROM #Answers ) BB ON BB.ModuleSequence <= AA.ModuleSequence GROUP BY AA.ModuleSequence ) B ON B.ModuleSequence = A.ModuleSequence Thanks again ! |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-12-28 : 01:16:04
|
| Do you really want to update or display the sequence number?How about having a view that does resequencing without updating the orginal values?Create View yourView asSelect (Select count(*) from mytable where myint<=T.myint) as Sno,myint from mytable TGoSelect Sno as myint from yourViewMadhivananFailing to plan is Planning to fail |
 |
|
|
SamC
White Water Yakist
3467 Posts |
Posted - 2005-12-28 : 08:21:28
|
quote: Originally posted by madhivanan Do you really want to update or display the sequence number?How about having a view that does resequencing without updating the orginal values?Create View yourView asSelect (Select count(*) from mytable where myint<=T.myint) as Sno,myint from mytable T
I omitted the point that many numbers will be duplicates. Taking the duplicates into account, your VIEW proposal would be as complicated as the INNER JOIN solution once it's revised.The source data with duplicates might be2, 5, 5, 8, 9The result should be1, 2, 2, 3, 4 |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-12-28 : 08:28:03
|
Then use Distinct countCREATE TABLE MyTable (MyInt INT NOT NULL)goinsert myTable(myint)select 2 union allselect 5 union all select 5 union allselect 8 union allselect 9Select (Select count(distinct myint) from mytable where myint<=T.myint) as Sno from mytable Tdrop table MyTable MadhivananFailing to plan is Planning to fail |
 |
|
|
SamC
White Water Yakist
3467 Posts |
Posted - 2005-12-28 : 11:59:14
|
quote: Originally posted by madhivanan Select (Select count(distinct myint) from mytable where myint<=T.myint) as Sno from mytable T
I'd had a bad execution plan experience as a young child coding a subquery in a select list. Seemed the execution plan would execute the subquery once on every row generated. This trauma shaped my thoughts about how SQL should be written.I'm surprised that the query above has an excellent execution plan, probably better than a comparable plan using JOINS. Go figure. |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2005-12-28 : 12:56:58
|
| I'd had the same traumatic childhood subquery experience. I noticed the plans had become much better somewhere along the way but I still sleep with one eye open when a subquery in a select list is in the next room.Be One with the OptimizerTG |
 |
|
|
|