compare columns only when NOT NULLAdd a column with a default value to an existing table in SQL ServerHow to return only the Date from a SQL Server DateTime datatypeHow to check if a column exists in a SQL Server table?sql union with an aggregation componentAltering a column: null to not nullNull Value StatementFind all tables containing column with specified name - MS SQL ServerT Sql Column only fills with nullHow to insert rows using values from mulitple columns in the same table where there are over 100 columns? Possible use LOOP?Copy View from MS SQL Server Database to a MySQL table

Does the Intel 8086 CPU have user mode and kernel mode?

Why can't my huge trees be chopped down?

How do I stop my characters falling in love?

Is a fighting a fallen friend with the help of a redeemed villain story too much for one book

The best place for swimming in Arctic Ocean

How to store my pliers and wire cutters on my desk?

Examples of simultaneous independent breakthroughs

What is this spacecraft tethered to another spacecraft in LEO (vintage)

Japanese reading of an integer

How to apply the changes to my `.zshrc` file after edit?

Why does Canada require mandatory bilingualism in all government posts?

If my pay period is split between 2 calendar years, which tax year do I file them in?

How can religions be structured in ways that allow inter-faith councils to work?

Can anyone give a concrete example to illustrate what is an uniform prior?

When does Haskell complain about incorrect typing in functions?

How do I explain an exponentially complex intuitively?

Vertical tennis ball into fancy new enumerate

TSA asking to see cell phone

Is this photo showing a woman standing in the nude before teenagers real?

Is a topological space considered to be a class in set theory?

Am I allowed to use personal conversation as a source?

What does "see" in "the Holy See" mean?

Send a single HTML email from Thunderbird, overriding the default "plain text" setting

Why force the nose of 737 Max down in the first place?



compare columns only when NOT NULL


Add a column with a default value to an existing table in SQL ServerHow to return only the Date from a SQL Server DateTime datatypeHow to check if a column exists in a SQL Server table?sql union with an aggregation componentAltering a column: null to not nullNull Value StatementFind all tables containing column with specified name - MS SQL ServerT Sql Column only fills with nullHow to insert rows using values from mulitple columns in the same table where there are over 100 columns? Possible use LOOP?Copy View from MS SQL Server Database to a MySQL table






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








5















This should be easy, but I'm just missing something. I have the following:



IF OBJECT_ID('LAST_NM') IS NOT NULL
DROP TABLE LAST_NM

CREATE TABLE LAST_NM (
ID int NOT NULL IDENTITY(1,1),
LAST_NM_ORIGINAL varchar(255) NOT NULL,
LAST_NM_1 varchar(255)NULL,
LAST_NM_2 varchar(255)NULL,
LAST_NM_3 varchar(255)NULL,
LAST_NM_4 varchar(255)NULL,
PRIMARY KEY (ID)
);

INSERT INTO LAST_NM
(LAST_NM_ORIGINAL, LAST_NM_1, LAST_NM_2, LAST_NM_3, LAST_NM_4)
VALUES
('SMITH', 'HARRIS', NULL, 'HARRIS', NULL),
('JONES', 'FUTURE', 'FUTURE', 'FUTURE', 'FUTURE'),
('SMITH', 'ALPHA', 'ALPHA', 'ALPHA', NULL),
('SMITH', 'BETA', 'BETA', 'GEORGE', NULL),
('SMITH', 'SMITH', NULL, 'SMITH', NULL),
('DOPE', NULL, NULL, NULL, 'CURLS')


What I want to do is SELECT from this table where:



  • the last_nm_# IS NOT NULL

  • the NOT NULL last_nm_# all have the same value

  • those last_nm_# are not the same as the LAST_NM_ORIGINAL

I've tried playing with CASE and SWITCH and I got a messy version working if I hard code the heck out of statements and union them like this:




SELECT * FROM (
SELECT ID, LAST_NM_ORIGINAL, LAST_NM_1, LAST_NM_2, LAST_NM_3, LAST_NM_4
FROM LAST_NM
WHERE LAST_NM_1 IS NOT NULL
AND LAST_NM_2 IS NOT NULL
AND LAST_NM_3 IS NOT NULL
AND LAST_NM_4 IS NOT NULL
AND LAST_NM_1 = LAST_NM_2
AND LAST_NM_1 = LAST_NM_3
AND LAST_NM_3 = LAST_NM_4
AND LAST_NM_1 <> LAST_NM_ORIGINAL
UNION
SELECT ID, LAST_NM_ORIGINAL, LAST_NM_1, LAST_NM_2, LAST_NM_3, LAST_NM_4
FROM LAST_NM
WHERE LAST_NM_1 IS NOT NULL
AND LAST_NM_2 IS NOT NULL
AND LAST_NM_3 IS NOT NULL
AND LAST_NM_4 IS NULL
AND LAST_NM_1 = LAST_NM_2
AND LAST_NM_1 = LAST_NM_3
AND LAST_NM_1 <> LAST_NM_ORIGINAL
/*
WRITE OUT EACH POSSIBLE WAY AND UNION ALL OF THEM
.
.
.
*/
UNION
SELECT ID, LAST_NM_ORIGINAL, LAST_NM_1, LAST_NM_2, LAST_NM_3, LAST_NM_4
FROM LAST_NM
WHERE LAST_NM_1 IS NULL
AND LAST_NM_2 IS NULL
AND LAST_NM_3 IS NULL
AND LAST_NM_4 IS NOT NULL
AND LAST_NM_4 <> LAST_NM_ORIGINAL
) AS RESULT_SET


To summarize, I want to select out the rows if the LAST_NM_# is not NULL and is the same as all the other NOT NULL LAST_NM_# and is different than the LAST_NM_ORIGINAL



So in my example, I should get back rows 1, 2, 3, and 6. But not rows 4 (the 'new' names don't agree) or row 5 (the new names are the same as the old one).



There has to be a better way than just writing each of those out and UNIONING them.. right?










share|improve this question
























  • Your sample data and what you expect for results doesn't match, and you only have 5 rows in your table in the example, so having row 6 come back as part of the results is not valid.

    – Ryan Wilson
    Mar 26 at 18:22











  • whoops! updating! thank you! updated :)

    – sniperd
    Mar 26 at 18:22












  • No problem, thanks for updating.

    – Ryan Wilson
    Mar 26 at 18:23











  • I don't understand the logic. Why do you want rows 3 & 6 but not row 1? What's the logic behind that?

    – Tab Alleman
    Mar 26 at 18:26












  • Once again, whoops! :) Yes row 1 should be returned, I've updated the question. Row 5 should not return as the 'new' last name is the same as the 'old' last name. Thank you!

    – sniperd
    Mar 26 at 18:28


















5















This should be easy, but I'm just missing something. I have the following:



IF OBJECT_ID('LAST_NM') IS NOT NULL
DROP TABLE LAST_NM

CREATE TABLE LAST_NM (
ID int NOT NULL IDENTITY(1,1),
LAST_NM_ORIGINAL varchar(255) NOT NULL,
LAST_NM_1 varchar(255)NULL,
LAST_NM_2 varchar(255)NULL,
LAST_NM_3 varchar(255)NULL,
LAST_NM_4 varchar(255)NULL,
PRIMARY KEY (ID)
);

INSERT INTO LAST_NM
(LAST_NM_ORIGINAL, LAST_NM_1, LAST_NM_2, LAST_NM_3, LAST_NM_4)
VALUES
('SMITH', 'HARRIS', NULL, 'HARRIS', NULL),
('JONES', 'FUTURE', 'FUTURE', 'FUTURE', 'FUTURE'),
('SMITH', 'ALPHA', 'ALPHA', 'ALPHA', NULL),
('SMITH', 'BETA', 'BETA', 'GEORGE', NULL),
('SMITH', 'SMITH', NULL, 'SMITH', NULL),
('DOPE', NULL, NULL, NULL, 'CURLS')


What I want to do is SELECT from this table where:



  • the last_nm_# IS NOT NULL

  • the NOT NULL last_nm_# all have the same value

  • those last_nm_# are not the same as the LAST_NM_ORIGINAL

