簡體   English   中英

添加新列SQL查詢

[英]adding a new column SQL query

我有一個查詢

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

我希望查詢結果看起來像這樣

 tot1     tot2     tot3

 500       600      3

這有可能嗎? 還是有其他替代解決方案供我在同一表格中查看這些查詢。

嘗試這個:

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

試試這個查詢

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 

嘗試這個:

從表1 t,表2 t2中選擇SUM(t1.column = 1)作為“ tot1”,選擇SUM(t1.column = 2)作為“ tot2”,將SUM(t1.column =)作為“ tot3”,其中t1.id = t2 。ID

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM