繁体   English   中英

将同一列中的值与sql server中的其他列值进行比较

[英]Compare the values in the same column and with other column values in sql server

我的表中有3列IDserial_nopriority_no

可能如下所示

--------------------
ID |  Ser_No | pri_NO
--------------------
1  |  123    | 215
1  |  123    | 280
2  |  215    | 215
3  |  123    | 360
4  |  111    | 111
-------------------

看桌子

我们有相同ser_no为ID 1和3,并且我们具有用于ID 1和2相同pri_no

为ID = 1,我们有Pri_No为215和215相同为Ser_No在ID = 2

反之亦然,一条记录的ser_no将在另一条记录的Pri_No

以上条件,我们称其为Family

现在我需要的输出是(不是家庭记录)

--------
  ID
--------
   4
--------
    select id from table a 
    where not exists 
(select * from table where (a.Ser_No = Ser_No or a.pri_NO = pri_NO) and id!=a.id)

SQL小提琴检查

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM