How to add temporary colomn which is selecting within the same table?

Viewed 33

i want to show a table that will show one more column, that column contains select statement within the table, i cant explain well but the example is like this...

I dont know the query for this

grade table:                                                  
+---------------------------------+     
| ID     Name     grade   quarterID       
----------------------------------      
| 1     | A     |  85    |    1         
| 2     | B     |  86    |    1          
| 3     | C     |  84    |    1         
| 4     | A     |  90    |    2
| 5     | B     |  88    |    2
| 6     | C     |  81    |    2

My Expected output is:

+-------------------------+
| Name | grade   | grade         
---------------------------
| A    |  85     |  90
| B    |  86     |  88
| C    |  84     |  81
3 Answers

You can try using conditional aggregation

select name, max(case when quarterID=1 then grade end) as grade1,
max(case when quarterID=2 then grade end) as grade2
from tablename
group by name

use case when with aggregate fucntion

select name, max(case when quarterID=1 then grade end),
max(case when quarterID=2 then grade end) from t group by name

Just copy and paste the below code

SELECT Name,max(case when quarterID=1 
                     then grade 
                     else 0 
                 end) as grade,
            max(case when quarterID=2
                     then grade
                     else 0
                 end) as grade
FROM grade
GROUP BY Name
Related