如何按月计算净额

时间:2010-08-03 07:06:35

标签: sql

SELECT EXTRACT(来自doc_date的月份)columnno,sum(net_amt_rm)sum_amt,max(doc_date)ddate
    来自POSM
    提取的地方(来自doc_date的年份)='2010'     GROUP BY EXTRACT(来自doc_date的月份)

UNION //union no 2  
SELECT EXTRACT(month from doc_date) columnno, sum(net_amt_rm) sum_amt, max(doc_date) ddate  
FROM CIM  
WHERE EXTRACT(year from doc_date) = '2010'  
GROUP BY EXTRACT(month from doc_date)   

UNION  //union no3  
SELECT EXTRACT(month from doc_date) columnno, sum(net_amt_rm) sum_amt, max(doc_date) ddate  
FROM CAM  
WHERE EXTRACT(year from doc_date) = '2010'  
GROUP BY EXTRACT(month from doc_date)

此查询的输出如下:

columnno | sum amt | ddate  
3             6      01/march/2010  
7            250     29/july/2010 // output for union no2  
7           2617     02/july/2010 // output for union no3

但我希望我的输出像这样

columnno | sum amt | ddate  
3             6      march  
7            2867    july //output for union no 2 and 3 are sum together to get only one july

表示仅在一个月内累计金额

1 个答案:

答案 0 :(得分:2)

尝试这个

select columnno , sum(sum_amt), EXTRACT(month from ddate) ddate from 
<your query> 
group by columnno , (EXTRACT(month from ddate))