Solved

Pivot query with values

Posted on 2013-05-17
2
309 Views
Last Modified: 2013-10-23
Sample TableI have a table combined_list and I would like the output to be a pivot output that shows for each unique entry in column 0, to return for each year in column 1 have each value in column 2

For example

PN                         2007            2008       2009

AK801US#ABA      7F                7F           87
AK802US#ABA                          87          88
0
Comment
Question by:Fairfield
[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 Comments
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 250 total points
ID: 39175599
CREATE TABLE SomeTable([Column 0] varchar(50), [Column 1] int, [Column 3] varchar(50))

INSERT INTO SomeTable([Column 0], [Column 1], [Column 3]) VALUES
('AK810US#ABA', 2007, '7F'),
('AK810US#ABA', 2008, '7F'),
('AK810US#ABA', 2009, '87'),
('AK820US#ABA', 2007, '7F'),
('AK820US#ABA', 2008, '7F'),
('AK820US#ABA', 2009, '87'),
('AK830US#ABA', 2010, '87')

DECLARE @sql varchar(MAX) = 'SELECT t1.[Column 0] AS PN'

SELECT @sql = @sql + ', (SELECT MAX(t2.[Column 3])
    FROM SomeTable t2
    WHERE t2.[Column 0] = t1.[Column 0] AND t2.[Column 1] = ' +
    CONVERT(varchar, [Column 1]) + ') AS [' + CONVERT(varchar, [Column 1]) + ']'
FROM SomeTable
GROUP BY [Column 1]
ORDER BY [Column 1]

SET @sql = @sql + ' FROM SomeTable t1
    GROUP BY t1.[Column 0]
    ORDER BY t1.[Column 0]'

EXEC(@sql)

DROP TABLE SomeTable
0
 
LVL 41

Assisted Solution

by:Sharath
Sharath earned 250 total points
ID: 39178698
You can try PIVOT also.
select *
  from SomeTable
pivot (max([Column 3]) for [Column 1] in ([2007],[2008],[2009])) as p

Open in new window


see this example: http://sqlfiddle.com/#!3/2ba3b/1
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I have written a PowerShell script to "walk" the security structure of each SQL instance to find:         Each Login (Windows or SQL)             * Its Server Roles             * Every database to which the login is mapped             * The associated "Database User" for this …
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.
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

688 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