I've tried playing with CASE and SWITCH and I got a messy version working if I hard code the heck out of statements and union them like this:




SELECT * FROM (
SELECT ID, LAST_NM_ORIGINAL, LAST_NM_1, LAST_NM_2, LAST_NM_3, LAST_NM_4
FROM LAST_NM
WHERE LAST_NM_1 IS NOT NULL
AND LAST_NM_2 IS NOT NULL
AND LAST_NM_3 IS NOT NULL
AND LAST_NM_4 IS NOT NULL
AND LAST_NM_1 = LAST_NM_2
AND LAST_NM_1 = LAST_NM_3
AND LAST_NM_3 = LAST_NM_4
AND LAST_NM_1 <> LAST_NM_ORIGINAL
UNION
SELECT ID, LAST_NM_ORIGINAL, LAST_NM_1, LAST_NM_2, LAST_NM_3, LAST_NM_4
FROM LAST_NM
WHERE LAST_NM_1 IS NOT NULL
AND LAST_NM_2 IS NOT NULL
AND LAST_NM_3 IS NOT NULL
AND LAST_NM_4 IS NULL
AND LAST_NM_1 = LAST_NM_2
AND LAST_NM_1 = LAST_NM_3
AND LAST_NM_1 <> LAST_NM_ORIGINAL
/*
WRITE OUT EACH POSSIBLE WAY AND UNION ALL OF THEM
.
.
.
*/
UNION
SELECT ID, LAST_NM_ORIGINAL, LAST_NM_1, LAST_NM_2, LAST_NM_3, LAST_NM_4
FROM LAST_NM
WHERE LAST_NM_1 IS NULL
AND LAST_NM_2 IS NULL
AND LAST_NM_3 IS NULL
AND LAST_NM_4 IS NOT NULL
AND LAST_NM_4 <> LAST_NM_ORIGINAL
) AS RESULT_SET


To summarize, I want to select out the rows if the LAST_NM_# is not NULL and is the same as all the other NOT NULL LAST_NM_# and is different than the LAST_NM_ORIGINAL



So in my example, I should get back rows 1, 2, 3, and 6. But not rows 4 (the 'new' names don't agree) or row 5 (the new names are the same as the old one).



There has to be a better way than just writing each of those out and UNIONING them.. right?










share|improve this question
























  • Your sample data and what you expect for results doesn't match, and you only have 5 rows in your table in the example, so having row 6 come back as part of the results is not valid.

    – Ryan Wilson
    Mar 26 at 18:22











  • whoops! updating! thank you! updated :)

    – sniperd
    Mar 26 at 18:22












  • No problem, thanks for updating.

    – Ryan Wilson
    Mar 26 at 18:23











  • I don't understand the logic. Why do you want rows 3 & 6 but not row 1? What's the logic behind that?

    – Tab Alleman
    Mar 26 at 18:26












  • Once again, whoops! :) Yes row 1 should be returned, I've updated the question. Row 5 should not return as the 'new' last name is the same as the 'old' last name. Thank you!

    – sniperd
    Mar 26 at 18:28














5












5








5


1






This should be easy, but I'm just missing something. I have the following:



IF OBJECT_ID('LAST_NM') IS NOT NULL
DROP TABLE LAST_NM

CREATE TABLE LAST_NM (
ID int NOT NULL IDENTITY(1,1),
LAST_NM_ORIGINAL varchar(255) NOT NULL,
LAST_NM_1 varchar(255)NULL,
LAST_NM_2 varchar(255)NULL,
LAST_NM_3 varchar(255)NULL,
LAST_NM_4 varchar(255)NULL,
PRIMARY KEY (ID)
);

INSERT INTO LAST_NM
(LAST_NM_ORIGINAL, LAST_NM_1, LAST_NM_2, LAST_NM_3, LAST_NM_4)
VALUES
('SMITH', 'HARRIS', NULL, 'HARRIS', NULL),
('JONES', 'FUTURE', 'FUTURE', 'FUTURE', 'FUTURE'),
('SMITH', 'ALPHA', 'ALPHA', 'ALPHA', NULL),
('SMITH', 'BETA', 'BETA', 'GEORGE', NULL),
('SMITH', 'SMITH', NULL, 'SMITH', NULL),
('DOPE', NULL, NULL, NULL, 'CURLS')


What I want to do is SELECT from this table where:



  • the last_nm_# IS NOT NULL

  • the NOT NULL last_nm_# all have the same value

  • those last_nm_# are not the same as the LAST_NM_ORIGINAL

I've tried playing with CASE and SWITCH and I got a messy version working if I hard code the heck out of statements and union them like this:




SELECT * FROM (
SELECT ID, LAST_NM_ORIGINAL, LAST_NM_1, LAST_NM_2, LAST_NM_3, LAST_NM_4
FROM LAST_NM
WHERE LAST_NM_1 IS NOT NULL
AND LAST_NM_2 IS NOT NULL
AND LAST_NM_3 IS NOT NULL
AND LAST_NM_4 IS NOT NULL
AND LAST_NM_1 = LAST_NM_2
AND LAST_NM_1 = LAST_NM_3
AND LAST_NM_3 = LAST_NM_4
AND LAST_NM_1 <> LAST_NM_ORIGINAL
UNION
SELECT ID, LAST_NM_ORIGINAL, LAST_NM_1, LAST_NM_2, LAST_NM_3, LAST_NM_4
FROM LAST_NM
WHERE LAST_NM_1 IS NOT NULL
AND LAST_NM_2 IS NOT NULL
AND LAST_NM_3 IS NOT NULL
AND LAST_NM_4 IS NULL
AND LAST_NM_1 = LAST_NM_2
AND LAST_NM_1 = LAST_NM_3
AND LAST_NM_1 <> LAST_NM_ORIGINAL
/*
WRITE OUT EACH POSSIBLE WAY AND UNION ALL OF THEM
.
.
.
*/
UNION
SELECT ID, LAST_NM_ORIGINAL, LAST_NM_1, LAST_NM_2, LAST_NM_3, LAST_NM_4
FROM LAST_NM
WHERE LAST_NM_1 IS NULL
AND LAST_NM_2 IS NULL
AND LAST_NM_3 IS NULL
AND LAST_NM_4 IS NOT NULL
AND LAST_NM_4 <> LAST_NM_ORIGINAL
) AS RESULT_SET


To summarize, I want to select out the rows if the LAST_NM_# is not NULL and is the same as all the other NOT NULL LAST_NM_# and is different than the LAST_NM_ORIGINAL



