2011-10-10 74 views
2

我有以下表包含以下數據:Group按查詢 - 任何其他方式?

http://img513.imageshack.us/img513/9039/mycities.png

CREATE語句和刀片是在http://snipt.org/xoKl

該表是城市列表,每個城市屬於一個地區和一個國家,每個城市都有創建日期。這裏的目標是爲每個「國家/地區」對獲得最古老的城市列表。我們需要在加拿大,在美國西海岸最古老的城市等東海岸最古老的城市...

,我現在使用的查詢:

SELECT * FROM MyCities 
INNER JOIN 
    (SELECT Country, Region, MIN(FoundingDate) AS CityFoundingDate 
     FROM MyCities 
    GROUP BY Country, Region) AS subquery  
     ON subquery.CityFoundingDate = MyCities.FoundingDate 
     AND MyCities.Country = subquery.Country 
     AND MyCities.Region = subquery.Region 

我只是想知道是否有其他方式通過查詢或不查詢該組。 :-)
此查詢是否有效?
期待討論。

回答

3

那麼呢?

select country, region, city from MyCities mc1 
where foundingDate <= ALL (
    select foundingDate from MyCities as mc2 
    where mc1.country = mc2.country and mc1.region = mc2.region 
) 
1

這樣的事情呢? 應該在甲骨文工作(雖然我現在不能測試)

SELECT country, region, city, foundingdate 
FROM (
SELECT country, region, city, foundingdate, MIN(founding_date) OVER PARTITION BY (country, region) min_date 
FROM mycities) WHERE foundingdate=min_date 

但是,如果在同一個國家/地區成立於同年兩市?