2009-12-13 221 views
0
SELECT @Tax = SUM(QuoteItem.SalesPrice) * TOP (1) Tax.Amount 
FROM Tax INNER JOIN 
     Job ON Tax.TaxId = Job.TaxId INNER JOIN 
     Quote ON Job.JobId = Quote.JobId INNER JOIN 
     QuoteItem INNER JOIN 
     Room ON QuoteItem.RoomId = Room.RoomId ON Quote.QuoteId = Room.QuoteId 
WHERE (Room.QuoteId = @QuoteId) AND (QuoteItem.UnitId = @UnitId) 
    RETURN @Tax 

結果:需要一個SQL查詢幫助

Msg 156, Level 15, State 1, Procedure fn_GetQuoteUnitTax, Line 54 
Incorrect syntax near the keyword 'TOP'. 

注意,當我忽略了TOP(1)它說:

Msg 8120, Level 16, State 1, Procedure fn_GetQuoteUnitTax, Line 54 
Column 'Tax.Amount' is invalid in the select list because it is not contained in 
either an aggregate function or the GROUP BY clause. 
+1

如果你告訴我們你是什麼這將有助於試圖完成。 – 2009-12-13 12:36:30

+0

我想要做的事情很簡單,你可以從查詢中看到它,我想把所有的QuoteItems的SalesPrice都拿走,並且把它和Job的稅率相乘。 – Shimmy 2009-12-13 13:05:14

+0

你爲什麼試圖在tax.amount字段上運行'TOP 1'?另外,你的SQL缺少'QUOTEITEM'連接標準。 – 2009-12-13 18:02:29

回答

0

根據http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=137110

SELECT @Tax = SUM(QuoteItem.SalesPrice * Tax.Amount) 
+0

解決了你的錯誤,但它只會返回一行 - 是你想要的輸出嗎? – 2009-12-13 18:03:00

+0

是的。 @Tax應該是一個標量值,你也可以看到我只想要1稅,因爲我試圖做TOP。 – Shimmy 2009-12-13 18:12:12

0

我認爲你需要在兩個單獨的查詢中執行此操作第一個得到稅額:在設定@tax變量值的時候,你不能用「頂」條款

select @tax = Tax.Amount 
    from Tax 
    inner join ...whatever else you need here... 
    where ... 

注 - 您將需要做的事情在你的where子句中選擇你想要的值。

然後拿到銷售價格:

select @sales = sum(QuoteItem.SalesPrice 
    from ... 
    where ... 

最後返回結果:

return @tax * @sales