Solved

Please help on PIVOT Query in SQL 2005

Posted on 2009-07-09
4
256 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 500 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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Suggested Solutions

So every once in a while at work I am asked to export data from one table and insert it into another on a different server.  I hate doing this.  There's so many different tables and data types.  Some column data needs quoted and some doesn't.  What …
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

738 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