i have table contains data shown below.
**fieldid documentid classid prevalue prescore prestatus postvalue postscore postvaluechanged** 1 123 1 john 80.00 y john 80.00 n 2 123 1 123456 71.23 n 1234 0.00 y 1 124 2 tom 95.65 y tom 95.65 n 2 124 2 5689 53.26 n 5689 53.26 y could please sql data in following fashion:-
**documentid classid 1-prevalue 1-prescore 1-prestatus 1-postvalue 1-postscore 1-postvaluechanged 2-prevalue 2-prescore 2-prestatus 2-postvalue 2-postscore 2-postvaluechanged** 123 1 john 80 y john 80 n 123456 71.23 n 1234 0 y 124 2 tom 95.65 y tom 95.65 n 5689 53.26 n 5689 53.26 y i tried using pivot clause couldn't it. please help.
----update
i able achieve prevalue column... couldn't make rest of 5 columns... struggling how concatenate rest 10 colmns i.e. 2*5. here's snippet..
declare @colpivot varchar(max) select @colpivot = stuff((select distinct top 100 percent ',' + quotename(convert(varchar(255),rtrim(ltrim(t2.fieldid))) + 'prevalue') [dbo].[tbldocument] t2 inner join [dbo].[tbldocument] t3 on t3.documentid=t2.documentid t2.batchclassid=t3.batchclassid order ',' + quotename(convert(varchar(255),rtrim(ltrim(t2.fieldid))) + 'prevalue') xml path(''), type ).value('.', 'nvarchar(max)'),1,1,'') print @colpivot declare @query varchar(max) set @query = 'select * (select [documentid], [classid], [prevalue], convert(varchar(255),[fieldid]) + ''prevalue'' fieldwise [testdb].[dbo].[tbldocument] )a pivot(max([prevalue]) fieldwise in ('+ @colpivot +'))p' print (@query) exec (@query) is possible create view above code is?
thanks, inder
Comments
Post a Comment