7

Before someone goes on a rant that this table should be normalized, best practices, etc. I am going to admit that this is an old table we have in SQL Server 2008 R2 and I can't do anything about changing it. Having said that, this table has the following columns:

"PreparedBy", "PrelimApprovalBy", "Approval1Signer", "Approval2Signer" 

All these fields have either usernames or NULL or ''. I want to get all the rows where the same username appears in 2 OR MORE of the fields mentioned above. If 2 fields are NULL they are NOT a match and they are NOT a match if they are both ''. So both NULL and '' need to be excluded as they don't signify anything.


HERE'S WHAT I THOUGHT OF SO FAR BUT AM NOT LIKING IT:
I am thinking of checking all permutations in the WHERE clause (checking for NULL and '') by doing something along the lines of

WHERE PreparedBy =  PrelimApprovalBy OR PreparedBy = Approval1Signer OR ...

There has got to be a better way to do it.

0

2 Answers 2

7

Here's one:

SELECT * FROM T
WHERE EXISTS 
     (SELECT 1 
      FROM (VALUES 
                   (PreparedBy)
                  ,(PrelimApprovalBy)
                  ,(Approval1Signer)
                  ,(Approval2Signer)) AS X (n)
      WHERE NULLIF(n, '') IS NOT NULL
      GROUP BY n
      HAVING COUNT(*)>1
     )

Basically, for each row, we're constructing a mini-table with the column values in different rows, and doing a GROUP BY and HAVING to check for groups of matching values. The NULLIF is helping us ignore '' values (making them NULL and then excluding all NULLs).

6
  • This is awesome! It works! Never saw this syntax before... FROM (VALUES ... AS X(n)). I learned something new today!
    – Denis
    Jul 9, 2013 at 18:23
  • Glad to help, and to present new syntax. More tools in the tool belt can't hurt. Also, this solution can easily handle more columns, while the other method would explode in size and complexity very soon.
    – GilM
    Jul 9, 2013 at 18:26
  • I think the Values syntax (table value constructor) was introduced in SQL Server 2008. But, you could do the same thing earlier using (SELECT ... UNION ALL SELECT ... UNION ALL...).
    – GilM
    Jul 9, 2013 at 18:31
  • I see it working but I don't really understand WHY it is working. Can you give a small explanation in your answer of how does it know to do 2 equivalences or more? i.e. what is it really doing... I am going to read up on it cause I am at a loss of what is happening here...
    – Denis
    Jul 9, 2013 at 18:38
  • Basically, for each row, we're constructing a mini-table with the column values in different rows, and doing a GROUP BY and HAVING to check for groups of matching values. The NULLIF is helping us ignore '' values (making them NULL and then excluding all NULLs). (Just noticed you asked "in your answer"). I did that.
    – GilM
    Jul 9, 2013 at 18:43
2

Try this query:

SELECT PreparedBy, PrelimApprovalBy, Approval1Signer, Approval2Signer
WHERE 
((PreparedBy = PrelimApprovalBy AND NULLIF(PreparedBy, '') IS NOT NULL) 
OR
(PreparedBy = Approval1Signer AND NULLIF(PreparedBy, '') IS NOT NULL) 
OR
(PreparedBy = Approval2Signer AND NULLIF(PreparedBy, '') IS NOT NULL) 
OR
(PrelimApprovalBy = Approval1Signer  AND NULLIF(PrelimApprovalBy, '') IS NOT NULL) 
OR 
(PrelimApprovalBy = Approval2Signer AND NULLIF(PrelimApprovalBy, '') IS NOT NULL) 
OR
(Approval1Signer = Approval2Signer AND NULLIF(Approval1Signer, '') IS NOT NULL))

I can't think of anything simplier to achieve what you seek for.

5
  • this won't work! What if PreparedBy IS NULL and Approval1Signer = Approval2Signer. I would want that record but you query would exclude it because PreparedBy IS NULL.
    – Denis
    Jul 9, 2013 at 18:11
  • @Denis It looks quite ugly - but then again: I can't think of anything easier which would get the results you are looking for. Jul 9, 2013 at 18:13
  • oh gosh, it looks like something that landed in Roswell! There's got to be a better way to do it...
    – Denis
    Jul 9, 2013 at 18:16
  • @Denis Hahaha. GilM's answer looks more beautiful. I wonder if it's also more performant. Can you please test them against each other? (I'm really curious about the outcome) Jul 9, 2013 at 18:19
  • My dataset is too small to see a difference. only about 2000 rows. I am also wondering if you missed any cases here. I know you are using the transitive property here but I would really need to check it.
    – Denis
    Jul 9, 2013 at 18:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.