0

我有这个查询:

select @cost = @cost +                  
    (select top 1 price from
    (select top 2 price, date  from myTable order by date DESC)
     order by date ASC
)

我得到错误:关键字“订单”附近的语法不正确。如何解决这个问题?

4

1 回答 1

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
)
于 2012-12-14T09:20:12.393 回答