sql - Unpivot Multiple Columns in into Rows -
i did searching , didn't come answer. trying create table data , unpivot in sql server, can't columns , rows required.
if object_id('tempdb..##expect_percents') not null drop table ##expect_percents create table ##expect_percents ( payor_id varchar(40) ,test_type varchar(40) ,[2011-08-31] int ,[2011-09-30] int ) go insert ##expect_percents values('uhc','udt','1','2'); select * ##expect_percents select payor_id, test_type, expect ##expect_percents unpivot ( expect expects in ([2011-08-31],[2011-09-30]) ) u
i trying unpivot dates there field called "date" 2 dates representing each number. first line should be:
uhc udt 1 2011-08-31
needing unpivot multiple columns can easier without unpivot
command. 1 option use cross apply
:
select payor_id, test_type, expect, expectdate expect_percents cross apply ( select [2011-08-31],'2011-08-31' union select [2011-09-30],'2011-09-30' ) c (expect, expectdate);
another option use union all
:
select payor_id, test_type, [2011-08-31] expect, '2011-08-31' date expect_percents union select payor_id, test_type, [2011-09-30] expect, '2011-09-30' date expect_percents;
Comments
Post a Comment