[英]select from two tables and group by a common column date in both tables
伙計們,我尋找了解決方案,但無法解決我的問題,如果有人能幫助我,我將不勝感激我有三個表term1
, term2
, term3
,它們具有相同的列Adm
, date
, amountneeded
, amountpaid
,日期格式為yyyy-mm-dd
我需要從三個表中檢索total amountpaid
的total amountpaid
year
group
結果應該是
year total
2012 323000
2013 423000
試試這個
select tyear,sum(total)
from (
select DATEPART(yyyy,date) as tyear ,sum(amountpaid) as total
from term1
group by DATEPART(yyyy,date)
union all
select DATEPART(yyyy,date) as tyear ,sum(amountpaid) as total
from term2
group by DATEPART(yyyy,date)
union all
select DATEPART(yyyy,date) as tyear ,sum(amountpaid) as total
from term1
group by DATEPART(yyyy,date)
)a group by tyear
select tyear,sum(total) from ( select EXTRACT(YEAR FROM date) as tyear ,sum(amountpaid) as total from term1 group by tyear union all select EXTRACT(YEAR FROM date) as tyear ,sum(amountpaid) as total from term2 group by tyear union all select EXTRACT(YEAR FROM date) as tyear ,sum(amountpaid) as total from term1 group by tyear )a group by tyear
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.