2012-12-14 83 views

回答

3

我想這個子查詢

(select top 2 price, date from myTable order by date DESC) 

需要一個別名,像這樣

(select top 2 price, date from myTable order by date DESC) sq 

但我會寫這樣的:

SELECT @cost = @cost + (
SELECT price FROM (
SELECT price, date, ROW_NUMBER() OVER (ORDER BY date DESC) AS rownum 
) sq WHERE rownum = 2 
)