有没有一种方法可以将左联接查询的结果按单个列进行分组

时间:2020-09-07 15:37:51

标签: sql postgresql

我有三个这样的表 schema

个人档案表数据如下(简化版本):

                  id                  |              avatar_id               |                          bio                           | followers | following | likes |     created_at      
--------------------------------------+--------------------------------------+--------------------------------------------------------+-----------+-----------+-------+---------------------
 a2b0639f-2cc6-44b8-b97b-15d69dbb511e  | 98b6d4b8-f04b-4c79-8c2e-a0aef46854b7 | Gíkúyú ní wendo                                        |       238 |       626 |    87 | 2020-09-04 00:00:00
 72f8b983-3eb4-48db-9ed0-e45cc6bd716b  | 85f6fb09-eb05-4874-ae39-82d1a30fe0d7 | A free spirit. I stand for justice. Proud to be black. |      1025 |       970 |   738 | 2020-09-04 00:00:00
 6ba7b814-9dad-11d1-80b4-00c04fd430c8  | a235be9e-ab5d-44e6-a987-fa1c749264c7 | Cuppycake Living large                                 |      1291 |      1344 |  3070 | 2020-09-04 05:10:10
 c0f7e7f0-219a-4446-a888-134907a2ce1c  | 98b6d4b8-f04b-4c79-8c2e-a0aef46854b7 | Gíkúyú ní wendo                                        |       308 |       826 |   207 | 2020-09-07 02:15:00

头像表

                   id                  |   username   |               user_id                |     created_at      |     updated_at      
--------------------------------------+--------------+--------------------------------------+---------------------+---------------------
 6ba7b810-9dad-11d1-80b4-00c04fd430c8 | TelPv       |                                      | 2020-09-04 08:00:00 | 2020-09-04 09:00:00
 98b6d4b8-f04b-4c79-8c2e-a0aef46854b7 | DKSnt4      | 45b5fbd3-755f-4379-8f07-a58d4a30fa2f | 2020-09-04 08:00:00 | 2020-09-04 09:20:00
 85f6fb09-eb05-4874-ae39-82d1a30fe0d7 | FeliQ       | 45b5fbd3-755f-4379-8f07-a58d4a30fa2f | 2020-09-04 08:00:00 | 2020-09-04 09:20:00
 a235be9e-ab5d-44e6-a987-fa1c749264c7 | jean_blissD | 5cf37266-3473-4006-984f-9325122678b7 | 2020-09-04 08:00:00 | 2020-09-04 09:20:00

用户表

                  id                  | firstname | lastname |        email        | active |                        password_hash                         |     created_at      |     updated_at      
--------------------------------------+-----------+----------+---------------------+--------+--------------------------------------------------------------+---------------------+---------------------
 5cf37266-3473-4006-984f-9325122678b7 | test      | user1    | testuser1@gmail.com | t      | ************************************************************ | 2020-09-04 00:00:00 | 2020-09-04 00:00:00
 45b5fbd3-755f-4379-8f07-a58d4a30fa2f | test      | user2    | testuser2@gmail.com | t      | ************************************************************ | 2020-09-04 00:00:00 | 2020-09-04 00:00:00

我想获取属于给定用户的所有头像配置文件。 NB://配置文件允许单个化身的多个配置文件实体由created_at字段区分。我正在使用以下查询,但是对于某些字段却获得了多个实体。在这种情况下,我想选择一个具有最新created_at的实体。我怎样才能做到这一点?以下是我的查询和结果。

SELECT 
 a.username,     
 a.user_id,
 p.followers,
 p.following,
 p.tweets,
 p.join_date,
 p.likes,
 p.bio from avatars a LEFT JOIN
profiles p on a.id = p.avatar_id 
 WHERE a.id in (select id from avatars where user_id='45b5fbd3-755f-4379-8f07-a58d4a30fa2f') GROUP BY p.created_at,a.username,a.user_id,p.followers,p.following,p.tweets,p.join_date,p.likes,p.bio;

结果

 username  |               user_id                | followers | following | tweets |           join_date            | likes |                          bio                           
-----------+--------------------------------------+-----------+-----------+--------+--------------------------------+-------+--------------------------------------------------------
 DKSnt4    | 45b5fbd3-755f-4379-8f07-a58d4a30fa2f |       238 |       626 |    486 | Tue Jun 02 10:32:52 +0000      |    87 | Gíkúyú ní wendo
 FeliQ     | 45b5fbd3-755f-4379-8f07-a58d4a30fa2f |      1025 |       970 |    647 | Wed Jun 03 06:00:31 +0000 2020 |   738 | A free spirit. I stand for justice. Proud to be black.
 DKSnt4    | 45b5fbd3-755f-4379-8f07-a58d4a30fa2f |       308 |       826 |    686 | Tue Jun 02 10:32:52 +0000      |   207 | Gíkúyú ní wendo

我想得到这个:

 username  |               user_id                | followers | following | tweets |           join_date            | likes |                          bio                           
-----------+--------------------------------------+-----------+-----------+--------+--------------------------------+-------+--------------------------------------------------------
 DKSnt4    | 45b5fbd3-755f-4379-8f07-a58d4a30fa2f |       308 |       826 |    686 | Tue Jun 02 10:32:52 +0000      |   207 | Gíkúyú ní wendo
 FeliQ     | 45b5fbd3-755f-4379-8f07-a58d4a30fa2f |      1025 |       970 |    647 | Wed Jun 03 06:00:31 +0000 2020 |   738 | A free spirit. I stand for justice. Proud to be black.

1 个答案:

答案 0 :(得分:0)

也许是这样的:


with allp as
(
    SELECT 
     a.username,     
     a.user_id,
     p.followers,
     p.following,
     p.tweets,
     p.join_date,
     p.likes,
     p.bio,
     row_number() over
     (
            partition by
                a.user_id,
                a.username
            order by
                p.created_at desc,
                p.id desc
     ) as priority_number
    from 
     avatars a 
    LEFT JOIN
     profiles p 
    on 
     a.id = p.avatar_id 
    WHERE 
     a.user_id='45b5fbd3-755f-4379-8f07-a58d4a30fa2f' 
)

select
    allp.username,     
    allp.user_id,
    allp.followers,
    allp.following,
    allp.tweets,
    allp.join_date,
    allp.likes,
    allp.bio
from
    allp
where
    priority_number = 1