我需要彙總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
我怎麼能渡過這個?任何幫助將不勝感激。
現在工作正常,謝謝! –