简体   繁体   English

Postgresql 表的多列中最大日期的 Select 值

[英]Select value by max date in multiple columns for Postgresql table

Let's say that I have a query that returns the following information:假设我有一个返回以下信息的查询:

+-----------+----------+----------+----------+----------+
| ForeignId | ScoreOne | DateOne  | ScoreTwo | DateTwo  |
+-----------+----------+----------+----------+----------+
| 1         |    1     |2021-06-20|    10    |2021-11-15|
| 1         |    2     |2021-06-21|    11    |2021-11-14|
| 1         |    3     |2021-06-22|    13    |2021-11-13|
| 1         |    4     |2021-06-23|     9    |2021-11-12|
| 1         |    3     |2021-06-24|     7    |2021-11-11|
| 1         |    1     |2021-06-25|     5    |2021-11-10|
| 1         |    2     |2021-06-26|     8    |2021-11-09|
| 2         |    5     |2021-02-11|     9    |2020-07-01|
| 2         |    8     |2021-02-12|     9    |2020-07-02|
| 2         |    9     |2021-02-13|     3    |2020-07-03|
| 2         |    7     |2021-02-14|     5    |2020-07-04|
+-----------+----------+----------+----------+----------+

In this case, I want to bring the ScoreOne value for the row with the max(DateOne), and the same for the ScoreTwo that it's in the same row as the max(DateTwo).在这种情况下,我想为具有 max(DateOne) 的行带来 ScoreOne 值,并为与 max(DateTwo) 位于同一行的 ScoreTwo 带来相同的值。 As you can see, the max(DateOne) is not in the same row as the max(DateTwo).如您所见,max(DateOne) 与 max(DateTwo) 不在同一行。 So basically the result will be this one:所以基本上结果将是这个:

+-----------+----------+----------+----------+----------+
| ForeignId | ScoreOne | DateOne  | ScoreTwo | DateTwo  |
+-----------+----------+----------+----------+----------+
| 1         |    2     |2021-06-26|    10    |2021-11-15|
| 2         |    7     |2021-02-14|    5     |2020-07-04|
+-----------+----------+----------+----------+----------+

I understand that my query will use a GROUP BY ForeignId and it also uses both max(DateOne) and max(DateTwo) , but how can I achieve this result?我知道我的查询将使用GROUP BY ForeignId并且它还同时使用max(DateOne)max(DateTwo) ,但我怎样才能达到这个结果?

Use window functions.使用 window 函数。 the_table CTE is a mimic of your actual data. the_table CTE 模拟了您的实际数据。

with the_table (foreignid,scoreone,dateone,scoretwo,datetwo) as
(
 values
  (1, 1, '2021-06-20'::date, 10, '2021-11-15'::date),    
  (1, 2, '2021-06-21', 11, '2021-11-14'),    
  (1, 3, '2021-06-22', 13, '2021-11-13'),    
  (1, 4, '2021-06-23',  9, '2021-11-12'),    
  (1, 3, '2021-06-24',  7, '2021-11-11'),    
  (1, 1, '2021-06-25',  5, '2021-11-10'),    
  (1, 2, '2021-06-26',  8, '2021-11-09'),    
  (2, 5, '2021-02-11',  9, '2020-07-01'),    
  (2, 8, '2021-02-12',  9, '2020-07-02'),    
  (2, 9, '2021-02-13',  3, '2020-07-03'),    
  (2, 7, '2021-02-14',  5, '2020-07-04')     
) 
select distinct on (foreignid)
    foreignid,
    first_value(scoreone) over win_one as "ScoreOne",
    first_value(dateone)  over win_one as "DateOne",
    first_value(scoretwo) over win_two as "ScoreTwo",
    first_value(datetwo)  over win_two as "DateTwo"
from the_table
window win_one as (partition by foreignid order by dateone desc),
       win_two as (partition by foreignid order by datetwo desc);
foreignid外国人 ScoreOne分数一 DateOne日期一号 ScoreTwo分数二 DateTwo日期二
1 1 2 2 2021-06-26 2021-06-26 10 10 2021-11-15 2021-11-15
2 2 7 7 2021-02-14 2021-02-14 5 5 2020-07-04 2020-07-04

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM