使用where时SELECT语句错误

时间:2018-03-15 12:06:18

标签: sql sql-server tsql sql-server-2012

SELECT EmployeeName, Date
ORDER BY Date desc 
where Lastname = @lastname
OFFSET 1 Rows 
FETCH NEXT 1 ROW ONLY;

如果我在上述查询中未包含where,那么它完全有效,但如何在一个语句中包含ORDER BYWHEREOFFSETFETCH,我想根据日期和姓氏选择第n行

1 个答案:

答案 0 :(得分:1)

where子句应该在order by子句之前 此外,您缺少from子句。

SELECT EmployeeName, Date
FROM <YourTableNameHere>
WHERE Lastname = @lastname
ORDER BY Date desc 
OFFSET 1 Rows 
FETCH NEXT 1 ROW ONLY;

(用您的实际表名替换<YourTableNameHere>