Link to home
Start Free TrialLog in
Avatar of bmanmike39
bmanmike39

asked on

Is there a way to ignore the first 4 records and select the second 4 records in a database?

Is there a way to ignore the first 4 records and select the second 4 records in a database.

This is what i have:
SELECT courseName, description, date from course WHERE date < dateNow
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

if you clarify the database you use, we can clarify the syntax to use.
presuming the database is ms sql server, you can do it like this:
http://technet.microsoft.com/en-us/library/ms186734.aspx
select  courseName, description, date
 from  (
SELECT courseName, description, date 
  , row_number() over ( order by date desc ) rn
 from course WHERE date < getdate() 
 ) sq
where sq.rn between 5 and 8 

Open in new window


if you wanted the 4 records per courseName, we add simply a partition by in the rownumber function:
select  courseName, description, date
 from  (
SELECT courseName, description, date 
  , row_number() over ( partition by courseName order by date desc ) rn
 from course WHERE date < getdate() 
 ) sq
where sq.rn between 5 and 8 

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Louis01
Louis01
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>if you clarify the database you use, we can clarify the syntax to use.
It would certainly be helpful if we knew which database (or databases) you need this for as the approach and/or syntax can differ.

row_number() is not available in MySQL for example.
Avatar of bmanmike39
bmanmike39

ASKER

Thanks