Solved

Pivot Rows to Columns

Posted on 2013-02-05
4
477 Views
Last Modified: 2013-02-05
I have a table the holds 2 values for each item,Current results the Max and Min values along with the flag for those values. I need the Values and the levels to be on the same row so that I only have 1 item number with those values, I was thinking of using Pivot but not sure how to do it dynamically as there are 760 rows. Any help would be greatly appreciated.
   
SELECT
[ITEMNMBR] = EXT00103.PT_UD_Key ,[VALUE ] = EXT00103.TOTAL,[LEVELS] = EXT00103.PT_UD_Number 
FROM         EXT00103

Open in new window

0
Comment
Question by:skull52
  • 2
  • 2
4 Comments
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 38856468
like this ?

select itemnmbr,[1] as max,[2] as min
from ext00103 as a
pivot (max(value) for levels in ([1],[2])) as pvt
where levels in ('1','2')
order by 1
0
 

Author Comment

by:skull52
ID: 38856598
Thanks for the quick response Low, but I am getting the following  errors

Msg 207, Level 16, State 1, Line 3
Invalid column name 'value'.
Msg 207, Level 16, State 1, Line 3
Invalid column name 'levels'.
Msg 207, Level 16, State 1, Line 4
Invalid column name 'levels'.
Msg 207, Level 16, State 1, Line 4
Invalid column name 'levels'.
Msg 207, Level 16, State 1, Line 1
Invalid column name 'itemnmbr'.
0
 
LVL 50

Accepted Solution

by:
Lowfatspread earned 500 total points
ID: 38856683
yes because i just read you column names from the result table you showed rather than your query

try this

select itemnmbr,[1] as max,[2] as min
from (SELECT
[ITEMNMBR] = PT_UD_Key ,[VALUE] = TOTAL,[LEVELS] = PT_UD_Number
FROM         EXT00103
where PT_UD_Number  in ('1','2')) as a
pivot (max(value) for levels in ([1],[2])) as pvt

order by 1
0
 

Author Comment

by:skull52
ID: 38856942
Perfect, Thank you
0

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

820 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