Given a table holding edges in a directed graph like this:
CREATE TABLE edges (
from_here int not null,
to_there int not null
)
What's the nicest way to get the number of distinct undirected links for a specific node? There aren't any duplicate directed edges nor are any nodes directly linked to themselves, I just want to avoid counting duplicate undirected edges (such as (1,2)
and (2,1)
) twice.
This works but the NOT IN
smells bad to me:
SELECT COUNT(*)
FROM edges
WHERE from_here = 1
OR (to_there = 1 AND from_here NOT IN (
SELECT to_there
FROM edges
WHERE from_here = 1
))
PostgreSQL-specific solutions are fine for this.
(1,2)
, there must exist a(2,1)
? – Louanneloucks