So in my example, I should get back rows 1, 2, 3, and 6. But not rows 4 (the 'new' names don't agree) or row 5 (the new names are the same as the old one).



There has to be a better way than just writing each of those out and UNIONING them.. right?










share|improve this question
















This should be easy, but I'm just missing something. I have the following:



IF OBJECT_ID('LAST_NM') IS NOT NULL
DROP TABLE LAST_NM

CREATE TABLE LAST_NM (
ID int NOT NULL IDENTITY(1,1),
LAST_NM_ORIGINAL varchar(255) NOT NULL,
LAST_NM_1 varchar(255)NULL,
LAST_NM_2 varchar(255)NULL,
LAST_NM_3 varchar(255)NULL,
LAST_NM_4 varchar(255)NULL,
PRIMARY KEY (ID)
);

INSERT INTO LAST_NM
(LAST_NM_ORIGINAL, LAST_NM_1, LAST_NM_2, LAST_NM_3, LAST_NM_4)
VALUES
('SMITH', 'HARRIS', NULL, 'HARRIS', NULL),
('JONES', 'FUTURE', 'FUTURE', 'FUTURE', 'FUTURE'),
('SMITH', 'ALPHA', 'ALPHA', 'ALPHA', NULL),
('SMITH', 'BETA', 'BETA', 'GEORGE', NULL),
('SMITH', 'SMITH', NULL, 'SMITH', NULL),
('DOPE', NULL, NULL, NULL, 'CURLS')


What I want to do is SELECT from this table where:



  • the last_nm_# IS NOT NULL

  • the NOT NULL last_nm_# all have the same value

  • those last_nm_# are not the same as the LAST_NM_ORIGINAL

I've tried playing with CASE and SWITCH and I got a messy version working if I hard code the heck out of statements and union them like this:




SELECT * FROM (
SELECT ID, LAST_NM_ORIGINAL, LAST_NM_1, LAST_NM_2, LAST_NM_3, LAST_NM_4
FROM LAST_NM
WHERE LAST_NM_1 IS NOT NULL
AND LAST_NM_2 IS NOT NULL
AND LAST_NM_3 IS NOT NULL
AND LAST_NM_4 IS NOT NULL
AND LAST_NM_1 = LAST_NM_2
AND LAST_NM_1 = LAST_NM_3
AND LAST_NM_3 = LAST_NM_4
AND LAST_NM_1 <> LAST_NM_ORIGINAL
UNION
SELECT ID, LAST_NM_ORIGINAL, LAST_NM_1, LAST_NM_2, LAST_NM_3, LAST_NM_4
FROM LAST_NM
WHERE LAST_NM_1 IS NOT NULL
AND LAST_NM_2 IS NOT NULL
AND LAST_NM_3 IS NOT NULL
AND LAST_NM_4 IS NULL
AND LAST_NM_1 = LAST_NM_2
AND LAST_NM_1 = LAST_NM_3
AND LAST_NM_1 <> LAST_NM_ORIGINAL
/*
WRITE OUT EACH POSSIBLE WAY AND UNION ALL OF THEM
.
.
.
*/
UNION
SELECT ID, LAST_NM_ORIGINAL, LAST_NM_1, LAST_NM_2, LAST_NM_3, LAST_NM_4
FROM LAST_NM
WHERE LAST_NM_1 IS NULL
AND LAST_NM_2 IS NULL
AND LAST_NM_3 IS NULL
AND LAST_NM_4 IS NOT NULL
AND LAST_NM_4 <> LAST_NM_ORIGINAL
) AS RESULT_SET


To summarize, I want to select out the rows if the LAST_NM_# is not NULL and is the same as all the other NOT NULL LAST_NM_# and is different than the LAST_NM_ORIGINAL



So in my example, I should get back rows 1, 2, 3, and 6. But not rows 4 (the 'new' names don't agree) or row 5 (the new names are the same as the old one).



There has to be a better way than just writing each of those out and UNIONING them.. right?







sql-server






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 26 at 18:28







sniperd

















asked Mar 26 at 18:19









sniperdsniperd

3,5635 gold badges19 silver badges32 bronze badges




3,5635 gold badges19 silver badges32 bronze badges












  • Your sample data and what you expect for results doesn't match, and you only have 5 rows in your table in the example, so having row 6 come back as part of the results is not valid.

    – Ryan Wilson
    Mar 26 at 18:22











  • whoops! updating! thank you! updated :)

    – sniperd
    Mar 26 at 18:22












  • No problem, thanks for updating.

    – Ryan Wilson
    Mar 26 at 18:23











  • I don't understand the logic. Why do you want rows 3 & 6 but not row 1? What's the logic behind that?

    – Tab Alleman
    Mar 26 at 18:26












  • Once again, whoops! :) Yes row 1 should be returned, I've updated the question. Row 5 should not return as the 'new' last name is the same as the 'old' last name. Thank you!

    – sniperd
    Mar 26 at 18:28


















  • Your sample data and what you expect for results doesn't match, and you only have 5 rows in your table in the example, so having row 6 come back as part of the results is not valid.

    – Ryan Wilson
    Mar 26 at 18:22











  • whoops! updating! thank you! updated :)

    – sniperd
    Mar 26 at 18:22












  • No problem, thanks for updating.

    – Ryan Wilson
    Mar 26 at 18:23











  • I don't understand the logic. Why do you want rows 3 & 6 but not row 1? What's the logic behind that?

    – Tab Alleman
    Mar 26 at 18:26












  • Once again, whoops! :) Yes row 1 should be returned, I've updated the question. Row 5 should not return as the 'new' last name is the same as the 'old' last name. Thank you!

    – sniperd
    Mar 26 at 18:28

















Your sample data and what you expect for results doesn't match, and you only have 5 rows in your table in the example, so having row 6 come back as part of the results is not valid.

– Ryan Wilson
Mar 26 at 18:22





Your sample data and what you expect for results doesn't match, and you only have 5 rows in your table in the example, so having row 6 come back as part of the results is not valid.

– Ryan Wilson
Mar 26 at 18:22













whoops! updating! thank you! updated :)

– sniperd
Mar 26 at 18:22






whoops! updating! thank you! updated :)

– sniperd
Mar 26 at 18:22














No problem, thanks for updating.

– Ryan Wilson
Mar 26 at 18:23





No problem, thanks for updating.

– Ryan Wilson
Mar 26 at 18:23













I don't understand the logic. Why do you want rows 3 & 6 but not row 1? What's the logic behind that?

– Tab Alleman
Mar 26 at 18:26






I don't understand the logic. Why do you want rows 3 & 6 but not row 1? What's the logic behind that?

– Tab Alleman
Mar 26 at 18:26














Once again, whoops! :) Yes row 1 should be returned, I've updated the question. Row 5 should not return as the 'new' last name is the same as the 'old' last name. Thank you!

– sniperd
Mar 26 at 18:28






Once again, whoops! :) Yes row 1 should be returned, I've updated the question. Row 5 should not return as the 'new' last name is the same as the 'old' last name. Thank you!

– sniperd
Mar 26 at 18:28













6 Answers
6






active

oldest

votes


















2














Here it is using UNPIVOT



;WITH NAMES
AS (
SELECT DISTINCT ID
,LAST_NM_ORIGINAL
,LAST_NM_NEW
FROM (
SELECT ID
,LAST_NM_ORIGINAL
,LAST_NM_1
,LAST_NM_2
,LAST_NM_3
,LAST_NM_4
FROM LAST_NM
) AS X
UNPIVOT(LAST_NM_NEW FOR LAST_NM_NEWS IN (
LAST_NM_1
,LAST_NM_2
,LAST_NM_3
,LAST_NM_4
)) AS Y
)
SELECT ID
,LAST_NM_ORIGINAL
,LAST_NM_NEW
FROM NAMES
WHERE ID IN (
SELECT ID
FROM NAMES
GROUP BY ID
HAVING COUNT(ID) = 1
)
AND LAST_NM_ORIGINAL <> LAST_NM_NEW





