一些tsql的帮助很少

Rod*_*Rod 4 sql sql-server

给出下表:

rowId  AccountId  Organization1  Organization2
-----------------------------------------------
1      1          20             10
2      1          10             20
3      1          40             30
4      2          15             10
5      2          20             15
6      2          10             20
Run Code Online (Sandbox Code Playgroud)

如何识别组织1中特定帐户中不存在Organization2的记录

例如,在上面给出的数据中,我的结果将是一条记录,它将是AccountId 1,因为在该组织1中,该特定帐户中不存在row3 organization2值30.

And*_*rew 6

SELECT rowId, AccountId, Organization1, Organization2
FROM   yourTable yt
WHERE  NOT EXISTS (SELECT 1 FROM yourTable yt2 WHERE yt.AccountId = yt2.AccountId AND yt.Organization1 = yt2.Organization2)
Run Code Online (Sandbox Code Playgroud)

  • +1`NOT EXISTS`是[SQL Server中最好的方法](http://sqlinthewild.co.za/index.php/2010/03/23/left-outer-join-vs-not-exists/) (3认同)