2012-03-29 86 views
0

我得到這個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 

,但是這不是跑步,所以我希望有人可以幫我完成我的查詢

回答

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 
....