3

假设我们在 PostgreSQL 中有两个表:

表“公民”

country_ref   citizen_name    entry_date
-----------------------------------------------------
0             peter           2013-01-14 21:00:00.000
1             fernando        2013-01-14 20:00:00.000
0             robert          2013-01-14 19:00:00.000
3             albert          2013-01-14 18:00:00.000
2             esther          2013-01-14 17:00:00.000
1             juan            2013-01-14 16:00:00.000
3             egbert          2013-01-14 15:00:00.000
1             francisco       2013-01-14 14:00:00.000
3             adolph          2013-01-14 13:00:00.000
2             emilie          2013-01-14 12:00:00.000
2             jacques         2013-01-14 11:00:00.000
0             david           2013-01-14 10:00:00.000

表“国家”

country_id     country_name   country_group
-------------------------------------------
0              england        0
1              spain          0 
2              france         1
3              germany        1

现在我想获取给定国家组的每个国家在“公民”表中最后输入的公民。

到目前为止,我最好的尝试是这个查询(我们称之为 Query_1):

SELECT country_ref, MAX(entry_date) FROM citizens 
LEFT JOIN countries ON country_id = country_ref 
WHERE country_group = 1 GROUP BY country_ref

输出:

country_ref   max
---------------------------------
3             2013-01-14 18:00:00
2             2013-01-14 17:00:00

那么我可以这样做:

SELECT citizen_name FROM citizens WHERE (country_ref, entry_date) IN (Query_1)

...这将为我提供我正在寻找的输出:albertesther.

但我更愿意在单个查询中实现这一点。我想知道这是否可能?

4

3 回答 3

7

这应该是最简单和最快的:

SELECT DISTINCT ON (i.country_ref)
       i.citizen_name
FROM   citizens  i
JOIN   countries o ON o.country_id = i.country_ref
WHERE  o.country_group = 1
ORDER  BY i.country_ref, i.entry_date DESC

您可以轻松地从两个表中返回更多列,只需将它们添加到SELECT列表中即可。
SQL小提琴。

此相关答案中的详细信息、链接和说明:

于 2013-02-11T06:25:08.823 回答
3
SELECT citizen_name, 
       country_ref, 
       entry_date
from (
  SELECT cit.citizen_name, 
         cit.country_ref, 
         MAX(cit.entry_date) over (partition by cit.country_ref) as max_date,
         cit.entry_date
  FROM citizens cit
    LEFT JOIN countries cou ON cou.country_id = cit.country_ref 
  WHERE cou.country_group = 1 
) t
where max_date = entry_date

SQLFiddle 演示:http ://www.sqlfiddle.com/#!12/50776/1

于 2013-02-10T23:32:47.723 回答
1

你为什么不简单地:

SELECT citizen_name FROM citizens WHERE (country_ref, entry_date) IN (
    SELECT country_ref, MAX(entry_date) FROM citizens 
    LEFT JOIN countries ON country_id = country_ref 
    WHERE country_group = 1 GROUP BY country_ref
)

这可能不是最好的计划,但它取决于很多因素,而且写起来很简单。

于 2013-02-10T23:10:15.823 回答