Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. How to avoid duplicate records while using a UNION - SOLVED

How to avoid duplicate records while using a UNION - SOLVED

Scheduled Pinned Locked Moved Database
helptutorialquestion
6 Posts 4 Posters 13 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Richard Andrew x64R Offline
    Richard Andrew x64R Offline
    Richard Andrew x64
    wrote on last edited by
    #1

    I have two SELECT statements joined with a UNION keyword. There's a problem if both SELECT statements return the same record thus causing it to appear twice in the result set. Is there any easy way to make the two SELECT statements mutually exclusive, so that if a record appears in the first one, it should not appear in the second one?

    SELECT Column1, Column2, (SUBQUERY1) [SOMEDATA]
    FROM TABLE1
    WHERE CRITERIA1 IS TRUE

    UNION

    SELECT Column1, Column2, (SUBQUERY2) [SOMEDIFFERENTDATA]
    FROM TABLE1
    WHERE CRITERIA2 IS TRUE

    SOLUTION: (This solution brought to you by rubber duck debugging.)

    SELECT Column1, Column2, COALESCE((SUBQUERY2), (SUBQUERY1)) [SOMEDATA]
    FROM TABLE1
    WHERE CRITERIA1 IS TRUE
    OR CRITERIA2 IS TRUE

    The difficult we do right away... ...the impossible takes slightly longer.

    Richard DeemingR A P 3 Replies Last reply
    0
    • Richard Andrew x64R Richard Andrew x64

      I have two SELECT statements joined with a UNION keyword. There's a problem if both SELECT statements return the same record thus causing it to appear twice in the result set. Is there any easy way to make the two SELECT statements mutually exclusive, so that if a record appears in the first one, it should not appear in the second one?

      SELECT Column1, Column2, (SUBQUERY1) [SOMEDATA]
      FROM TABLE1
      WHERE CRITERIA1 IS TRUE

      UNION

      SELECT Column1, Column2, (SUBQUERY2) [SOMEDIFFERENTDATA]
      FROM TABLE1
      WHERE CRITERIA2 IS TRUE

      SOLUTION: (This solution brought to you by rubber duck debugging.)

      SELECT Column1, Column2, COALESCE((SUBQUERY2), (SUBQUERY1)) [SOMEDATA]
      FROM TABLE1
      WHERE CRITERIA1 IS TRUE
      OR CRITERIA2 IS TRUE

      The difficult we do right away... ...the impossible takes slightly longer.

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      Depending on your data, COALESCE is probably not the right approach. You unconditionally execute subquery 2, and only execute subquery 1 if #2 returns Null. That's a change in behaviour from your original query. You should probably use a CASE statement instead:

      SELECT Column1, Column2, CASE WHEN CRITERIA1 IS TRUE THEN (SUBQUERY1) ELSE (SUBQUERY2) END As SomeData
      FROM TABLE1
      WHERE CRITERIA1 IS TRUE
      OR CRITERIA2 IS TRUE

      If you want to prioritize criteria 2 matches, then swap the case around:

      CASE WHEN CRITERIA2 IS TRUE THEN (SUBQUERY2) ELSE (SUBQUERY1) END As SomeData


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      Richard Andrew x64R 1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        Depending on your data, COALESCE is probably not the right approach. You unconditionally execute subquery 2, and only execute subquery 1 if #2 returns Null. That's a change in behaviour from your original query. You should probably use a CASE statement instead:

        SELECT Column1, Column2, CASE WHEN CRITERIA1 IS TRUE THEN (SUBQUERY1) ELSE (SUBQUERY2) END As SomeData
        FROM TABLE1
        WHERE CRITERIA1 IS TRUE
        OR CRITERIA2 IS TRUE

        If you want to prioritize criteria 2 matches, then swap the case around:

        CASE WHEN CRITERIA2 IS TRUE THEN (SUBQUERY2) ELSE (SUBQUERY1) END As SomeData


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        Richard Andrew x64R Offline
        Richard Andrew x64R Offline
        Richard Andrew x64
        wrote on last edited by
        #3

        Hi Richard, Thanks for your response. I should have specified that if NULL is returned from either of the subqueries, then the whole record should be ignored. I appreciate your analysis. Thanks!

        The difficult we do right away... ...the impossible takes slightly longer.

        1 Reply Last reply
        0
        • Richard Andrew x64R Richard Andrew x64

          I have two SELECT statements joined with a UNION keyword. There's a problem if both SELECT statements return the same record thus causing it to appear twice in the result set. Is there any easy way to make the two SELECT statements mutually exclusive, so that if a record appears in the first one, it should not appear in the second one?

          SELECT Column1, Column2, (SUBQUERY1) [SOMEDATA]
          FROM TABLE1
          WHERE CRITERIA1 IS TRUE

          UNION

          SELECT Column1, Column2, (SUBQUERY2) [SOMEDIFFERENTDATA]
          FROM TABLE1
          WHERE CRITERIA2 IS TRUE

          SOLUTION: (This solution brought to you by rubber duck debugging.)

          SELECT Column1, Column2, COALESCE((SUBQUERY2), (SUBQUERY1)) [SOMEDATA]
          FROM TABLE1
          WHERE CRITERIA1 IS TRUE
          OR CRITERIA2 IS TRUE

          The difficult we do right away... ...the impossible takes slightly longer.

          A Offline
          A Offline
          Andrea Simonassi
          wrote on last edited by
          #4

          Hi. You must explicitly use UNION ALL when you need duplicate. If you using UNION already remove duplicates.

          1 Reply Last reply
          0
          • Richard Andrew x64R Richard Andrew x64

            I have two SELECT statements joined with a UNION keyword. There's a problem if both SELECT statements return the same record thus causing it to appear twice in the result set. Is there any easy way to make the two SELECT statements mutually exclusive, so that if a record appears in the first one, it should not appear in the second one?

            SELECT Column1, Column2, (SUBQUERY1) [SOMEDATA]
            FROM TABLE1
            WHERE CRITERIA1 IS TRUE

            UNION

            SELECT Column1, Column2, (SUBQUERY2) [SOMEDIFFERENTDATA]
            FROM TABLE1
            WHERE CRITERIA2 IS TRUE

            SOLUTION: (This solution brought to you by rubber duck debugging.)

            SELECT Column1, Column2, COALESCE((SUBQUERY2), (SUBQUERY1)) [SOMEDATA]
            FROM TABLE1
            WHERE CRITERIA1 IS TRUE
            OR CRITERIA2 IS TRUE

            The difficult we do right away... ...the impossible takes slightly longer.

            P Offline
            P Offline
            PIEBALDconsult
            wrote on last edited by
            #5

            Howsabout... SELECT Column1, Column2, (SUBQUERY1) [SOMEDATA] FROM TABLE1 WHERE CRITERIA1 IS TRUE UNION SELECT Column1, Column2, (SUBQUERY2) [SOMEDIFFERENTDATA] FROM TABLE1 WHERE CRITERIA2 IS TRUE AND CRITERIA1 IS FALSE

            Richard Andrew x64R 1 Reply Last reply
            0
            • P PIEBALDconsult

              Howsabout... SELECT Column1, Column2, (SUBQUERY1) [SOMEDATA] FROM TABLE1 WHERE CRITERIA1 IS TRUE UNION SELECT Column1, Column2, (SUBQUERY2) [SOMEDIFFERENTDATA] FROM TABLE1 WHERE CRITERIA2 IS TRUE AND CRITERIA1 IS FALSE

              Richard Andrew x64R Offline
              Richard Andrew x64R Offline
              Richard Andrew x64
              wrote on last edited by
              #6

              Brilliant! I can't believe I didn't think of that. :laugh:

              The difficult we do right away... ...the impossible takes slightly longer.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups