Solved

Pivot query with values

Posted on 2013-05-17
2
290 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
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 40

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to install/upgrade the Blitz responder kit 8 42
SQL 2008 R2 syntax 11 29
SQL Server - executing an agent job from a stored proc 2 19
Addition to SQL for dynamic fields 6 38
Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

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