簡體   English   中英

從兩個表中選擇並按兩個表中的公共列日期分組

[英]select from two tables and group by a common column date in both tables

伙計們,我尋找了解決方案,但無法解決我的問題,如果有人能幫助我,我將不勝感激我有三個表term1term2term3 ,它們具有相同的列Admdateamountneededamountpaid ,日期格式為yyyy-mm-dd我需要從三個表中檢索total amountpaidtotal 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.

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