クエリで同じテーブルに参加しようとしていますが、重複する行があります。同じテーブルに重複する行を結合する
私が得たいのは、年間の請求書の合計と、各クライアントの各Trimesterの列です。ここで
は、クエリの簡易版である:
select client.name, sum(total.totdoc) as TOTAL, sum(t1.totdoc) as T1
from client
join invoices total
on total.idclient=client.idclient
join invoices t1
on t1.idclient=total.idclient
where t1.date between '01/01/2016' and '30/03/2016'
and total.date between '01/01/2016' and '31/12/2016'
group by client.name
having sum(total.income) > '1000'
例データ(クライアントはちょうどidclientと名前です):
Invoices table
Idclient totdoc date
1 123.56 '01/02/2016'
1 1,258.61 '05/05/2016'
2 2,557.32 '07/03/2016'
3 123.56 '30/11/2016'
outpotは、次のようになります。
client name Total T1
A 1382.17 123.56
B 2,557.32 2,557.32
私が手動でそれらの1つをチェックすると、結果は約3.000になるはずです。私は81.000に近づいています
私はsum(distinct total.totdoc)
で試してみましたが、いくつか(合計が異なるもの)で動作しますが、繰り返されるものは紛失します。
これもまた単純化された例なので、不明な点がある場合は質問してください。
ありがとうございます!
はあなたが欠けていると考えて... client.name – Veljko89
でグループここに行方不明の引用があります: ''01/01/2016' の間t1.dateと30/06/2016''は、このような何か – McNets
いくつかのサンプルデータを提供することができます –