| Author |
Topic |
|
mahesh_bote
Constraint Violating Yak Guru
298 Posts |
Posted - 2006-12-14 : 02:11:11
|
| Hi,I have one table: sayCol1 Col2A 2B 1C 3...now i wants o/p like thisCol1 Col2A 1A 1B 1C 1C 1C 1...thanks in advance,Mahesh |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2006-12-14 : 02:28:35
|
| [code]declare @t table( col1 varchar(10), col2 int)insert @tselect 'A', 2 union allselect 'B', 1 union allselect 'C', 3select t1.col1, 1from @t t1 join master..spt_values t2on t1.col2 >= t2.number and t2.[name] is null and t2.number > 0[/code]Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
|
mahesh_bote
Constraint Violating Yak Guru
298 Posts |
Posted - 2006-12-14 : 02:47:00
|
| thanks Harsh,can u explain me what the master..spt_values is? Is it DB object or something else, as i m not aware with this.thanks again,Mahesh |
 |
|
|
mahesh_bote
Constraint Violating Yak Guru
298 Posts |
Posted - 2006-12-14 : 02:50:17
|
quote: Originally posted by mahesh_bote thanks Harsh,can u explain me what the master..spt_values is? Is it DB object or something else, as i m not aware with this.thanks again,Mahesh
I got it, Its user table in Master DB. But why u use this table in join, what is the purpose?Mahesh |
 |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2006-12-14 : 02:57:53
|
| Because to implement your solution, I need table of numbers and I am lazy enough not to create a new one when MS is kind enough to provide ready-made one (although it's for the internal purpose).Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
|
mahesh_bote
Constraint Violating Yak Guru
298 Posts |
Posted - 2006-12-14 : 04:17:37
|
| or try this...Create Table Tab( col1 varchar(10), col2 int)insert into Tabselect 'A', 2 union allselect 'B', 1 union allselect 'C', 3Select Tab.Col1, 1 As Cnt From Tab, (Select (Select Count(*) From Tab Where Tab.Col1 <= Tab2.Col1) As SrNo ,Col1 ,Col2 From Tab As Tab2 ) TmpWhere Tab.Col2 >= Tmp.SrNoOrder By Tab.Col1Note:(but problem with this qry is that, there must be squencial nos in col2. e.g. if u have 10 as max val in col2, there must be 1 to 10 nos in col2.)Mahesh |
 |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2006-12-14 : 04:40:57
|
| Besides that, its way too costly and hard to interpret. Just check the execution plan.Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-12-14 : 10:47:46
|
quote: Originally posted by mahesh_bote Hi,I have one table: sayCol1 Col2A 2B 1C 3...now i wants o/p like thisCol1 Col2A 1A 1B 1C 1C 1C 1...thanks in advance,Mahesh
May I know why you need this?MadhivananFailing to plan is Planning to fail |
 |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2006-12-14 : 11:03:56
|
| May be just for fun? to know whether it is possible to do this with set-based solution...just a brain-teaser!Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
|
|