这是我的
message
桌子。现在我正在尝试编写一个查询,按 collection_id
(仅第一个实例)对记录进行分组 if type
multi-store
else by message
.id
.
id | collection_id | type | affiliation_id | status | scheduled_for_date
--------------------------------------+--------------------------------------+----------------+----------------+--------------+--------------------
1143c066-01ed-4eb5-a146-68487de702a9 | bce85e31-4d2f-43b1-b263-57fca356856f | multi-store | 12091 | draft |
e1183732-e91d-42eb-9998-110e039cfc25 | bce85e31-4d2f-43b1-b263-57fca356856f | multi-store | 12092 | draft |
8f962a49-7da1-46d0-87b9-595788767dfe | bce85e31-4d2f-43b1-b263-57fca356856f | multi-store | 12097 | draft |
6e4dee09-7a4e-47be-bdd8-935a67bb2063 | 740f6b42-bbf1-4aeb-8fe9-6874635d9e29 | multi-store | 12091 | draft |
79afab0e-14e7-4d1b-9a15-358763743c3e | 740f6b42-bbf1-4aeb-8fe9-6874635d9e29 | multi-store | 12092 | draft |
7bc78bee-074a-4031-9492-954e7c4eeb09 | 740f6b42-bbf1-4aeb-8fe9-6874635d9e29 | multi-store | 12097 | draft |
3bb38fbd-d411-4f78-9c42-c858bf57b784 | | standard-store | 10511 | draft |
fbb3b175-1a3b-4515-b0b3-0ce6d6d0145f | | standard-store | 10511 | draft |
84004999-d2cf-4af4-bfaa-c1077d1d8621 | | standard-store | 10511 | sent | 2017-05-21
cbea0789-6886-431a-a8e1-723d5aafc7b9 | | standard-store | 10511 | scheduled | 2019-02-12
ec8988ff-5136-4b81-b448-cd456dc487a4 | | standard-store | 10511 | review | 2019-01-13
0e119440-5fbc-4afe-a784-a6bcfe3a6e4d | | standard-store | 10511 | draft |
98503a20-4396-4809-b3ec-8e330c15afa9 | | standard-store | 10511 | needs_action | 2018-12-11
d33a9173-dc64-464f-8e58-49b4c9c2fdae | | standard-store | 10511 | draft |
bee0dc72-acca-44e2-82ea-d18e830f91a2 | | standard-store | 10511 | sent | 2016-03-12
所以输出会是这样的
id | collection_id | type | affiliation_id | status | scheduled_for_date
--------------------------------------+--------------------------------------+----------------+----------------+--------------+--------------------
1143c066-01ed-4eb5-a146-68487de702a9 | bce85e31-4d2f-43b1-b263-57fca356856f | multi-store | 12091 | draft |
6e4dee09-7a4e-47be-bdd8-935a67bb2063 | 740f6b42-bbf1-4aeb-8fe9-6874635d9e29 | multi-store | 12091 | draft |
3bb38fbd-d411-4f78-9c42-c858bf57b784 | | standard-store | 10511 | draft |
fbb3b175-1a3b-4515-b0b3-0ce6d6d0145f | | standard-store | 10511 | draft |
84004999-d2cf-4af4-bfaa-c1077d1d8621 | | standard-store | 10511 | sent | 2017-05-21
cbea0789-6886-431a-a8e1-723d5aafc7b9 | | standard-store | 10511 | scheduled | 2019-02-12
ec8988ff-5136-4b81-b448-cd456dc487a4 | | standard-store | 10511 | review | 2019-01-13
0e119440-5fbc-4afe-a784-a6bcfe3a6e4d | | standard-store | 10511 | draft |
98503a20-4396-4809-b3ec-8e330c15afa9 | | standard-store | 10511 | needs_action | 2018-12-11
d33a9173-dc64-464f-8e58-49b4c9c2fdae | | standard-store | 10511 | draft |
bee0dc72-acca-44e2-82ea-d18e830f91a2 | | standard-store | 10511 | sent | 2016-03-12
一种方法是通过
union
:
SELECT DISTINCT ON (collection_id)
collection_id,
id,
type,
affiliation_id,
status,
scheduled_for_date
from
message
where
type = 'multi-store'
union
SELECT
collection_id,
id,
type,
affiliation_id,
status,
scheduled_for_date
from
message
where
type = 'standard-store'
但我觉得这样效率较低,另一种选择可能是基于案例的分组。但这包括需要将所有选择字段添加到组时的复杂性。
编写查询最有效的方法是什么?
您可以将
distinct on
子句与 case
表达式一起使用。
select distinct on (case when type='multi-store' then collection_id else id end)
id, collection_id, type,
affiliation_id, status, scheduled_for_date
from the_table;
DB-Fiddle 演示