我有两张桌子:
tbl_Invoice
tbl_payment
当我通过client_Id = 5
时,所需的输出应为:
PSUM IPRICE Amt_Type
----------- -------------- ----------------
100.00 100.00 USD
0.00 1000.00 GBP
哪里
PSUM
是总付款总和
IPRICE
是发票总价的总和
我试过的是:
Select SUM(P.Amt) as PSUM, SUM(I.Total_price) as IPRICE, I.Amt_Type from
[tbl_payment] P left join [tbl_Invoice] I on P.invoice_Id = I.invoice_Id
WHERE P.client_id = @Client_Id and I.client_id = @Client_Id
group by I.Amt_Type
答案 0 :(得分:2)
以下似乎有效:
declare @client_id int = 5
select
IsNull(sum(p.amt), 0) PSUM,
IsNull(sum(i.total_price), 0) IPRICE,
i.amt_type
from tbl_invoice i
left join tbl_payment p
on i.invoice_id = p.invoice_id
and i.client_id = p.client_id
where i.client_id = @client_id
group by i.amt_type, i.invoice_id
order by i.invoice_id
结果:
| PSUM | IPRICE | AMT_TYPE |
----------------------------
| 100 | 100 | USD |
| 0 | 1000 | GBP |