[英]sum certain columns of 2 tables matching criteria sql
表1數據:
ACType Dualhr P1hr Total
A320 2 2 4
B787 1 2 3
B777 3 1 4
表2數據:
ACType P1hr Total
A320 5 5
比較兩個表時,什么sql查詢會給我結果:
ACType Dualhr P1hr Total
A320 2 7 9
B787 1 2 3
B777 3 1 4
當Table1為空時,這也應考慮大小寫,以便得出結果:
ACType Dualhr P1hr Total
A320 0 5 5
使用聯合和子查詢:
select ACType, sum(Dualhr) as Dualhr, sum(P1hr) as P1hr,sum(Total) as Total
from
(
select ACType, Dualhr, P1hr, Total from table1
union all
select ACType, 0, P1hr, Total from table2)a
group by ACType
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.