最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

postgresql - How can I filter for ids with more than one corresponding unique value in a certain column? - Stack Overflow

programmeradmin0浏览0评论

I have the following postgres SQL table with three columns;

    id      linktype      to_id    
  322754       4            1
  322754       4            1
  322754       4            2
  322798       4            2
  322798       4            2
  322797       4            3
  322791       4            6
  322790       4            3

What I am trying to do is filter for to_id values where there are more than one corresponding id value that is different.

So from the above I would simply want to_id value 3 returned, since it is the only to_id to have more than one DISTINCT id corresponding to it.

I have the following postgres SQL table with three columns;

    id      linktype      to_id    
  322754       4            1
  322754       4            1
  322754       4            2
  322798       4            2
  322798       4            2
  322797       4            3
  322791       4            6
  322790       4            3

What I am trying to do is filter for to_id values where there are more than one corresponding id value that is different.

So from the above I would simply want to_id value 3 returned, since it is the only to_id to have more than one DISTINCT id corresponding to it.

Share Improve this question asked Feb 4 at 12:13 Patrick ChongPatrick Chong 635 bronze badges 1
  • It looks like to_id=2 should also qualify, because it's listed once with id 322754 and twice with 322798. – Zegarek Commented Feb 4 at 13:16
Add a comment  | 

1 Answer 1

Reset to default 2

Use a having clause
demo at db<>fiddle

select to_id
from test
group by to_id
having count(distinct id)>1;
to_id
2
3

To make that faster, use exists()

select distinct to_id
from test a
where exists(
  select from test b
  where a.id<>b.id
    and a.to_id=b.to_id);

Or a self-join:

select distinct a.to_id
from test a
join test b 
  on a.id<>b.id
  and a.to_id=b.to_id;

There are other methods using except/intersect, an in/not in, but exists() is usually quicker.

Don't fet about the index:

create index on test (to_id,id);

It speeds things up for all 3 methods above, significantly. Also note that the optimal method and index depends on the characteristics of your data set: on 100k records where only 807 to_id's match your criteria, the last two examples win: demo1. If out of that 100k records I make 28k match that, the first example becomes faster: demo2.

Make sure to run your own benchmarks and adjust your approach.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论