Solved

Pivot Rows to Columns

Posted on 2013-02-05
4
478 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
[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 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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

739 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