Calc的订单和订单项目,其中包含产品
问题描述:
我得到这个SQL查询Calc的订单和订单项目,其中包含产品
declare @ProductNo int
set @ProductNo = 123
select o.OrderNo, OrderDate, ol.Amount
from OrderLine ol
inner join [Order] o on ol.OrderNo = o.OrderNo
where ol.ProductNo = @ProductNo
现在的问题是,我想回一笔每个订单。 我tryid改变我的查询
select o.OrderNo, OrderDate, ol.Amount,(select sum(ol.Amount * ol.UnitPrice) as OrderTotal from OrderLine oll where oll.OrderNo = o.OrderNo)
from OrderLine ol
inner join [Order] o on ol.OrderNo = o.OrderNo
where ol.ProductNo = @ProductNo
,但是这不是跑步,所以我希望有人可以帮我完成我的查询
答
在你的子选择你指的是ol.Amount * ol.UnitPrice
,但你的OrderLine
表在子查询中被称为oll
,所以下面应该这样做:
select o.OrderNo, OrderDate, ol.Amount,(select sum(oll.Amount * oll.UnitPrice) from OrderLine oll where oll.OrderNo = o.OrderNo) as OrderTotal
....