Last active
December 11, 2015 20:39
-
-
Save waveform80/4657034 to your computer and use it in GitHub Desktop.
Pivotting columns in SQL...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| WITH qdps_with_rows AS ( | |
| SELECT | |
| ROW_NUMBER() OVER (PARTITION BY q.product_code) AS row_num, | |
| q.* | |
| FROM qdps q | |
| ) | |
| SELECT | |
| p.product_code, | |
| SUM(CASE WHEN q.row_num = 1 THEN q.value END) AS qdp1, | |
| SUM(CASE WHEN q.row_num = 2 THEN q.value END) AS qdp2, | |
| SUM(CASE WHEN q.row_num = 3 THEN q.value END) AS qdp3, | |
| SUM(CASE WHEN q.row_num = 4 THEN q.value END) AS qdp4, | |
| SUM(CASE WHEN q.row_num = 5 THEN q.value END) AS qdp5 | |
| FROM | |
| products p INNER JOIN qdps_with_rows q | |
| ON p.product_code = q.product_code | |
| GROUP BY | |
| p.product_code |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment