选择按不同顺序排序的 DISTINCT 行
selecting DISTINCT rows that are ordered by something different
我有这个tableperformances
,那里有其他table的戏剧表演:
id | play_id | when | other uninteresting meta data
1 | 3 | 2020-04-01 | ...
2 | 4 | 2020-03-03 |
3 | 3 | 2020-01-02 |
4 | 1 | 2020-06-03 |
5 | 4 | 2020-10-13 |
我想select每个不同戏剧的最早表演(所以每个戏剧都由一个最早的表演代表),从早到晚排序。
所以根据提供的数据我想得到这个:
id | play_id | when | other uninteresting meta data
3 | 3 | 2020-01-02 | ...
2 | 4 | 2020-03-03 |
4 | 1 | 2020-06-03 |
到目前为止,在研究了一些答案后 here and here 我想到了这个查询
SELECT * FROM
(
SELECT DISTINCT ON (play_id) *
FROM performances
WHERE performances.deleted_at is null
ORDER BY performances.play_id ASC, performances.when ASC
) distinct_plays
order by distinct_plays.when ASC
然而,我一点都不相信,因为在链接的线程中有人争吵并告诉对方的答案是错误的;虽然我看到该线程中的答案存在一些问题,但我还没有看到该答案中的问题。
这个查询适合我的任务吗?它不是 select 重复行还是非常无效?
你可以使用 first_value
:
select first_value(id) over(w), play_id, first_value(when) over(w) -- the rest of the columns analogously
from performances
group by play_id
window w as (partition by play_id order by when)
您的查询符合您的要求。 distinct on
通常是 Postgres 中解决此类 greatest-n-per-group 问题的正确工具...唉,它在结果集中的行顺序上没有灵活性。
您似乎希望结果中的排序与 distinct on
中的排序不同 - 因此您需要为此进行另一层嵌套。虽然您的代码可以满足您的要求,但我建议改用 row_number()
(与 vendor-specific distinct on
相比,它还具有在许多数据库中受支持的优势):
SELECT *
FROM (
SELECT p.*, ROW_NUMBER() OVER(PARTITION BY play_id ORDER BY p.when asc) rn
FROM performances p
WHERE p.deleted_at is null
) p
WHERE rn = 1
ORDER BY p.when asc
您可能还想尝试相关子查询:
SELECT p.*
FROM performances p
WHERE p.deleted_at IS NULL AND p.when = (
SELECT MIN(p1.when) FROM performances p1 WHERE p1.play_id = p.play_id
)
ORDER BY p.when
为了提高相关子查询的性能,请考虑 (play_id, when)
.
上的索引
我有这个tableperformances
,那里有其他table的戏剧表演:
id | play_id | when | other uninteresting meta data
1 | 3 | 2020-04-01 | ...
2 | 4 | 2020-03-03 |
3 | 3 | 2020-01-02 |
4 | 1 | 2020-06-03 |
5 | 4 | 2020-10-13 |
我想select每个不同戏剧的最早表演(所以每个戏剧都由一个最早的表演代表),从早到晚排序。
所以根据提供的数据我想得到这个:
id | play_id | when | other uninteresting meta data
3 | 3 | 2020-01-02 | ...
2 | 4 | 2020-03-03 |
4 | 1 | 2020-06-03 |
到目前为止,在研究了一些答案后 here and here 我想到了这个查询
SELECT * FROM
(
SELECT DISTINCT ON (play_id) *
FROM performances
WHERE performances.deleted_at is null
ORDER BY performances.play_id ASC, performances.when ASC
) distinct_plays
order by distinct_plays.when ASC
然而,我一点都不相信,因为在链接的线程中有人争吵并告诉对方的答案是错误的;虽然我看到该线程中的答案存在一些问题,但我还没有看到该答案中的问题。
这个查询适合我的任务吗?它不是 select 重复行还是非常无效?
你可以使用 first_value
:
select first_value(id) over(w), play_id, first_value(when) over(w) -- the rest of the columns analogously
from performances
group by play_id
window w as (partition by play_id order by when)
您的查询符合您的要求。 distinct on
通常是 Postgres 中解决此类 greatest-n-per-group 问题的正确工具...唉,它在结果集中的行顺序上没有灵活性。
您似乎希望结果中的排序与 distinct on
中的排序不同 - 因此您需要为此进行另一层嵌套。虽然您的代码可以满足您的要求,但我建议改用 row_number()
(与 vendor-specific distinct on
相比,它还具有在许多数据库中受支持的优势):
SELECT *
FROM (
SELECT p.*, ROW_NUMBER() OVER(PARTITION BY play_id ORDER BY p.when asc) rn
FROM performances p
WHERE p.deleted_at is null
) p
WHERE rn = 1
ORDER BY p.when asc
您可能还想尝试相关子查询:
SELECT p.*
FROM performances p
WHERE p.deleted_at IS NULL AND p.when = (
SELECT MIN(p1.when) FROM performances p1 WHERE p1.play_id = p.play_id
)
ORDER BY p.when
为了提高相关子查询的性能,请考虑 (play_id, when)
.