tags:

views:

63

answers:

5

Given following table:

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

How do I identify the records where Organization2 doesn't exist in Organization1 for a particular account

for instance, in the given data above my results will be a single record which will be AccountId 1 because row3 organization2 value 30 doesn't exist in organization1 for that particular account.

A: 

Use left join as Noel Abrahams presented.

Vash
@Vash I don't think that's quite what he's asking, though it provides the right result in the example. He wants to know if there are records for a given AccountId whose Organization2 value does not exist for *any* Organization1 value within that AccountId. Unless I'm mistaken, of course.
Andrew
You have totally right. I had inline those values.. and thing about them as equal or in midtime the post has been edited. Thaks.
Vash
A: 
SELECT
    *
FROM
    [YorTable]
WHERE
    [Organization1] <> [Organization2]  -- The '<>' is read "Does Not Equal".
John Gietzen
+1  A: 

Here is a how you could do it:

Test data:

CREATE TABLE #T(rowid int, acc int, org1 int, org2 int)

INSERT #T
SELECT 1,1,10,10  UNION
SELECT 2,1,20,20   UNION
SELECT 3,1,40,30   UNION
SELECT 4,2,10,10   UNION
SELECT 5,2,15,15   UNION
SELECT 6,2,20,20

Then perform a self-join to discover missing org2:

SELECT
*
FROM #T T1
LEFT JOIN
    #T T2
 ON t1.org1 = t2.org2
AND t1.acc = t2.acc

WHERE t2.org1 IS NULL
Noel Abrahams
+6  A: 
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)
Andrew
+1 `NOT EXISTS` is [the best approach in SQL Server](http://sqlinthewild.co.za/index.php/2010/03/23/left-outer-join-vs-not-exists/)
Martin Smith
Damn, I wish I had the time to read through these links. Thx ;-)
Noel Abrahams
Are you sure this isn't backwards? I believe it will return all rows where Organization1 is not in any Organization2 value for the same account. The OP wanted the opposite. It works out the same in the sample data, but if you drop row 4 from the sample, the OP would want row 5 returned but your SQL would return row 6 instead.
Larry Lustig
@Martin: That link suggests that for non-NULLable columns, NOT EXISTS and NOT IN produce identical execution plans. If the columns are NULLable, NOT IN won't work at all if there are NULL values and will work slowly if there are no NULL values in the table. Of the two, I think NOT IN is substantially clearer to read.
Larry Lustig
@Larry - Agreed on that interpretation. Maybe it would have been fairer to say "`NOT EXISTS` is the best or sometimes joint best approach in SQL Server"
Martin Smith
+3  A: 

There are two possible interpretations of your question. The first (where the Organization1 and Organization2 columns are not equal) is trivial:

SELECT AccountID FROM Table WHERE Organization1 <> Organization2

But I suspect you're asking the slightly more difficult interpretation (where Organization2 does not appear in ANY Organization1 value for the same account):

SELECT AccountID From Table T1 WHERE Organization2 NOT IN 
  (SELECT Organization1 FROM Table T2 WHERE T2.AccountID = T1.AccountID)
Larry Lustig
+1 for the non-trivial interpretation.
Joe Stefanelli