2017-04-20 117 views
-1

我有一個表partywise_account_ledgername_of_the_customer,invoice_dateinvoice_value。我想根據天數檢索invoice_value。要獲得< 5 days值我使用查詢:如何獲取列記錄sql中兩個日期之間的天數

select name_of_the_customer, sum(invoice_value) 
from partywise_accounts_ledger 
where datediff(d, invoice_date, '2017-04-19') < 5 
group by name_of_the_customer 

This is the Result of the Query

我怎樣才能獲得< 10 days列在單獨的語句追加到它,並說明了什麼?

謝謝!

+0

添加一些示例表格數據和預期結果 - 以及格式化文本。 – jarlh

+0

請參閱http://meta.stackoverflow.com/questions/333952/why-should-i-provide-an-mcve-for-what-seems-to-me-to-be-a-very-simple-sql-查詢 – Strawberry

回答

1

使用case表達式條件聚合:

select name_of_the_customer, 
     sum(case when datediff(d, invoice_date, '2017-04-19') < 5 then invoice_value end), 
     sum(case when datediff(d, invoice_date, '2017-04-19') < 10 then invoice_value end) 
from partywise_accounts_ledger 
where datediff(d, invoice_date, '2017-04-19') < 10 
group by name_of_the_customer 

第二SUM()(對於< 10天),可作爲simplifed sum(invoice_value)

+0

jarlh幫助很多人非常感謝你 –

相關問題