Solved

Pivot query with values

Posted on 2013-05-17
2
279 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
2 Comments
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 250 total points
Comment Utility
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 40

Assisted Solution

by:Sharath
Sharath earned 250 total points
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

This is basically a blog post I wrote recently. I've found that SARGability is poorly understood, and since many people don't read blogs, I figured I'd post it here as an article. SARGable is an adjective in SQL that means that an item can be fou…
Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

762 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

6 Experts available now in Live!

Get 1:1 Help Now