Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 sql query help !!!

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2005-01-05 : 08:14:02
mo writes "hi all,

got an easy oen here...

i got 2 tables in sql srvr2000. one contains galleries and the other projections.

i want to return all galleries that match a nile like '%gomez%' but only if the gallery_id is not 'trashed' in the projected table.

here is the curver ball. the projected table can have no records or multiple records for any given gallery. if it has no record that is fine but if it has many i need to find the most recent and make sure its gallery_type is not "trashed". if there are multiple records for a gallery then the most curent is the one i need to check for if it is trashed. there is a date field for this.

the tables are linked by galelry_id

thank you all."

nr
SQLTeam MVY

12543 Posts

Posted - 2005-01-05 : 08:36:04
from galleries g
left join (select gallery_id , dte = max(dte) from projected group by gallery_id) pmax
on g.gallery_id = pmax.gallery_id
left join projected p
on p.gallery_id = pmax.gallery_id
and p.dte = pmax.dte
where g.nile like '%gomez%'
and (p.trashed = 1 or p.trashed is null)

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page
   

- Advertisement -