Last active
August 29, 2015 14:16
-
-
Save boy3vil/d1be8f84c275c2a2e43b to your computer and use it in GitHub Desktop.
SQL vertical to horizontal without Transpose
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
| #table ------------------------------------------------------------------------------------------- | |
| Roll_No | Subject | Marks | |
| --------------------------- | |
| 1212324 | MTH | 90 | |
| 1212324 | PHY | 72 | |
| 1212324 | CHE | 85 | |
| 1212324 | BIO | 78 | |
| 1212334 | MTH | 85 | |
| 1212334 | PHY | 65 | |
| 1212334 | CHE | 74 | |
| 1212672 | MTH | 88 | |
| 1212672 | PHY | 42 | |
| 1212672 | BIO | 12 | |
| #SQL ------------------------------------------------------------------------------------------ | |
| SELECT DISTINCT A.ROLL_NO, | |
| B.MARKS AS MTH, | |
| C.MARKS AS PHY, | |
| D.MARKS AS CHE, | |
| E.MARKS AS BIO | |
| FROM yr_table A | |
| LEFT JOIN yr_table B ON A.ROLL_NO = B.ROLL_NO AND B.SUBJECT = 'MTH' | |
| LEFT JOIN yr_table C ON A.ROLL_NO = C.ROLL_NO AND C.SUBJECT = 'PHY' | |
| LEFT JOIN yr_table D ON A.ROLL_NO = D.ROLL_NO AND D.SUBJECT = 'CHE' | |
| LEFT JOIN yr_table E ON A.ROLL_NO = E.ROLL_NO AND E.SUBJECT = 'BIO' | |
| ORDER BY 1 | |
| LIMIT 0 , 30 | |
| #RETURN ----------------------------------------------------------------------- | |
| ROLL_NO | MTH | PHY | CHE | BIO | |
| -------------------------------------- | |
| 1212324 | 90 | 72 | 85 | 78 | |
| 1212334 | 85 | 65 | 74 | NULL | |
| 1212672 | 88 | 42 | NULL | 12 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment