Kino
Kino

Reputation: 217

adding a new column SQL query

I have a query like

select SUM(*) as "tot1" from table1 t, table2 t2 where t1.id=t2.id and t1.column=1

select SUM(*) as "tot2" from table1 t, table2 t2 where t1.id=t2.id and t1.column=2

select SUM(*) as "tot3" from table1 t, table2 t2 where t1.id=t2.id and t1.column=3

I want a query result look like this

 tot1     tot2     tot3

 500       600      3

Is this even possible? or is there any alternate solution for me to view these query in same table.

Upvotes: 2

Views: 86

Answers (3)

pas
pas

Reputation: 98

Try this query

select 
    SUM(CASE t1.column WHEN 1 THEN t1.column ELSE 0 END) as tot1, 
    SUM(CASE t1.column WHEN 2 THEN t1.column ELSE 0 END) as tot2, 
    SUM(CASE t1.column WHEN 3 THEN t1.column ELSE 0 END) as tot3 
from 
    table1 t, table2 t2 
where 
    t1.id=t2.id 

Upvotes: 1

attila
attila

Reputation: 2219

try this:

select * from 
(select SUM(*) as "tot1" from table1 t, table2 t2 where t1.id=t2.id and t1.column=1) a,

(select SUM(*) as "tot2" from table1 t, table2 t2 where t1.id=t2.id and t1.column=2) b,

(select SUM(*) as "tot3" from table1 t, table2 t2 where t1.id=t2.id and t1.column=3) c

Upvotes: 2

Cedric Simon
Cedric Simon

Reputation: 4659

Try this:

select SUM(t1.column=1) as "tot1",SUM(t1.column=2) as "tot2", SUM(t1.column=) as "tot3" from table1 t, table2 t2 where t1.id=t2.id

Upvotes: 0

Related Questions