0

I am looking to write a query where it shows me the last 50 of the highest ids, without knowing the most recent id #. The id is an autogenerated primary key. Any help would be appreciated.

Maybe I should be more specific. I work for a scheduling company that is always recruiting new employees to schedule. I have a table containing scheduled appts, and I have table with potential employees, some of which have been scheduled, and others just recruited. I want to query for employees that have done work w/in last year, and employees that have not done work within last year but have been added to the employee table. I alo have employees that exist that have worked but more than a year ago. I am using a left join to join two tables. Using Scheduled Between DateAdd("y", -1, Date()) and Date() to get date range. The only Date / Time field I have in the employee table, is a field called updated (unfortunately this changes with any update and doesn't necessarily tell me if a record has just been created or updated. My query thus far:

Select Distinct i.FirstName + ' ' + i.LastName as Interpreter, il.Language, i.City, Format(i.HomePhone, "(###)###-####") as Home, Format(i.CellPhone, "(###)###-####") as Cell, i.Email From (Interpreter as I inner join InterpLanguage as Il On i.InterpID = il.InterpID) Left Join Dispatch as d on i.InterpID = d.InterpId Where i.Active = True and il.Interp = True and i.Comment Not Like "do not use" and i.FirstName + ' ' + i.LastName Not In ("Language Line", "Cancel Interpreter", "Cancel Appointment", "LLE Interpreter") and il.Language Not In ("All Languages", "Bay Area Translations") and i.FirstName is Not Null and i.LastName Is Not Null and d.scheduled Between DateAdd("m", -12, Date()) and Date() or i.Active = True and il.Interp = True and i.Comment Not Like "do not use" and i.FirstName + ' ' + i.LastName Not In ("Language Line", "Cancel Interpreter", "Cancel Appointment", "LLE Interpreter") and il.Language Not In ("All Languages", "Bay Area Translations") and i.FirstName is Not Null and i.LastName Is Not Null and Updated Between DateAdd("m", -6, Date()) And Date() Order By i.City ASC

2
  • Which database are you using? Relevant because some of them allow the use of a LIMIT keyword to restrict your resultset. Commented Dec 4, 2009 at 22:25
  • SQL Database, Microsoft SQL Server 2003
    – Michael Erickson
    Commented Dec 5, 2009 at 2:08

1 Answer 1

1

Mysql:

SELECT id FROM table ORDER BY id DESC LIMIT 50

Mssql:

SELECT TOP 50 id FROM table ORDER BY id DESC
1
  • How would that work for using in the WHERE. Maybe I should be more specific. I work for a scheduling company that is always recruiting new employees to schedule. I have a table containing scheduled appts, and I have table with potential employees. I want to query for employees that have done work w/in last year, and employees that have not done work within last year but have been added to the employee table. I have a left join using to join two tables. Using Scheduled Between DateAdd("y", -1, Date()) and Date() to get date range.
    – Michael Erickson
    Commented Dec 5, 2009 at 2:07

You must log in to answer this question.