0

I have a query

  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

Which when I add the LIMIT it gives me a error "Incorrect syntax near '20'.". Is there another way to get the top 20?

I have Microsoft SQL Server Management Studio 10.0.2531.0, SQL server 2008.

user3591637
  • 499
  • 5
  • 20

2 Answers2

1

you are probably looking for 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]
iruvar
  • 22,736
  • 7
  • 53
  • 82
0

If you read the documentation for select, you'll see that it uses the keyword top, whose documentation says that it

Limits the rows returned in a query result set to a specified number of rows or percentage of rows in SQL Server 2012. When TOP is used in conjunction with the ORDER BY clause, the result set is limited to the first N number of ordered rows; otherwise, it returns the first N number of rows in an undefined order. Use this clause to specify the number of rows returned from a SELECT statement or affected by an INSERT, UPDATE, MERGE, or DELETE statement.

So you can get what you want by saying something like

select top 20
       *
from foo
Nicholas Carey
  • 71,308
  • 16
  • 93
  • 135