[英]how to add two query results as a single result in sql?
I need to add two queries output as single result. 我需要将两个查询输出添加为单个结果。
first query output is from "InvoiceDetail" table: 第一个查询输出来自“ InvoiceDetail”表:
InvoiceID, POSCode, CQuantity, CPrice, TCost, AmountPaid
19327 07310000504 1 22.38 22.38 368.59
19326 07161030192 1 28.90 28.90 5402.69
19326 07013711044 2 12.53 25.06 5402.69
19327 07310000622 2 6.28 12.56 368.59
19326 07013721043 1 25.07 25.07 5402.69
19327 07310000626 2 6.28 12.56 368.59
19327 01230050268 2 37.15 74.30 368.59
19326 04210000910 2 9.84 19.68 5402.69
19327 01230050271 1 37.15 37.15 368.59
and my second query output is from "InvoiceCharge" table: 我的第二个查询输出是来自“ InvoiceCharge”表:
InvoiceID totalCharge
19326 9.00
19326 6.00
finally i want output like: 最后我想要输出像:
InvoiceID, POSCode, CQuantity, CPrice, TCost, AmountPaid, totalCharge 发票ID,POSCode,CQuantity,CPrice,TCost,已付金额,总费用
19327 07310000504 1 22.38 22.38 368.59 0.00
19326 07161030192 1 28.90 28.90 5402.69 15.00
19326 07013711044 2 12.53 25.06 5402.69 15.00
19327 07310000622 2 6.28 12.56 368.59 0.00
19326 07013721043 1 25.07 25.07 5402.69 15.00
19327 07310000626 2 6.28 12.56 368.59 0.00
19327 01230050268 2 37.15 74.30 368.59 0.00
19326 04210000910 2 9.84 19.68 5402.69 15.00
19327 01230050271 1 37.15 37.15 368.59 0.00
this is how i need.. 这就是我需要的..
here InvoiceID 19326 having charge(9.00+6.00)= 15.00 and InvoiceID 19327 having no charge, so no row found in InvoiceCharge table. 这里的InvoiceID 19326的费用为(9.00 + 6.00)= 15.00,InvoiceID 19327的费用为免费,因此在InvoiceCharge表中找不到行。
Please tried below query 请在下面查询
select
InvoiceID,
POSCode,
CQuantity,
CPrice,
TCost,
AmountPaid,
(SELECT sum(totalCharge) FROM InvoiceCharge IC WHERE IC.InvoiceID = ID.InvoiceID) AS totalCharge
from InvoiceDetail ID
try this: 尝试这个:
select InvoiceID,
POSCode,
CQuantity,
CPrice,
TCost,
AmountPaid,
(case when a.charge is null then 0 else a.charge end)) as totalCharge
from InvoiceDetail left join (SELECT sum(totalCharge) as charge , InvoiceCharge.InvoiceID FROM InvoiceCharge group by InvoiceCharge.InvoiceID) a on InvoiceCharge.InvoiceID = InvoiceDetail.InvoiceID
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.