Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I have a table called surveys with

Status
Not open for further replies.

andremc

Programmer
Aug 1, 2003
5
0
0
US
I have a table called surveys with columns (store_id, survey_date) Store_id is an int, survey_date is a datetime. They query I need to write needs 2 greatest survey dates for each store in one bitlist.

Any assistance is appreciated (using sqlServer2000).
Thank you.
 
Try this:

Code:
SELECT store_id, survey_date
FROM surveys s
WHERE survey_date IN (
  SELECT TOP 2 survey_date
  FROM surveys
  WHERE store_id = s.store_id
  ORDER BY survey_date DESC
)
ORDER BY store_id, survey_date

--James
 
JamesLean,
Thank you!! Works like a charm.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top