Is there a way to be sure your union queries do not contain dups?

  • Hi,

    I wanted to find a way to check that the first table and the second table do not have any overlapping rows.

    Will a union see the duplicate if there is one and not dup it in the result, or will it just one reslult set on top of the other?

    select MemberID, HIC, ICD9 from ##DM_TblExcel60Joined2 UNION select MemberID, HIC, ICD9 from ##DM_SummaryReviewJoined2

    Thanks in advance

  • Union concatenates the two resultsets then eliminates duplicates. Union All just concatenates the two resultsets.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • A quick test will show you how this works. Run this code in your analyzer:

    ;WITH cte (cola, colb, colc) AS

    ( SELECT 1,2,3),

    cte2 (cola, colb, colc) AS

    (SELECT 1,2,3)

    SELECT

    cola,colb,colc

    FROM

    cte

    UNION ALL

    SELECT

    cola,colb,colc

    FROM

    cte2

    ;WITH cte (cola, colb, colc) AS

    ( SELECT 1,2,3),

    cte2 (cola, colb, colc) AS

    (SELECT 1,2,3)

    SELECT

    cola,colb,colc

    FROM

    cte

    UNION

    SELECT

    cola,colb,colc

    FROM

    cte2


    - Craig Farrell

    Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.

    For better assistance in answering your questions[/url] | Forum Netiquette
    For index/tuning help, follow these directions.[/url] |Tally Tables[/url]

    Twitter: @AnyWayDBA

  • Thank you both. Ran the code and makes perfect sense!!

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply