2017-04-20 10 views
-1

私はテーブルpartywise_account_ledgerとカラムname_of_the_customer,invoice_dateおよびinvoice_valueを持っています。私は、日数に応じてinvoice_valueを検索したいと思います。私は、クエリを使用< 5 daysの値を取得するには:カラムを取得する方法SQLの2つの日付の間の日数を持つレコード

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式:(< 10日間)SUM()

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(invoice_value)として簡体することができます。

+0

多くの人が大変感謝してくれたjarlh –

関連する問題