Thursday, March 22, 2012

a complex query

I have the following SQL 2000 database table:
NEWS (IDNews, Country, PublishDate, Title)

I have to get a dataset containing only one record for each country, having most recent publish date.
Any suggestions? Thanks.SELECT TOP 1 IDNews, Country, PublishDate, Title From News Group By Country Order By PublishDate DESC?|||Try this:

select IDNews, Country, PublishDate, Title
from NEWS
where IDNews in
(
select
max(IDnews)
from news n
join
(select
country, max(publishdate) as publishdate
from news
group by country) md
on n.country = md.country
and n.publishdate = md.publishdate
group by n.country
)|||


select
news.*
from
news
inner join
(select
country, max(publishdate) as publishdate
from
news
group by
country
) as TMP
on news.country = TMP.country
and news.publishdate = TMP.publishdate
sql

No comments:

Post a Comment