我得到了这个Sql查询
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
现在我的问题是,我想为每个订单返还一笔款项。 我尝试将查询更改为
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
但那没有运行,所以我希望有人可以帮我完成我的查询
答案 0 :(得分:1)
在您的子选择中,您指的是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
....