tblOrders
OrderId totalamount OrderStatus
01 1000 4
02 2000 4tblCart
CartId OrderId NetPrice
05 01 400
06 01 650
07 02 750
08 02 1350期望的结果:
OrderId totalamount OrderStatus NetPrice ItemCount
01 1000 4 1050 2
02 2000 4 2100 2我想在OrderId Link的基础上用SUM(Netprice)实现两个表的连接
我尝试对存储过程使用SQL查询来执行此操作,但它给出了不明确的列错误。
发布于 2020-06-09 15:25:55
请使用以下查询,
select t1.OrderId, t1.totalamount, tl1.OrderStatus, sum(t2.NetPrice) as NetPrice ,
count(1) as ItemCount from
tblOrders t1
inner join tblCart t2
on (t1.OrderId = t2.OrderId)
group by t1.OrderId, t1.totalamount, tl1.OrderStatus;发布于 2020-06-09 20:02:17
如果您希望orders表中的所有列和另一个表中的汇总数据,您可能会发现apply很有帮助:
select o.*, c.*
from tblOrders o outer apply
(select sum(c.netprice) as netprice, count(*) as itemcount
from tblCart c
where c.OrderId = o.OrderId
) c;https://stackoverflow.com/questions/62277127
复制相似问题