PostgreSQL: Returning the most recent rows grouping by a foreign key column

后端 未结 2 1368
刺人心
刺人心 2021-01-23 10:54

I have a table that looks similar to this:

credit
+---------+----------------+-------------+--------------+-------------------------+
| id (PK) | person_id (FK)          


        
相关标签:
2条回答
  • 2021-01-23 11:14

    Use distinct on:

    select distinct on (person_id) t.*
    from t
    order by person_id, date_time desc
    
    0 讨论(0)
  • 2021-01-23 11:35

    You can try this. use ROW_NUMBER with window function to make row number split by person_id and order by date_time in subquery then get row number is 1

    CREATE TABLE credit(
       id int,
       person_id int,
       transaction float,
       "total credit" float,
       date_time timestamp
    );
    
    INSERT INTO credit values (345,1 ,-1.00,34.50, '2018-08-29 12:00:00.000');
    INSERT INTO credit values (897,1 ,5.45 ,39.95, '2018-08-29 12:34:00.000');
    INSERT INTO credit values (378,2 ,0.01 ,0.01 , '2018-08-29 08:00:00.000');
    INSERT INTO credit values (789,2 ,20.00,20.01, '2018-08-29 09:00:00.000');
    

    Query 1:

    SELECT * FROM (
        SELECT *,ROW_NUMBER() OVER (PARTITION BY person_id  ORDER BY date_time DESC) rn
        FROM credit
    ) t1
    where rn = 1
    

    Results:

    |  id | person_id | transaction | total credit |            date_time | rn |
    |-----|-----------|-------------|--------------|----------------------|----|
    | 897 |         1 |        5.45 |        39.95 | 2018-08-29T12:34:00Z |  1 |
    | 789 |         2 |          20 |        20.01 | 2018-08-29T09:00:00Z |  1 |
    
    0 讨论(0)
提交回复
热议问题