i want to display new column in select query which will contain the sum of values from other table with corresponding to the id. I am learning Sql now so just want to know how to do it my problem is further explained as.
Asked
Active
Viewed 1,990 times
1
-
the pivot is not working........do you have any idea... – Zebronix_777 Feb 04 '14 at 07:34
-
you havent tag which DB engine you are using – Miller Feb 04 '14 at 07:35
-
http://stackoverflow.com/questions/13168066/how-do-i-transpose-rows-and-columns-a-k-a-perform-a-pivot-in-postgresql-only this might help you – Miller Feb 04 '14 at 07:38
-
1ys got worked thank you – Zebronix_777 Feb 04 '14 at 07:49
1 Answers
0
SELECT table2.t1_id,table1.name,
sum(case when month='Jan' then amount else 0 end) as jan,
sum(case when month='feb' then amount else 0 end) as feb,
sum(case when month='Mar' then amount else 0 end) as Mar
from table2 inner
join table1 on table1.t1_id=table2.t1_id group by table2.t1_id,name
order by table2.t1_id;

Zebronix_777
- 345
- 4
- 25