在SQL中的有向图中计算不同的无向边

给定一个表,在这样的有向图中保持边:
CREATE TABLE edges ( 
    from_here int not null,to_there  int not null
)

获取特定节点的不同无向链接数量的最佳方法是什么?没有任何重复的定向边缘,也没有任何节点直接链接到自己,我只想避免计数重复的无向边(如(1,2)和(2,1))两次.

这个工作,但NOT IN闻到对我不好:

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特定的解决方案对此很好.

解决方法

select count(*) from (
  select to_there from edges where from_here = 1
  union
  select from_here from edges where to_there = 1
) as whatever

相关文章

SELECT a.*,b.dp_name,c.pa_name,fm_name=(CASE WHEN a.fm_n...
if not exists(select name from syscolumns where name=&am...
select a.*,pano=a.pa_no,b.pa_name,f.dp_name,e.fw_state_n...
要在 SQL Server 2019 中设置定时自动重启,可以使用 Window...
您收到的错误消息表明数据库 'EastRiver' 的...
首先我需要查询出需要使用SQL Server Profiler跟踪的数据库标...