share|improve this answer






























    1














    This question immediately screams UNPIVOT to me.



    You can see the link or just google for syntax and examples, and then use it to get a derived table that looks like this:



    ID NM_Orig NM_Number NM_Value
    1 Smith 1 Harris
    1 Smith 2 NULL
    1 Smith 3 Harris
    1 Smith 4 NULL
    2 Jones 1 Future
    etc...


    From that derived table you would query to get IDs WHERE NM_Value is NOT NULL AND NM_Value <> NM_Orig AND WHERE NOT EXISTS a correlated ROW With a NON-NULL NM_Value that is different from the correlated NM_Value.






    share|improve this answer






























      1














      Here's another way to do it.



      SELECT
      *
      FROM
      LAST_NM
      WHERE
      (LAST_NM_1 IS NULL OR LAST_NM_1 = COALESCE(LAST_NM_1, LAST_NM_2, LAST_NM_3, LAST_NM_4)) AND
      (LAST_NM_2 IS NULL OR LAST_NM_2 = COALESCE(LAST_NM_1, LAST_NM_2, LAST_NM_3, LAST_NM_4)) AND
      (LAST_NM_3 IS NULL OR LAST_NM_3 = COALESCE(LAST_NM_1, LAST_NM_2, LAST_NM_3, LAST_NM_4)) AND
      (LAST_NM_4 IS NULL OR LAST_NM_4 = COALESCE(LAST_NM_1, LAST_NM_2, LAST_NM_3, LAST_NM_4)) AND
      (LAST_NM_1 IS NULL OR LAST_NM_1 <> LAST_NM_ORIGINAL) AND
      (LAST_NM_2 IS NULL OR LAST_NM_2 <> LAST_NM_ORIGINAL) AND
      (LAST_NM_3 IS NULL OR LAST_NM_3 <> LAST_NM_ORIGINAL) AND
      (LAST_NM_4 IS NULL OR LAST_NM_4 <> LAST_NM_ORIGINAL)


      Edit. Can be shortened to the query below since you want at least one of the LAST_NM_# to not be null:



      SELECT
      *
      FROM
      LAST_NM
      WHERE
      (LAST_NM_1 IS NULL OR LAST_NM_1 = COALESCE(LAST_NM_1, LAST_NM_2, LAST_NM_3, LAST_NM_4)) AND
      (LAST_NM_2 IS NULL OR LAST_NM_2 = COALESCE(LAST_NM_1, LAST_NM_2, LAST_NM_3, LAST_NM_4)) AND
      (LAST_NM_3 IS NULL OR LAST_NM_3 = COALESCE(LAST_NM_1, LAST_NM_2, LAST_NM_3, LAST_NM_4)) AND
      (LAST_NM_4 IS NULL OR LAST_NM_4 = COALESCE(LAST_NM_1, LAST_NM_2, LAST_NM_3, LAST_NM_4)) AND
      (LAST_NM_ORIGINAL <> COALESCE(LAST_NM_1, LAST_NM_2, LAST_NM_3, LAST_NM_4))





      share|improve this answer
































        1














        Here is one way to do this.



        select ID
        , LAST_NM_ORIGINAL
        , LAST_NM_1
        , LAST_NM_2
        , LAST_NM_3
        , LAST_NM_4
        from LAST_NM
        where replace(isnull(LAST_NM_1, '') + isnull(LAST_NM_2, '') + isnull(LAST_NM_3, '') + isnull(LAST_NM_4, ''), LAST_NM_ORIGINAL, '') > ''
        AND replace(isnull(LAST_NM_1, '') + isnull(LAST_NM_2, '') + isnull(LAST_NM_3, '') + isnull(LAST_NM_4, ''), coalesce(LAST_NM_1, LAST_NM_2, LAST_NM_3, LAST_NM_4), '') = ''





        share|improve this answer

























        • Not following; ID is an identity column. It's unique. What does grouping by ID achieve?

          – Tab Alleman
          Mar 26 at 18:45











        • This is bringing back line 4: 4 SMITH BETA BETA GEORGE NULL which should not

          – sniperd
          Mar 26 at 18:45











        • Just saw that requirement. Give me a second.

          – Sean Lange
          Mar 26 at 18:45











        • @sniperd try now.

          – Sean Lange
          Mar 26 at 18:48











        • @TabAlleman it is just to force the aggregation so you don't have to UNPIVOT and then PIVOT.

          – Sean Lange
          Mar 26 at 18:50


















        1














        Your original query does have some superfluous predicates. Any row meeting LAST_NM_1 = LAST_NM_2 is guaranteed to have both LAST_NM_1 NOT NULL and LAST_NM_2 NOT NULL for example.



        But it can be made quite concise by using VALUES to get the 4 columns in tabular form as below.



        SELECT *
        FROM LAST_NM
        WHERE EXISTS (SELECT *
        FROM (VALUES(LAST_NM_1),
        (LAST_NM_2),
        (LAST_NM_3),
        (LAST_NM_4)) V(LAST_NM_N)
        HAVING MAX(LAST_NM_N) = MIN(LAST_NM_N) /*exactly one NOT NULL value among the 4 columns*/
        AND MAX(LAST_NM_N) <> LAST_NM_ORIGINAL)





        share|improve this answer
































          1














          Here is another way to do it



          SELECT * FROM
          #LAST_NM
          WHERE
          COALESCE(LAST_NM_1, LAST_NM_2, LAST_NM_3, LAST_NM_4) = COALESCE(LAST_NM_2, LAST_NM_3, LAST_NM_4,LAST_NM_1)
          AND COALESCE(LAST_NM_2, LAST_NM_3, LAST_NM_4,LAST_NM_1) = COALESCE(LAST_NM_3, LAST_NM_4,LAST_NM_1,LAST_NM_2)
          AND COALESCE(LAST_NM_3, LAST_NM_4,LAST_NM_1,LAST_NM_2) = COALESCE(LAST_NM_4,LAST_NM_1,LAST_NM_2,LAST_NM_3)
          AND (LAST_NM_ORIGINAL <> COALESCE(LAST_NM_1, LAST_NM_2, LAST_NM_3, LAST_NM_4))





          share|improve this answer

























          • This is incorrect. Consider a record where LAST_NM_1 = LAST_NM_2 = LAST_NM_3 <> LAST_NM_4. This query will output that faulty record.

            – bdebaere
            Mar 28 at 4:06











          • yep, sorry allready edited my awnser. thanks

            – Krone Torres
            Mar 29 at 2:11













          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55363861%2fcompare-columns-only-when-not-null%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          6 Answers
          6






          active

          oldest

          votes








          6 Answers
          6






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          Here it is using UNPIVOT



          ;WITH NAMES
          AS (
          SELECT DISTINCT ID
          ,LAST_NM_ORIGINAL
          ,LAST_NM_NEW
          FROM (
          SELECT ID
          ,LAST_NM_ORIGINAL
          ,LAST_NM_1
          ,LAST_NM_2
          ,LAST_NM_3
          ,LAST_NM_4
          FROM LAST_NM
          ) AS X
          UNPIVOT(LAST_NM_NEW FOR LAST_NM_NEWS IN (
          LAST_NM_1
          ,LAST_NM_2
          ,LAST_NM_3
          ,LAST_NM_4
          )) AS Y
          )
          SELECT ID
          ,LAST_NM_ORIGINAL
          ,LAST_NM_NEW
          FROM NAMES
          WHERE ID IN (
          SELECT ID
          FROM NAMES
          GROUP BY ID
          HAVING COUNT(ID) = 1
          )
          AND LAST_NM_ORIGINAL <> LAST_NM_NEW





          share|improve this answer



























            2














            Here it is using UNPIVOT



            ;WITH NAMES
            AS (
            SELECT DISTINCT ID
            ,LAST_NM_ORIGINAL
            ,LAST_NM_NEW
            FROM (
            SELECT ID
            ,LAST_NM_ORIGINAL
            ,LAST_NM_1
            ,LAST_NM_2
            ,LAST_NM_3
            ,LAST_NM_4
            FROM LAST_NM
            ) AS X
            UNPIVOT(LAST_NM_NEW FOR LAST_NM_NEWS IN (
            LAST_NM_1
            ,LAST_NM_2
            ,LAST_NM_3
            ,LAST_NM_4
            )) AS Y
            )
            SELECT ID
            ,LAST_NM_ORIGINAL
            ,LAST_NM_NEW
            FROM NAMES
            WHERE ID IN (
            SELECT ID
            FROM NAMES
            GROUP BY ID
            HAVING COUNT(ID) = 1
            )
            AND LAST_NM_ORIGINAL <> LAST_NM_NEW





            share|improve this answer

























              2












              2








              2







              Here it is using UNPIVOT



              ;WITH NAMES
              AS (
              SELECT DISTINCT ID
              ,LAST_NM_ORIGINAL
              ,LAST_NM_NEW
              FROM (
              SELECT ID
              ,LAST_NM_ORIGINAL
              ,LAST_NM_1
              ,LAST_NM_2
              ,LAST_NM_3
              ,LAST_NM_4
              FROM LAST_NM
              ) AS X
              UNPIVOT(LAST_NM_NEW FOR LAST_NM_NEWS IN (
              LAST_NM_1
              ,LAST_NM_2
              ,LAST_NM_3
              ,LAST_NM_4
              )) AS Y
              )
              SELECT ID
              ,LAST_NM_ORIGINAL
              ,LAST_NM_NEW
              FROM NAMES
              WHERE ID IN (
              SELECT ID
              FROM NAMES
              GROUP BY ID
              HAVING COUNT(ID) = 1
              )
              AND LAST_NM_ORIGINAL <> LAST_NM_NEW





              share|improve this answer













              Here it is using UNPIVOT



              ;WITH NAMES
              AS (
              SELECT DISTINCT ID
              ,LAST_NM_ORIGINAL
              ,LAST_NM_NEW
              FROM (
              SELECT ID
              ,LAST_NM_ORIGINAL
              ,LAST_NM_1
              ,LAST_NM_2
              ,LAST_NM_3
              ,LAST_NM_4
              FROM LAST_NM
              ) AS X
              UNPIVOT(LAST_NM_NEW FOR LAST_NM_NEWS IN (
              LAST_NM_1
              ,LAST_NM_2
              ,LAST_NM_3
              ,LAST_NM_4
              )) AS Y
              )
              SELECT ID
              ,LAST_NM_ORIGINAL
              ,LAST_NM_NEW
              FROM NAMES
              WHERE ID IN (
              SELECT ID
              FROM NAMES
              GROUP BY ID
              HAVING COUNT(ID) = 1
              )
              AND LAST_NM_ORIGINAL <> LAST_NM_NEW






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Mar 26 at 20:15









              WhilstWhilst

              363 bronze badges




              363 bronze badges























                  1














                  This question immediately screams UNPIVOT to me.



                  You can see the link or just google for syntax and examples, and then use it to get a derived table that looks like this:



                  ID NM_Orig NM_Number NM_Value
                  1 Smith 1 Harris
                  1 Smith 2 NULL
                  1 Smith 3 Harris
                  1 Smith 4 NULL
                  2 Jones 1 Future
                  etc...


                  From that derived table you would query to get IDs WHERE NM_Value is NOT NULL AND NM_Value <> NM_Orig AND WHERE NOT EXISTS a correlated ROW With a NON-NULL NM_Value that is different from the correlated NM_Value.






                  share|improve this answer



























                    1














                    This question immediately screams UNPIVOT to me.



                    You can see the link or just google for syntax and examples, and then use it to get a derived table that looks like this:



                    ID NM_Orig NM_Number NM_Value
                    1 Smith 1 Harris
                    1 Smith 2 NULL
                    1 Smith 3 Harris
                    1 Smith 4 NULL
                    2 Jones 1 Future
                    etc...


                    From that derived table you would query to get IDs WHERE NM_Value is NOT NULL AND NM_Value <> NM_Orig AND WHERE NOT EXISTS a correlated ROW With a NON-NULL NM_Value that is different from the correlated NM_Value.






                    share|improve this answer

























                      1












                      1








                      1







                      This question immediately screams UNPIVOT to me.



                      You can see the link or just google for syntax and examples, and then use it to get a derived table that looks like this:



                      ID NM_Orig NM_Number NM_Value
                      1 Smith 1 Harris
                      1 Smith 2 NULL
                      1 Smith 3 Harris
                      1 Smith 4 NULL
                      2 Jones 1 Future
                      etc...


                      From that derived table you would query to get IDs WHERE NM_Value is NOT NULL AND NM_Value <> NM_Orig AND WHERE NOT EXISTS a correlated ROW With a NON-NULL NM_Value that is different from the correlated NM_Value.






                      share|improve this answer













                      This question immediately screams UNPIVOT to me.



                      You can see the link or just google for syntax and examples, and then use it to get a derived table that looks like this:



                      ID NM_Orig NM_Number NM_Value
                      1 Smith 1 Harris
                      1 Smith 2 NULL
                      1 Smith 3 Harris
                      1 Smith 4 NULL
                      2 Jones 1 Future
                      etc...


                      From that derived table you would query to get IDs WHERE NM_Value is NOT NULL AND NM_Value <> NM_Orig AND WHERE NOT EXISTS a correlated ROW With a NON-NULL NM_Value that is different from the correlated NM_Value.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Mar 26 at 18:46









                      Tab AllemanTab Alleman

                      28.4k6 gold badges26 silver badges45 bronze badges




                      28.4k6 gold badges26 silver badges45 bronze badges





















                          1














                          Here's another way to do it.



                          SELECT
                          *
                          FROM
                          LAST_NM
                          WHERE
                          (LAST_NM_1 IS NULL OR LAST_NM_1 = COALESCE(LAST_NM_1, LAST_NM_2, LAST_NM_3, LAST_NM_4)) AND
                          (LAST_NM_2 IS NULL OR LAST_NM_2 = COALESCE(LAST_NM_1, LAST_NM_2, LAST_NM_3, LAST_NM_4)) AND
                          (LAST_NM_3 IS NULL OR LAST_NM_3 = COALESCE(LAST_NM_1, LAST_NM_2, LAST_NM_3, LAST_NM_4)) AND
                          (LAST_NM_4 IS NULL OR LAST_NM_4 = COALESCE(LAST_NM_1, LAST_NM_2, LAST_NM_3, LAST_NM_4)) AND
                          (LAST_NM_1 IS NULL OR LAST_NM_1 <> LAST_NM_ORIGINAL) AND
                          (LAST_NM_2 IS NULL OR LAST_NM_2 <> LAST_NM_ORIGINAL) AND
                          (LAST_NM_3 IS NULL OR LAST_NM_3 <> LAST_NM_ORIGINAL) AND
                          (LAST_NM_4 IS NULL OR LAST_NM_4 <> LAST_NM_ORIGINAL)


                          Edit. Can be shortened to the query below since you want at least one of the LAST_NM_# to not be null:



                          SELECT
                          *
                          FROM
                          LAST_NM
                          WHERE
                          (LAST_NM_1 IS NULL OR LAST_NM_1 = COALESCE(LAST_NM_1, LAST_NM_2, LAST_NM_3, LAST_NM_4)) AND
                          (LAST_NM_2 IS NULL OR LAST_NM_2 = COALESCE(LAST_NM_1, LAST_NM_2, LAST_NM_3, LAST_NM_4)) AND
                          (LAST_NM_3 IS NULL OR LAST_NM_3 = COALESCE(LAST_NM_1, LAST_NM_2, LAST_NM_3, LAST_NM_4)) AND
                          (LAST_NM_4 IS NULL OR LAST_NM_4 = COALESCE(LAST_NM_1, LAST_NM_2, LAST_NM_3, LAST_NM_4)) AND
                          (LAST_NM_ORIGINAL <> COALESCE(LAST_NM_1, LAST_NM_2, LAST_NM_3, LAST_NM_4))





                          share|improve this answer





























                            1














                            Here's another way to do it.



                            SELECT
                            *
                            FROM
                            LAST_NM
                            WHERE
                            (LAST_NM_1 IS NULL OR LAST_NM_1 = COALESCE(LAST_NM_1, LAST_NM_2, LAST_NM_3, LAST_NM_4)) AND
                            (LAST_NM_2 IS NULL OR LAST_NM_2 = COALESCE(LAST_NM_1, LAST_NM_2, LAST_NM_3, LAST_NM_4)) AND
                            (LAST_NM_3 IS NULL OR LAST_NM_3 = COALESCE(LAST_NM_1, LAST_NM_2, LAST_NM_3, LAST_NM_4)) AND
                            (LAST_NM_4 IS NULL OR LAST_NM_4 = COALESCE(LAST_NM_1, LAST_NM_2, LAST_NM_3, LAST_NM_4)) AND
                            (LAST_NM_1 IS NULL OR LAST_NM_1 <> LAST_NM_ORIGINAL) AND
                            (LAST_NM_2 IS NULL OR LAST_NM_2 <> LAST_NM_ORIGINAL) AND
                            (LAST_NM_3 IS NULL OR LAST_NM_3 <> LAST_NM_ORIGINAL) AND
                            (LAST_NM_4 IS NULL OR LAST_NM_4 <> LAST_NM_ORIGINAL)


                            Edit. Can be shortened to the query below since you want at least one of the LAST_NM_# to not be null:



                            SELECT
                            *
                            FROM
                            LAST_NM
                            WHERE
                            (LAST_NM_1 IS NULL OR LAST_NM_1 = COALESCE(LAST_NM_1, LAST_NM_2, LAST_NM_3, LAST_NM_4)) AND
                            (LAST_NM_2 IS NULL OR LAST_NM_2 = COALESCE(LAST_NM_1, LAST_NM_2, LAST_NM_3, LAST_NM_4)) AND
                            (LAST_NM_3 IS NULL OR LAST_NM_3 = COALESCE(LAST_NM_1, LAST_NM_2, LAST_NM_3, LAST_NM_4)) AND
                            (LAST_NM_4 IS NULL OR LAST_NM_4 = COALESCE(LAST_NM_1, LAST_NM_2, LAST_NM_3, LAST_NM_4)) AND
                            (LAST_NM_ORIGINAL <> COALESCE(LAST_NM_1, LAST_NM_2, LAST_NM_3, LAST_NM_4))





                            share|improve this answer



























                              1












                              1








                              1







                              Here's another way to do it.



                              SELECT
                              *
                              FROM
                              LAST_NM
                              WHERE
                              (LAST_NM_1 IS NULL OR LAST_NM_1 = COALESCE(LAST_NM_1, LAST_NM_2, LAST_NM_3, LAST_NM_4)) AND
                              (LAST_NM_2 IS NULL OR LAST_NM_2 = COALESCE(LAST_NM_1, LAST_NM_2, LAST_NM_3, LAST_NM_4)) AND
                              (LAST_NM_3 IS NULL OR LAST_NM_3 = COALESCE(LAST_NM_1, LAST_NM_2, LAST_NM_3, LAST_NM_4)) AND
                              (LAST_NM_4 IS NULL OR LAST_NM_4 = COALESCE(LAST_NM_1, LAST_NM_2, LAST_NM_3, LAST_NM_4)) AND
                              (LAST_NM_1 IS NULL OR LAST_NM_1 <> LAST_NM_ORIGINAL) AND
                              (LAST_NM_2 IS NULL OR LAST_NM_2 <> LAST_NM_ORIGINAL) AND
                              (LAST_NM_3 IS NULL OR LAST_NM_3 <> LAST_NM_ORIGINAL) AND
                              (LAST_NM_4 IS NULL OR LAST_NM_4 <> LAST_NM_ORIGINAL)


                              Edit. Can be shortened to the query below since you want at least one of the LAST_NM_# to not be null:



                              SELECT
                              *
                              FROM
                              LAST_NM
                              WHERE
                              (LAST_NM_1 IS NULL OR LAST_NM_1 = COALESCE(LAST_NM_1, LAST_NM_2, LAST_NM_3, LAST_NM_4)) AND
                              (LAST_NM_2 IS NULL OR LAST_NM_2 = COALESCE(LAST_NM_1, LAST_NM_2, LAST_NM_3, LAST_NM_4)) AND
                              (LAST_NM_3 IS NULL OR LAST_NM_3 = COALESCE(LAST_NM_1, LAST_NM_2, LAST_NM_3, LAST_NM_4)) AND
                              (LAST_NM_4 IS NULL OR LAST_NM_4 = COALESCE(LAST_NM_1, LAST_NM_2, LAST_NM_3, LAST_NM_4)) AND
                              (LAST_NM_ORIGINAL <> COALESCE(LAST_NM_1, LAST_NM_2, LAST_NM_3, LAST_NM_4))





                              share|improve this answer















                              Here's another way to do it.



                              SELECT
                              *
                              FROM
                              LAST_NM
                              WHERE
                              (LAST_NM_1 IS NULL OR LAST_NM_1 = COALESCE(LAST_NM_1, LAST_NM_2, LAST_NM_3, LAST_NM_4)) AND
                              (LAST_NM_2 IS NULL OR LAST_NM_2 = COALESCE(LAST_NM_1, LAST_NM_2, LAST_NM_3, LAST_NM_4)) AND
                              (LAST_NM_3 IS NULL OR LAST_NM_3 = COALESCE(LAST_NM_1, LAST_NM_2, LAST_NM_3, LAST_NM_4)) AND
                              (LAST_NM_4 IS NULL OR LAST_NM_4 = COALESCE(LAST_NM_1, LAST_NM_2, LAST_NM_3, LAST_NM_4)) AND
                              (LAST_NM_1 IS NULL OR LAST_NM_1 <> LAST_NM_ORIGINAL) AND
                              (LAST_NM_2 IS NULL OR LAST_NM_2 <> LAST_NM_ORIGINAL) AND
                              (LAST_NM_3 IS NULL OR LAST_NM_3 <> LAST_NM_ORIGINAL) AND
                              (LAST_NM_4 IS NULL OR LAST_NM_4 <> LAST_NM_ORIGINAL)


                              Edit. Can be shortened to the query below since you want at least one of the LAST_NM_# to not be null:



                              SELECT
                              *
                              FROM
                              LAST_NM
                              WHERE
                              (LAST_NM_1 IS NULL OR LAST_NM_1 = COALESCE(LAST_NM_1, LAST_NM_2, LAST_NM_3, LAST_NM_4)) AND
                              (LAST_NM_2 IS NULL OR LAST_NM_2 = COALESCE(LAST_NM_1, LAST_NM_2, LAST_NM_3, LAST_NM_4)) AND
                              (LAST_NM_3 IS NULL OR LAST_NM_3 = COALESCE(LAST_NM_1, LAST_NM_2, LAST_NM_3, LAST_NM_4)) AND
                              (LAST_NM_4 IS NULL OR LAST_NM_4 = COALESCE(LAST_NM_1, LAST_NM_2, LAST_NM_3, LAST_NM_4)) AND
                              (LAST_NM_ORIGINAL <> COALESCE(LAST_NM_1, LAST_NM_2, LAST_NM_3, LAST_NM_4))






                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Mar 26 at 18:52

























                              answered Mar 26 at 18:44









                              bdebaerebdebaere

                              9336 silver badges19 bronze badges




                              9336 silver badges19 bronze badges





















                                  1














                                  Here is one way to do this.



                                  select ID
                                  , LAST_NM_ORIGINAL
                                  , LAST_NM_1
                                  , LAST_NM_2
                                  , LAST_NM_3
                                  , LAST_NM_4
                                  from LAST_NM
                                  where replace(isnull(LAST_NM_1, '') + isnull(LAST_NM_2, '') + isnull(LAST_NM_3, '') + isnull(LAST_NM_4, ''), LAST_NM_ORIGINAL, '') > ''
                                  AND replace(isnull(LAST_NM_1, '') + isnull(LAST_NM_2, '') + isnull(LAST_NM_3, '') + isnull(LAST_NM_4, ''), coalesce(LAST_NM_1, LAST_NM_2, LAST_NM_3, LAST_NM_4), '') = ''





                                  share|improve this answer

























                                  • Not following; ID is an identity column. It's unique. What does grouping by ID achieve?

                                    – Tab Alleman
                                    Mar 26 at 18:45











                                  • This is bringing back line 4: 4 SMITH BETA BETA GEORGE NULL which should not

                                    – sniperd
                                    Mar 26 at 18:45











                                  • Just saw that requirement. Give me a second.

                                    – Sean Lange
                                    Mar 26 at 18:45











                                  • @sniperd try now.

                                    – Sean Lange
                                    Mar 26 at 18:48











                                  • @TabAlleman it is just to force the aggregation so you don't have to UNPIVOT and then PIVOT.

                                    – Sean Lange
                                    Mar 26 at 18:50















                                  1














                                  Here is one way to do this.



                                  select ID
                                  , LAST_NM_ORIGINAL
                                  , LAST_NM_1
                                  , LAST_NM_2
                                  , LAST_NM_3
                                  , LAST_NM_4
                                  from LAST_NM
                                  where replace(isnull(LAST_NM_1, '') + isnull(LAST_NM_2, '') + isnull(LAST_NM_3, '') + isnull(LAST_NM_4, ''), LAST_NM_ORIGINAL, '') > ''
                                  AND replace(isnull(LAST_NM_1, '') + isnull(LAST_NM_2, '') + isnull(LAST_NM_3, '') + isnull(LAST_NM_4, ''), coalesce(LAST_NM_1, LAST_NM_2, LAST_NM_3, LAST_NM_4), '') = ''





                                  share|improve this answer

























                                  • Not following; ID is an identity column. It's unique. What does grouping by ID achieve?

                                    – Tab Alleman
                                    Mar 26 at 18:45











                                  • This is bringing back line 4: 4 SMITH BETA BETA GEORGE NULL which should not

                                    – sniperd
                                    Mar 26 at 18:45











                                  • Just saw that requirement. Give me a second.

                                    – Sean Lange
                                    Mar 26 at 18:45











                                  • @sniperd try now.

                                    – Sean Lange
                                    Mar 26 at 18:48











                                  • @TabAlleman it is just to force the aggregation so you don't have to UNPIVOT and then PIVOT.

                                    – Sean Lange
                                    Mar 26 at 18:50













                                  1












                                  1








                                  1







                                  Here is one way to do this.



                                  select ID
                                  , LAST_NM_ORIGINAL
                                  , LAST_NM_1
                                  , LAST_NM_2
                                  , LAST_NM_3
                                  , LAST_NM_4
                                  from LAST_NM
                                  where replace(isnull(LAST_NM_1, '') + isnull(LAST_NM_2, '') + isnull(LAST_NM_3, '') + isnull(LAST_NM_4, ''), LAST_NM_ORIGINAL, '') > ''
                                  AND replace(isnull(LAST_NM_1, '') + isnull(LAST_NM_2, '') + isnull(LAST_NM_3, '') + isnull(LAST_NM_4, ''), coalesce(LAST_NM_1, LAST_NM_2, LAST_NM_3, LAST_NM_4), '') = ''





                                  share|improve this answer















                                  Here is one way to do this.



                                  select ID
                                  , LAST_NM_ORIGINAL
                                  , LAST_NM_1
                                  , LAST_NM_2
                                  , LAST_NM_3
                                  , LAST_NM_4
                                  from LAST_NM
                                  where replace(isnull(LAST_NM_1, '') + isnull(LAST_NM_2, '') + isnull(LAST_NM_3, '') + isnull(LAST_NM_4, ''), LAST_NM_ORIGINAL, '') > ''
                                  AND replace(isnull(LAST_NM_1, '') + isnull(LAST_NM_2, '') + isnull(LAST_NM_3, '') + isnull(LAST_NM_4, ''), coalesce(LAST_NM_1, LAST_NM_2, LAST_NM_3, LAST_NM_4), '') = ''






                                  share|improve this answer














                                  share|improve this answer



                                  share|improve this answer








                                  edited Mar 26 at 18:56

























                                  answered Mar 26 at 18:43









                                  Sean LangeSean Lange

                                  27.2k2 gold badges20 silver badges35 bronze badges




                                  27.2k2 gold badges20 silver badges35 bronze badges












                                  • Not following; ID is an identity column. It's unique. What does grouping by ID achieve?

                                    – Tab Alleman
                                    Mar 26 at 18:45











                                  • This is bringing back line 4: 4 SMITH BETA BETA GEORGE NULL which should not

                                    – sniperd
                                    Mar 26 at 18:45











                                  • Just saw that requirement. Give me a second.

                                    – Sean Lange
                                    Mar 26 at 18:45











                                  • @sniperd try now.

                                    – Sean Lange
                                    Mar 26 at 18:48











                                  • @TabAlleman it is just to force the aggregation so you don't have to UNPIVOT and then PIVOT.

                                    – Sean Lange
                                    Mar 26 at 18:50

















                                  • Not following; ID is an identity column. It's unique. What does grouping by ID achieve?

                                    – Tab Alleman
                                    Mar 26 at 18:45











                                  • This is bringing back line 4: 4 SMITH BETA BETA GEORGE NULL which should not

                                    – sniperd
                                    Mar 26 at 18:45











                                  • Just saw that requirement. Give me a second.

                                    – Sean Lange
                                    Mar 26 at 18:45











                                  • @sniperd try now.

                                    – Sean Lange
                                    Mar 26 at 18:48











                                  • @TabAlleman it is just to force the aggregation so you don't have to UNPIVOT and then PIVOT.

                                    – Sean Lange
                                    Mar 26 at 18:50
















                                  Not following; ID is an identity column. It's unique. What does grouping by ID achieve?

                                  – Tab Alleman
                                  Mar 26 at 18:45





                                  Not following; ID is an identity column. It's unique. What does grouping by ID achieve?

                                  – Tab Alleman
                                  Mar 26 at 18:45













                                  This is bringing back line 4: 4 SMITH BETA BETA GEORGE NULL which should not

                                  – sniperd
                                  Mar 26 at 18:45





                                  This is bringing back line 4: 4 SMITH BETA BETA GEORGE NULL which should not

                                  – sniperd
                                  Mar 26 at 18:45













                                  Just saw that requirement. Give me a second.

                                  – Sean Lange
                                  Mar 26 at 18:45





                                  Just saw that requirement. Give me a second.

                                  – Sean Lange
                                  Mar 26 at 18:45













                                  @sniperd try now.

                                  – Sean Lange
                                  Mar 26 at 18:48





                                  @sniperd try now.

                                  – Sean Lange
                                  Mar 26 at 18:48













                                  @TabAlleman it is just to force the aggregation so you don't have to UNPIVOT and then PIVOT.

                                  – Sean Lange
                                  Mar 26 at 18:50





                                  @TabAlleman it is just to force the aggregation so you don't have to UNPIVOT and then PIVOT.

                                  – Sean Lange
                                  Mar 26 at 18:50











                                  1














                                  Your original query does have some superfluous predicates. Any row meeting LAST_NM_1 = LAST_NM_2 is guaranteed to have both LAST_NM_1 NOT NULL and LAST_NM_2 NOT NULL for example.



                                  But it can be made quite concise by using VALUES to get the 4 columns in tabular form as below.



                                  SELECT *
                                  FROM LAST_NM
                                  WHERE EXISTS (SELECT *
                                  FROM (VALUES(LAST_NM_1),
                                  (LAST_NM_2),
                                  (LAST_NM_3),
                                  (LAST_NM_4)) V(LAST_NM_N)
                                  HAVING MAX(LAST_NM_N) = MIN(LAST_NM_N) /*exactly one NOT NULL value among the 4 columns*/
                                  AND MAX(LAST_NM_N) <> LAST_NM_ORIGINAL)





                                  share|improve this answer





























                                    1














                                    Your original query does have some superfluous predicates. Any row meeting LAST_NM_1 = LAST_NM_2 is guaranteed to have both LAST_NM_1 NOT NULL and LAST_NM_2 NOT NULL for example.



                                    But it can be made quite concise by using VALUES to get the 4 columns in tabular form as below.



                                    SELECT *
                                    FROM LAST_NM
                                    WHERE EXISTS (SELECT *
                                    FROM (VALUES(LAST_NM_1),
                                    (LAST_NM_2),
                                    (LAST_NM_3),
                                    (LAST_NM_4)) V(LAST_NM_N)
                                    HAVING MAX(LAST_NM_N) = MIN(LAST_NM_N) /*exactly one NOT NULL value among the 4 columns*/
                                    AND MAX(LAST_NM_N) <> LAST_NM_ORIGINAL)





                                    share|improve this answer



























                                      1












                                      1








                                      1







                                      Your original query does have some superfluous predicates. Any row meeting LAST_NM_1 = LAST_NM_2 is guaranteed to have both LAST_NM_1 NOT NULL and LAST_NM_2 NOT NULL for example.



                                      But it can be made quite concise by using VALUES to get the 4 columns in tabular form as below.



                                      SELECT *
                                      FROM LAST_NM
                                      WHERE EXISTS (SELECT *
                                      FROM (VALUES(LAST_NM_1),
                                      (LAST_NM_2),
                                      (LAST_NM_3),
                                      (LAST_NM_4)) V(LAST_NM_N)
                                      HAVING MAX(LAST_NM_N) = MIN(LAST_NM_N) /*exactly one NOT NULL value among the 4 columns*/
                                      AND MAX(LAST_NM_N) <> LAST_NM_ORIGINAL)





                                      share|improve this answer















                                      Your original query does have some superfluous predicates. Any row meeting LAST_NM_1 = LAST_NM_2 is guaranteed to have both LAST_NM_1 NOT NULL and LAST_NM_2 NOT NULL for example.



                                      But it can be made quite concise by using VALUES to get the 4 columns in tabular form as below.



                                      SELECT *
                                      FROM LAST_NM
                                      WHERE EXISTS (SELECT *
                                      FROM (VALUES(LAST_NM_1),
                                      (LAST_NM_2),
                                      (LAST_NM_3),
                                      (LAST_NM_4)) V(LAST_NM_N)
                                      HAVING MAX(LAST_NM_N) = MIN(LAST_NM_N) /*exactly one NOT NULL value among the 4 columns*/
                                      AND MAX(LAST_NM_N) <> LAST_NM_ORIGINAL)






                                      share|improve this answer














                                      share|improve this answer



                                      share|improve this answer








                                      edited Mar 26 at 19:36

























                                      answered Mar 26 at 19:23









                                      Martin SmithMartin Smith

                                      359k61 gold badges604 silver badges712 bronze badges




                                      359k61 gold badges604 silver badges712 bronze badges





















                                          1














                                          Here is another way to do it



                                          SELECT * FROM
                                          #LAST_NM
                                          WHERE
                                          COALESCE(LAST_NM_1, LAST_NM_2, LAST_NM_3, LAST_NM_4) = COALESCE(LAST_NM_2, LAST_NM_3, LAST_NM_4,LAST_NM_1)
                                          AND COALESCE(LAST_NM_2, LAST_NM_3, LAST_NM_4,LAST_NM_1) = COALESCE(LAST_NM_3, LAST_NM_4,LAST_NM_1,LAST_NM_2)
                                          AND COALESCE(LAST_NM_3, LAST_NM_4,LAST_NM_1,LAST_NM_2) = COALESCE(LAST_NM_4,LAST_NM_1,LAST_NM_2,LAST_NM_3)
                                          AND (LAST_NM_ORIGINAL <> COALESCE(LAST_NM_1, LAST_NM_2, LAST_NM_3, LAST_NM_4))





                                          share|improve this answer

























                                          • This is incorrect. Consider a record where LAST_NM_1 = LAST_NM_2 = LAST_NM_3 <> LAST_NM_4. This query will output that faulty record.

                                            – bdebaere
                                            Mar 28 at 4:06











                                          • yep, sorry allready edited my awnser. thanks

                                            – Krone Torres
                                            Mar 29 at 2:11















                                          1














                                          Here is another way to do it



                                          SELECT * FROM
                                          #LAST_NM
                                          WHERE
                                          COALESCE(LAST_NM_1, LAST_NM_2, LAST_NM_3, LAST_NM_4) = COALESCE(LAST_NM_2, LAST_NM_3, LAST_NM_4,LAST_NM_1)
                                          AND COALESCE(LAST_NM_2, LAST_NM_3, LAST_NM_4,LAST_NM_1) = COALESCE(LAST_NM_3, LAST_NM_4,LAST_NM_1,LAST_NM_2)
                                          AND COALESCE(LAST_NM_3, LAST_NM_4,LAST_NM_1,LAST_NM_2) = COALESCE(LAST_NM_4,LAST_NM_1,LAST_NM_2,LAST_NM_3)
                                          AND (LAST_NM_ORIGINAL <> COALESCE(LAST_NM_1, LAST_NM_2, LAST_NM_3, LAST_NM_4))





                                          share|improve this answer

























                                          • This is incorrect. Consider a record where LAST_NM_1 = LAST_NM_2 = LAST_NM_3 <> LAST_NM_4. This query will output that faulty record.

                                            – bdebaere
                                            Mar 28 at 4:06











                                          • yep, sorry allready edited my awnser. thanks

                                            – Krone Torres
                                            Mar 29 at 2:11













                                          1












                                          1








                                          1







                                          Here is another way to do it



                                          SELECT * FROM
                                          #LAST_NM
                                          WHERE
                                          COALESCE(LAST_NM_1, LAST_NM_2, LAST_NM_3, LAST_NM_4) = COALESCE(LAST_NM_2, LAST_NM_3, LAST_NM_4,LAST_NM_1)
                                          AND COALESCE(LAST_NM_2, LAST_NM_3, LAST_NM_4,LAST_NM_1) = COALESCE(LAST_NM_3, LAST_NM_4,LAST_NM_1,LAST_NM_2)
                                          AND COALESCE(LAST_NM_3, LAST_NM_4,LAST_NM_1,LAST_NM_2) = COALESCE(LAST_NM_4,LAST_NM_1,LAST_NM_2,LAST_NM_3)
                                          AND (LAST_NM_ORIGINAL <> COALESCE(LAST_NM_1, LAST_NM_2, LAST_NM_3, LAST_NM_4))





                                          share|improve this answer















                                          Here is another way to do it



                                          SELECT * FROM
                                          #LAST_NM
                                          WHERE
                                          COALESCE(LAST_NM_1, LAST_NM_2, LAST_NM_3, LAST_NM_4) = COALESCE(LAST_NM_2, LAST_NM_3, LAST_NM_4,LAST_NM_1)
                                          AND COALESCE(LAST_NM_2, LAST_NM_3, LAST_NM_4,LAST_NM_1) = COALESCE(LAST_NM_3, LAST_NM_4,LAST_NM_1,LAST_NM_2)
                                          AND COALESCE(LAST_NM_3, LAST_NM_4,LAST_NM_1,LAST_NM_2) = COALESCE(LAST_NM_4,LAST_NM_1,LAST_NM_2,LAST_NM_3)
                                          AND (LAST_NM_ORIGINAL <> COALESCE(LAST_NM_1, LAST_NM_2, LAST_NM_3, LAST_NM_4))






                                          share|improve this answer














                                          share|improve this answer



                                          share|improve this answer








                                          edited Mar 29 at 2:11

























                                          answered Mar 26 at 21:49









                                          Krone TorresKrone Torres

                                          815 bronze badges




                                          815 bronze badges












                                          • This is incorrect. Consider a record where LAST_NM_1 = LAST_NM_2 = LAST_NM_3 <> LAST_NM_4. This query will output that faulty record.

                                            – bdebaere
                                            Mar 28 at 4:06











                                          • yep, sorry allready edited my awnser. thanks

                                            – Krone Torres
                                            Mar 29 at 2:11

















                                          • This is incorrect. Consider a record where LAST_NM_1 = LAST_NM_2 = LAST_NM_3 <> LAST_NM_4. This query will output that faulty record.

                                            – bdebaere
                                            Mar 28 at 4:06











                                          • yep, sorry allready edited my awnser. thanks

                                            – Krone Torres
                                            Mar 29 at 2:11
















                                          This is incorrect. Consider a record where LAST_NM_1 = LAST_NM_2 = LAST_NM_3 <> LAST_NM_4. This query will output that faulty record.

                                          – bdebaere
                                          Mar 28 at 4:06





                                          This is incorrect. Consider a record where LAST_NM_1 = LAST_NM_2 = LAST_NM_3 <> LAST_NM_4. This query will output that faulty record.

                                          – bdebaere
                                          Mar 28 at 4:06













                                          yep, sorry allready edited my awnser. thanks

                                          – Krone Torres
                                          Mar 29 at 2:11





                                          yep, sorry allready edited my awnser. thanks

                                          – Krone Torres
                                          Mar 29 at 2:11

















                                          draft saved

                                          draft discarded
















































                                          Thanks for contributing an answer to Stack Overflow!


                                          • Please be sure to answer the question. Provide details and share your research!

                                          But avoid


                                          • Asking for help, clarification, or responding to other answers.

                                          • Making statements based on opinion; back them up with references or personal experience.

                                          To learn more, see our tips on writing great answers.




                                          draft saved


                                          draft discarded














                                          StackExchange.ready(
                                          function ()
                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55363861%2fcompare-columns-only-when-not-null%23new-answer', 'question_page');

                                          );

                                          Post as a guest















                                          Required, but never shown





















































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown

































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown







                                          Popular posts from this blog

                                          Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                                          SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

                                          은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현