我对postgresql中的数据分组有问题。假设我有一个名为my_table的表
some_id | description | other_id
---------|-----------------|-----------
1 | description-1 | a
1 | description-2 | b
2 | description-3 | a
2 | description-4 | a
3 | description-5 | a
3 | description-6 | b
3 | description-7 | b
4 | description-8 | a
4 | description-9 | a
4 | description-10 | a
...我想根据some_id对我的数据库进行分组,然后区分哪一个具有相同的和不同的other_id
我希望有两种类型的查询:一个有相同的other_id,一个有不同的other_id。
预期结果
some_id | description | other_id
---------|-----------------|-----------
2 | description-3 | a
2 | description-4 | a
4 | description-8 | a
4 | description-9 | a
4 | description-10 | a和
some_id | description | other_id
---------|-----------------|-----------
1 | description-1 | a
1 | description-2 | b
3 | description-5 | a
3 | description-6 | b
3 | description-7 | b我愿意接受建议,无论是使用续集还是原始查询。
谢谢
发布于 2021-06-02 08:34:35
一种方法是使用MIN和MAX作为分析函数:
WITH cte AS (
SELECT *, MIN(other_id) OVER (PARTITION BY some_id) min_other_id,
MAX(other_id) OVER (PARTITION BY some_id) max_other_id
FROM yourTable
)
-- all some_id the same
SELECT some_id, description, other_id
FROM cte
WHERE min_other_id = max_other_id;
-- not all some_id the same
SELECT some_id, description, other_id
FROM cte
WHERE min_other_id <> max_other_id;
发布于 2021-06-02 11:00:19
您也可以使用exists和not exists来完成这一任务。
-- all same
select t.*
from my_table t
where not exists (select 1
from my_table t2
where t2.some_id = t.some_id and t2.other_id <> t.other_id
);
-- any different
select t.*
from my_table t
where exists (select 1
from my_table t2
where t2.some_id = t.some_id and t2.other_id <> t.other_id
);请注意,这会忽略NULL值。如果希望将它们视为“不同”值,则使用is distinct from而不是<>。
https://stackoverflow.com/questions/67801536
复制相似问题