-1

Possible Duplicate:
SQL Query to Select First/Top N records

Here is my code so far:

SELECT WeekNumber, SUM(Hours)
FROM Information
WHERE YEAR = 2011
GROUP BY WeekNumber
ORDER BY WeekNumber DESC

However; I am trying to select either the information for the last week, or the last month. Is there a way to select just the first row, or the first x rows, where those rows contain the hours for that month?

Community
  • 1
  • 1
  • http://stackoverflow.com/questions/7680672/sql-query-to-select-first-top-n-records/7680733#7680733 is the answer for this question in multiple RDBMS – Adriano Carneiro Oct 12 '11 at 15:29

2 Answers2

3

Assuming you're using SQL Server, use the TOP clause (limit is only for MySQL); e.g.:

SELECT TOP 10 WeekNumber, SUM(Hours)
FROM Information 
WHERE YEAR = 2011
GROUP BY WeekNumber
ORDER BY WeekNumber DESC
ChrisW
  • 4,970
  • 7
  • 55
  • 92
0

Using Microsoft SQL Server 2008 (as specified in your tags), you need to use the TOP keyword.

SELECT TOP 10 WeekNumber, SUM(Hours)
FROM Information
WHERE YEAR = 2011
GROUP BY WeekNumber
ORDER BY WeekNumber DESC

The LIMIT keyword is for MySQL, not for SQL Server

You may also use SET ROWCOUNT 10 at the very start of your query, which does the same thing but will last for all subsequent queries in the connection.

Connell
  • 13,925
  • 11
  • 59
  • 92