Solved

Pivot Rows to Columns

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Access 2003 query lost it's only join 7 27
Help in Bulk Insert 9 35
SQL Query assistance 16 27
SQL Query Help Top 1 and Distinct? 6 26
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

803 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