0

我有两张桌子:

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
4

1 回答 1

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

请参阅带有演示的 SQL Fiddle

结果:

| PSUM | IPRICE | AMT_TYPE |
----------------------------
|  100 |    100 |      USD |
|    0 |   1000 |      GBP |
于 2012-12-12T10:23:54.673 回答