2
4

1 回答 1

1

以下查询应该为您提供所需的内容。它有点长,但它可以解决问题:

`select LIST.Civil_Parish, SD.Site_name, LIST.MSite_Date
from
(
select Civil_Parish, min(Site_date) as MSite_date
from SiteDetail
where Boolean = 1
group by Civil_Parish

union

select Civil_parish, min(Site_date) as MSite_date
from SiteDetail
where Civil_parish not in
(select Civil_parish
from SiteDetail
where Boolean = 1)
group by Civil_Parish) as LIST 
left join sitedetail SD on LIST.Civil_Parish = SD.Civil_Parish and LIST.MSite_Date = SD.Site_Date`

请注意以下事项:

1)我正在使用 PowerUser 的布尔建议。如果布尔列的值为 1,则该行是教区教堂,否则为 0。

2) 为了本示例的目的,我将表“SiteDates”和“SiteDetails”组合在一起,因为它们是 1 比 1。

查询的核心是 A) 在教区中查找最古老的教区教堂,然后 B) 查找没有教区教堂的教区。

A)的代码如下:

'select Civil_Parish, min(Site_date) as MSite_date
from SiteDetail
where Boolean = 1
group by Civil_Parish'

然后,我们将其与没有教区教堂的教区中最古老的教堂联合起来:

'select Civil_parish, min(Site_date) as MSite_date
from SiteDetail
where Civil_parish not in
(select Civil_parish
from SiteDetail
where Boolean = 1)
group by Civil_Parish'

然后,我们将联合查询(此处名为“LIST”)与我们在教区和日期上的原始“SITEDETAIL”表连接起来,以引入教堂名称。

于 2013-07-10T20:37:38.753 回答