简体   繁体   中英

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.

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

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 

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM