Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Please help on PIVOT Query in SQL 2005

Posted on 2009-07-09
4
Medium Priority
?
274 Views
Last Modified: 2012-05-07
Hello,

I need to turn data columns into data a row using PIVOT query in SQL 2005 or crosstab query? Please see attached image for more information. Thank you very much
MWSnap122.jpg
0
Comment
Question by:ixcorp
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 41

Expert Comment

by:ralmada
ID: 24817451
Are the specs and hearder texts fixed values? If so you can try something like this:

with CTE as (
select PartNumber, cast(Spec as varchar) as tcolumn
from yourtable
union all
select PartNumber, HeaderText as tcolumn
from yourtable
)
 
select Partnumber,
	[100] as Spec1,
	[60] as Spec2,
	...
from
(select * from CTE) o
pivot (max(tcolumn) for tcolumn in ([100], [60], [0.017], ... [VDSS,max,(V)]...)p

Open in new window

0
 

Author Comment

by:ixcorp
ID: 24817834
Hi ralmada,

Thanks for quick response. Unfortunately the specs and header texts are not fixed values. I should have clarify this in my question. See attached image for more information.

Thanks,
Dat
MWSnap123.jpg
0
 
LVL 41

Accepted Solution

by:
ralmada earned 2000 total points
ID: 24818012
What about something like this then?

with CTE as (
select PartNumber, cast(Spec as varchar) as tcolumn, 'spec' + cast(row_number() over (partition by PartNumber order by PartNumber) as varchar) as rn
from yourtable
union all
select PartNumber, cast(HeaderText) as tcolumn, 'header' + cast(row_number() over (partition by PartNumber order by PartNumber) as varchar) as rn
from yourtable
)
 
select 	PartNumber,
	[spec1],
	[spec2],
	...
	[header1],
	...
from
(select * from CTE) o
pivot (max(tcolumn) for rn in ([spec1], [spec2], .... , [header1], [header2], ....)p

Open in new window

0
 

Author Closing Comment

by:ixcorp
ID: 31601779
Working perfectly. Thank you very much!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question