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 |
|
Ranjit.ileni
Posting Yak Master
183 Posts |
Posted - 2011-05-31 : 08:19:43
|
| Hi,Here is the tablecreate table #temp(subcustid int,paxcount int)insert into #tempselect 2,5 union allselect 3,10 union allselect 4,2 union allselect 5,9Expected output:for example subcustid 2 ---> paxcount 5i need to get 5 rows for subcustid 2 as 2/p1,2/p2,2/p3,2/p4,2/p5i need to retrive this results from a VIEWsubcustId/pax2/p12/p22/p32/p42/p53/p13/p23/p33/p43/p53/p63/p73/p83/p93/p104/p14/p25/p15/p25/p35/p45/p55/p65/p75/p85/p9Here /p is static .. Thanks,--Ranjit |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2011-05-31 : 08:26:22
|
| ;with cteas(select subcustid,paxcount, 1 as NextRow from #tempunion allselect subcustid,paxcount, NextRow+1 from ctewhere nextrow < paxcount)select subcustid,paxcount,'p' + convert(varchar(10),NextRow) from cteorder by 1,2JimEveryday I learn something that somebody else already knew |
 |
|
|
Ranjit.ileni
Posting Yak Master
183 Posts |
Posted - 2011-05-31 : 08:28:19
|
| Thanks Jimf....Actually recursive CTE Idea not came to mind...--Ranjit |
 |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2011-05-31 : 08:49:03
|
| You're welcome. Recursive CTEs are finally start to come to my mind first. I usually would have solved this one using master..spt_Values, and then someone else would solve it better with the CTE.JimEveryday I learn something that somebody else already knew |
 |
|
|
dujiaojing0
Starting Member
12 Posts |
Posted - 2011-05-31 : 21:06:41
|
| That's great answer from Jim. Really appreciate that. Thanks.Simonunspammed |
 |
|
|
|
|
|
|
|