Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

share|improve this question
    
Which database are you using? Relevant because some of them allow the use of a LIMIT keyword to restrict your resultset. –  Satanicpuppy Dec 4 '09 at 22:25
    
SQL Database, Microsoft SQL Server 2003 –  Michael Erickson Dec 5 '09 at 2:08

1 Answer 1

Mysql:

SELECT id FROM table ORDER BY id DESC LIMIT 50

Mssql:

SELECT TOP 50 id FROM table ORDER BY id DESC
share|improve this answer
    
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 Dec 5 '09 at 2:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.