April 27, 2010 at 2:04 am
Hi Everyone ,
I have a table with data as shown below
UniqueId ParameterName ParameterValue
-------------------------------------------------------------
300 Parameter Name1 Parameter Value300_1
300 Parameter Name2 Parameter Value300_2
300 Parameter Name3 Parameter Value300_3
301 Parameter Name1 Parameter Value301_1
301 Parameter Name2 Parameter Value301_2
301 Parameter Name3 Parameter Value301_3
302 Parameter Name1 Parameter Value302_1
302 Parameter Name2 Parameter Value302_2
302 Parameter Name3 Parameter Value302_3
The datatype of the columns are
UniqueId - numeric(18, 0) , NOT NULL
ParameterName - varchar(50) NULL
ParameterValue - varchar(50) NULL
How do i write a pivot statement so that i can get the o/p as the following
UniqueId Parameter Name1 Parameter Name2 Parameter Name2
------------------------------------------------------------------------------
300Parameter Value300_1 Parameter Value300_2 Parameter Value300_3
301Parameter Value301_1 Parameter Value301_2 Parameter Value301_3
302Parameter Value302_1 Parameter Value302_2 Parameter Value302_3
Regards
Sabarish
wwewew
April 27, 2010 at 1:29 pm
You might want to have a look at the CrossTab article referenced in my signature. This will show you an alternative to the PIVOT statement. If you need to display a varying number of columns you might want to continue with reading DynamicCrossTabs (also referenced in my signature).
And, yet another option would be to do the pivoting in the presentation layer. ReportingServices does a good job with that sort of requirements...
April 27, 2010 at 2:12 pm
Like this:
select
UniqueId,
max(case when ParameterName = 'Parameter Name1' then ParameterValue else null end) as 'Paramater Name1',
max(case when ParameterName = 'Parameter Name2' then ParameterValue else null end) as 'Paramater Name2',
max(case when ParameterName = 'Parameter Name3' then ParameterValue else null end) as 'Paramater Name3'
from
dbo.MyTable
group by
UniqueId
order by
UniqueId;
April 27, 2010 at 8:29 pm
Thanks for the reply... will try this and get back
wwewew
April 29, 2010 at 4:07 am
Hi.,
if u really want try pivot property in sql server then jst try this.............
select * from
(
select unique_id,parameter_name,parameter_value from dbo.MyTable
) ttbl
PIVOT
(
max(parameter_value) --in case of max u can use sum,avg any aggregate function
for parameter_name in ([Parameter Name1],[Parameter Name2],[Parameter Name3])
) AS PIVOT_TBL
give me
April 30, 2010 at 3:05 am
Thanks , both the cross tab and pivot worked
wwewew
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply