Pivot Query

Source Data

YEAR QUARTER RESULTS
———– ———– ———–
2004 1 20
2004 2 30
2004 3 15
2004 4 10
2005 1 18
2005 2 40
2005 3 12
2005 4 27

Expected Result Set

YEAR Q1 Q2 Q3 Q4
———– ———– ———–
2004 20 30 15 10
2005 18 40 12 27
Solution
SELECT Year,MAX(CASE WHEN Quarter = 1THEN Results END) AS Q1,MAX(CASE WHEN Quarter = 2THEN Results END) AS Q2,MAX(CASE WHEN Quarter = 3THEN Results END) AS Q3,MAX(CASE WHEN Quarter = 4THEN Results END) AS Q4FROM SalesGROUP BY Year

Courtesy:
http://it.toolbox.com/blogs/db2luw/pivot-query-12757

స్పందించండి

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / మార్చు )

Twitter picture

You are commenting using your Twitter account. Log Out / మార్చు )

Facebook photo

You are commenting using your Facebook account. Log Out / మార్చు )

Google+ photo

You are commenting using your Google+ account. Log Out / మార్చు )

Connecting to %s