我需要汇总Amounts
以按月份的日期范围显示。为了说明请大家看看下面的表中:根据特定日期范围在一个月内汇总多个列
Invoice_Payment
Customer_id Invoice_no Invoice_date Amount
---------------------------------------------------
10 10023 2016-07-08 60
10 10018 2016-08-04 90
11 10016 2016-07-01 110
11 10021 2016-07-05 120
12 10028 2016-07-11 10
12 10038 2016-07-31 5
正如您看到的,我想他们基于Customer_id
组和从头到尾显示的日期。此外,这只能在每个月进行。
下面的查询到目前为止,我曾尝试:
select Customer_id, (mindate + ' to ' + maxdate) Date_Range, Amount
from (
select Customer_id, sum(Amount) Amount, min(Invoice_date) mindate, max(Invoice_date) maxdate
from Invoice_Payment
group by Customer_id
) I ;
从上面查询我越来越喜欢Output
:
Customer_id Date_Range Amount
10 2016-07-08 to 2016-08-04 150
11 2016-07-01 to 2016-07-05 230
12 2016-07-11 to 2016-07-31 15
请检查这个.. SQL Fiddle Working Demo
比方说, Customer_id = 10
谁在July,2016
和中有Invoice_date。我需要在特定日期范围内分别总结该特定客户在七月份和八月份的所有付款。但是我从上面的努力获得所有Invoice_date
的Amount
的总和。
所需的输出:
Customer_id Date_Range Amount
10 2016-07-08 to 2016-07-08 60
10 2016-08-04 to 2016-08-04 90
11 2016-07-01 to 2016-07-05 230
12 2016-07-11 to 2016-07-31 15
我怎么能渡过这个?任何帮助将不胜感激。
现在工作正常,谢谢! –