Solved

Please help on PIVOT Query in SQL 2005

Posted on 2009-07-09
4
210 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
  • 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I've encountered valid database schemas that do not have a primary key.  For example, I use LogParser from Microsoft to push IIS logs into a SQL database table for processing and analysis.  However, occasionally due to user error or a scheduled task…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

747 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now