SQL Server - Select one random record not showing duplicates

Posted by Lukes123 on Stack Overflow See other posts from Stack Overflow or by Lukes123
Published on 2010-04-04T12:13:09Z Indexed on 2010/04/04 12:33 UTC
Read the original article Hit count: 189

Filed under:
|
|

I have two tables, events and photos, which relate together via the 'Event_ID' column. I wish to select ONE random photo from each event and display them.

How can I do this?

I have the following which displays all the photos which are associated. How can I limit it to one per event?

SELECT Photos.Photo_Id, Photos.Photo_Path, Photos.Event_Id, Events.Event_Title,
   Events.Event_StartDate, Events.Event_EndDate FROM Photos, Events 
WHERE 
   Photos.Event_Id = Events.Event_Id AND 
   Events.Event_EndDate < GETDATE() AND 
   Events.Event_EndDate IS NOT NULL AND
   Events.Event_StartDate IS NOT NULL
ORDER BY NEWID()

Thanks

Luke Stratton

© Stack Overflow or respective owner

Related posts about sql

Related posts about sql-server