Author |
Topic |
wypwong
Starting Member
9 Posts |
Posted - 2012-10-09 : 08:15:08
|
Hi allI have this tableMonthBegDate Rate20121 1.5620122 1.60 20123 1.6220124 1.60and I want it to convert the table to look like thisMonthBegDate Mth1Rate Mth2Rate Mth3Rate Mth4Rate20121 1.56 1.60 1.62 1.6020122 1.56 1.60 1.62 1.6020123 1.56 1.60 1.62 1.6020124 1.56 1.60 1.62 1.60Hope someone can help me here. Thanks.Phyllis |
|
lazerath
Constraint Violating Yak Guru
343 Posts |
Posted - 2012-10-09 : 18:34:12
|
Look into the PIVOT operator. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-10-09 : 21:32:55
|
are there always four values per MonthBegDate?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
nandhujob88
Starting Member
4 Posts |
Posted - 2012-10-10 : 06:35:26
|
Hi Please find below code for your problemCreate Table #Temp(MonthBegDate int,Rate float)Insert Into #Temp Select 20121, 1.56 Union AllSelect 20122, 1.60 Union AllSelect 20123 ,1.62 Union AllSelect 20124 ,1.60 Select * From #TempSelect MonthBegDate , ( Select Stuff ( (Select ','+Cast(Rate AS Varchar(100)) From #Temp For Xml Path('')), 1, 1, '') )From #Temp tDrop Table #TempNandhu - Software Analyst |
|
|
nandhujob88
Starting Member
4 Posts |
Posted - 2012-10-10 : 06:38:06
|
let me know the feedback pleaseNandhu - Software Analyst |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-10-11 : 00:19:37
|
quote: Originally posted by nandhujob88 Hi Please find below code for your problemCreate Table #Temp(MonthBegDate int,Rate float)Insert Into #Temp Select 20121, 1.56 Union AllSelect 20122, 1.60 Union AllSelect 20123 ,1.62 Union AllSelect 20124 ,1.60 Select * From #TempSelect MonthBegDate , ( Select Stuff ( (Select ','+Cast(Rate AS Varchar(100)) From #Temp For Xml Path('')), 1, 1, '') )From #Temp tDrop Table #TempNandhu - Software Analyst
this will put all values as comma separated list in same field which is not OP want as per sample output;With MonthlyRatesAS(SELECT ROW_NUMBER() OVER ( ORDER BY monthBegDate) AS Seq,*FROM table)SELECT *FROM(SELECT MonthBegDate,Seq,RateFROM (SELECT Seq,Rate FROM MonthlyRates) tCROSS JOIN (SELECT MonthBegDate FROM MonthlyRates) m)mPIVOT(MAX(Rate) FOR Seq IN ([1],[2],[3],[4]))n ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
wypwong
Starting Member
9 Posts |
Posted - 2012-10-11 : 05:50:15
|
Wow... Visakhm, it's magic. Thank you so much. One thing I didn't mention in my first message, my currency table is a lot more complex, I have different currencies and a lot more years.MonthBegDate Rate20111 1.4020112 1.38...201112 1.8 20121 1.5620122 1.60 20123 1.6220124 1.60....and I want it to convert the table to look like thisMonthBegDate Mth1Rate Mth2Rate Mth3Rate Mth4Rate20111 1.40 1.38 ...20112 1.40 1.38 ......201112 1.4 1.38 ..... 1.8 (in Mth12Rate)20121 1.56 1.60 1.62 1.6020122 1.56 1.60 1.62 1.6020123 1.56 1.60 1.62 1.6020124 1.56 1.60 1.62 1.60I have asked my colleague about it, he has used cursor to produce that. But I wonder whether there is other methods. |
|
|
wypwong
Starting Member
9 Posts |
Posted - 2012-10-11 : 05:50:15
|
Wow... Visakhm, it's magic. Thank you so much. One thing I didn't mention in my first message, my currency table is a lot more complex, I have different currencies and a lot more years.MonthBegDate Rate20111 1.4020112 1.38...201112 1.8 20121 1.5620122 1.60 20123 1.6220124 1.60....and I want it to convert the table to look like thisMonthBegDate Mth1Rate Mth2Rate Mth3Rate Mth4Rate20111 1.40 1.38 ...20112 1.40 1.38 ......201112 1.4 1.38 ..... 1.8 (in Mth12Rate)20121 1.56 1.60 1.62 1.6020122 1.56 1.60 1.62 1.6020123 1.56 1.60 1.62 1.6020124 1.56 1.60 1.62 1.60I have asked my colleague about it, he has used cursor to produce that. But I wonder whether there is other methods. |
|
|
wypwong
Starting Member
9 Posts |
Posted - 2012-10-11 : 05:50:16
|
Wow... Visakhm, it's magic. Thank you so much. One thing I didn't mention in my first message, my currency table is a lot more complex, I have different currencies and a lot more years.MonthBegDate Rate20111 1.4020112 1.38...201112 1.8 20121 1.5620122 1.60 20123 1.6220124 1.60....and I want it to convert the table to look like thisMonthBegDate Mth1Rate Mth2Rate Mth3Rate Mth4Rate20111 1.40 1.38 ...20112 1.40 1.38 ......201112 1.4 1.38 ..... 1.8 (in Mth12Rate)20121 1.56 1.60 1.62 1.6020122 1.56 1.60 1.62 1.6020123 1.56 1.60 1.62 1.6020124 1.56 1.60 1.62 1.60I have asked my colleague about it, he has used cursor to produce that. But I wonder whether there is other methods. |
|
|
wypwong
Starting Member
9 Posts |
Posted - 2012-10-11 : 06:39:40
|
Hi Nandhu Thanks for showing me how to use xml path, I didn't know this function. I can see where you are getting to, but I do need information in columns rather than a long text.P |
|
|
wypwong
Starting Member
9 Posts |
Posted - 2012-10-11 : 06:39:41
|
Hi Nandhu Thanks for showing me how to use xml path, I didn't know this function. I can see where you are getting to, but I do need information in columns rather than a long text.P |
|
|
wypwong
Starting Member
9 Posts |
Posted - 2012-10-11 : 06:39:41
|
Hi Nandhu Thanks for showing me how to use xml path, I didn't know this function. I can see where you are getting to, but I do need information in columns rather than a long text.P |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-10-11 : 15:49:52
|
quote: Originally posted by wypwong Wow... Visakhm, it's magic. Thank you so much. One thing I didn't mention in my first message, my currency table is a lot more complex, I have different currencies and a lot more years.MonthBegDate Rate20111 1.4020112 1.38...201112 1.8 20121 1.5620122 1.60 20123 1.6220124 1.60....and I want it to convert the table to look like thisMonthBegDate Mth1Rate Mth2Rate Mth3Rate Mth4Rate20111 1.40 1.38 ...20112 1.40 1.38 ......201112 1.4 1.38 ..... 1.8 (in Mth12Rate)20121 1.56 1.60 1.62 1.6020122 1.56 1.60 1.62 1.6020123 1.56 1.60 1.62 1.6020124 1.56 1.60 1.62 1.60I have asked my colleague about it, he has used cursor to produce that. But I wonder whether there is other methods.
sorry didnt understand why you cant extend my suggestion to fit these additional month values------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
nandhujob88
Starting Member
4 Posts |
Posted - 2012-10-12 : 02:36:46
|
Hi wypwong,Your Always Welocme..Nandhu - Software Analyst |
|
|
|