?
Solved

Please help on PIVOT Query in SQL 2005

Posted on 2009-07-09
4
Medium Priority
?
265 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

This article will describe one method to parse a delimited string into a table of data.   Why would I do that you ask?  Let's say that you need to pass multiple parameters into a stored procedure to search for.  For our sake, we'll say that we wa…
INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

777 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