【问题标题】:Is LIMIT not allow in sql server 2008? [duplicate]LIMIT 在 sql server 2008 中不允许吗? [复制]
【发布时间】:2014-06-11 07:13:41
【问题描述】:

我有一个问题

  SELECT employeedept, execoffice_status, employee, COUNT(*) AS 'employeetotal', YEAR_cse1 =YEAR(execoffice_date)
FROM CSEReduxResponses
WHERE execoffice_status = 1
GROUP BY employeedept, execoffice_status, YEAR(execoffice_date), employee 
order by [YEAR_cse1],
LIMIT 20

当我添加 LIMIT 时,它会给我一个错误“'20' 附近的语法不正确。”。 还有其他方法可以进入前 20 名吗?

我有 Microsoft SQL Server Management Studio 10.0.2531.0,SQL Server 2008。

【问题讨论】:

  • 也许我会得到我的第一个标志,但这需要大约 5 秒才能通过搜索“sql server limit”找到答案
  • 不要得到一个标志...给一个“重复”

标签: sql sql-server-2008


【解决方案1】:

您可能正在寻找top

SELECT TOP 20 employeedept, execoffice_status, employee,
COUNT(*) AS 'employeetotal',YEAR_cse1 =YEAR(execoffice_date)
FROM CSEReduxResponses
WHERE execoffice_status = 1
GROUP BY employeedept, execoffice_status, YEAR(execoffice_date), employee 
order by [YEAR_cse1]

【讨论】:

    【解决方案2】:

    如果你read the documentation for select,你会看到它使用关键字topwhose documentation 表示它

    将查询结果集中返回的行限制为指定的行数或 SQL Server 2012 中的行百分比。当 TOP 与 ORDER BY 结合使用时 子句,结果集仅限于前N个有序行;否则,它 以未定义的顺序返回前 N 行。使用此子句指定 从 SELECT 语句返回或受 INSERT、UPDATE、MERGE、 或 DELETE 语句。

    所以你可以通过说类似的话得到你想要的东西

    select top 20
           *
    from foo
    

    【讨论】:

      猜你喜欢
      • 2013-04-20
      • 1970-01-01
      • 1970-01-01
      • 2010-12-10
      • 2011-02-22
      • 1970-01-01
      • 2012-01-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多