One to Many Join causes Duplicates

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • kj

    One to Many Join causes Duplicates

    When I run the attached query, I get duplicates when there is one to
    many relationship between tableA and tableB. The query, tested schema
    and the result is attached. Sorry for the long post.

    Here is tested Schema and Data inserts.
    ----------------------
    create table TestTblA
    (ShipDate datetime,
    CPEID varchar(30),
    phonenum char(14))
    go
    create table TestTblB
    (CPEID varchar(30),
    itemID varchar(30),
    active char(1))
    go
    create table TestTblC
    (itemID varchar(30),
    descr varchar(50))
    go



    insert into TestTblA values (getdate(),'TWM UA','(408)-555-1211')
    insert into TestTblA values (getdate(),'TWM UA','(408)-555-1212')
    insert into TestTblA values (getdate(),'TWM UB','(408)-555-1211')
    insert into TestTblA values (getdate(),'TWM UB','(408)-555-1212')
    insert into TestTblA values (getdate(),'TWM UB','(408)-555-1213')
    insert into TestTblA values (getdate(),'TWM UC','(408)-555-1211')
    insert into TestTblA values (getdate(),'TWM UC','(408)-555-1212')
    insert into TestTblA values (getdate(),'TWM UC','(408)-555-1213')
    insert into TestTblA values (getdate(),'WWE XI','(408)-555-1211')
    insert into TestTblA values (getdate(),'WWE XI','(408)-555-1212')
    insert into TestTblA values (getdate(),'WWE XI','(408)-555-1211')


    insert into TestTblB values ('TWMUA','1000-000043-000','Y')
    insert into TestTblB values ('TWMUB','1000-100002-001','Y')
    insert into TestTblB values ('TWMUC','1000-200005-000','Y')
    insert into TestTblB values ('WWEXI','1000-401001-000','Y')
    insert into TestTblB values ('WWEXI','1000-401002-000','Y')


    insert into TestTblC values ('1000-000043-000','descrUA')
    insert into TestTblC values ('1000-100002-001','descrUB')
    insert into TestTblC values ('1000-200005-000','descrUC')
    insert into TestTblC values ('1000-401001-000','descrWW')
    insert into TestTblC values ('1000-401002-000','descrWW')

    ----------------Query follows------------
    SELECT A.ShipDate,A.CP EId,
    ItemId = CASE

    WHEN A.CPEId = 'TWMUA' THEN 'New - Single User'
    WHEN A.CPEID = 'TWMUB' THEN 'New - Multi User'
    WHEN A.CPEID = 'TWMUC' THEN 'New - Triple User'
    When B.ITEMID is NULL THEN 'Unknown'
    When B.ITEMID = ' ' THEN 'Unknown'
    else B.ItemId
    end,
    MODEL_NO = Case
    When B.ITEMID = '1000-000043-000' Then rtrim(C.DESCR)
    When B.ITEMID = '1000-100002-001' Then rtrim(C.DESCR)
    When B.ITEMID = '1000-200005-000' Then rtrim(C.DESCR)
    WHEN A.CPEId = 'TWMUA' THEN '1100'
    WHEN A.CPEID = 'TWMUB' THEN '1100'
    WHEN A.CPEID = 'TWMUC' THEN '1000SW'
    When C.DESCR is NULL THEN 'Unknown'
    else 'Unknown'
    end ,
    COUNT(A.phonenu m)
    FROM TestTblA A LEFT OUTER JOIN TestTblB B ON A.CPEID=B.CPEID and
    b.active = 'Y'
    LEFT OUTER JOIN TestTblC C ON B.ItemId=C.ITEM ID
    GROUP BY A.ShipDate,A.CP EId,B.ItemId,C. DESCR
    ORDER BY A.ShipDate,A.CP EId,B.ItemId,C. DESCR

    ---- end of query

    The result (modified the output format to fit a single line)
    ShipDate CPEId ItemId MODEL_NO Count
    2003-07-18 TWMUA New - Single User descrUA 2
    2003-07-18 TWMUB New - Multi User descrUB 3
    2003-07-18 TWMUC New - Triple User descrUC 3
    2003-07-18 WWEXI 1000-401001-000 NULL 3
    2003-07-18 WWEXI 1000-401002-000 NULL 3


    ** The problem **
    I need WWEXI or any similar entry to only show once, it shows twice.
    Thanks for your help.
  • Erland Sommarskog

    #2
    Re: One to Many Join causes Duplicates

    kj (kjaggi@hotmail .com) writes:[color=blue]
    > When I run the attached query, I get duplicates when there is one to
    > many relationship between tableA and tableB. The query, tested schema
    > and the result is attached. Sorry for the long post.
    >...
    > The result (modified the output format to fit a single line)
    > ShipDate CPEId ItemId MODEL_NO Count
    > 2003-07-18 TWMUA New - Single User descrUA 2
    > 2003-07-18 TWMUB New - Multi User descrUB 3
    > 2003-07-18 TWMUC New - Triple User descrUC 3
    > 2003-07-18 WWEXI 1000-401001-000 NULL 3
    > 2003-07-18 WWEXI 1000-401002-000 NULL 3
    >
    >
    > ** The problem **
    > I need WWEXI or any similar entry to only show once, it shows twice.
    > Thanks for your help.[/color]

    Thanks for the tables and sample data. However, I'm afraid that I don't
    understand what result you are desiring. WWEXI shows up twice, but your
    GROUP BY reads:

    GROUP BY A.ShipDate, A.CPEId, B.ItemId, C.DESCR

    So if there is more than one shipdate, itemid or description for any
    cpeid, it will show up twice. In the sample data, there are two different
    itemid. Which of them should be include in the output? And should the
    Count column be 3 or 6? And if there are two shipdates for the same cpeid,
    should there still only be one row?


    --
    Erland Sommarskog, SQL Server MVP, sommar@algonet. se

    Books Online for SQL Server SP3 at
    SQL Server 2025 redefines what's possible for enterprise data. With developer-first features and integration with analytics and AI models, SQL Server 2025 accelerates AI innovation using the data you already have.

    Comment

    • kj

      #3
      Re: One to Many Join causes Duplicates

      Right. Sorry about that. I need the first entry (ItemID) of the
      matching CPEID to show where there is more than one match between
      TestTblA and TestTblB. So for each day, I need a count for entries in
      TestTblA and pull the first matching entry for CPEID from TestTblB
      (itemID). So the count would be only 3 in this case but because there
      are 2 entries TestTblB, it duplicates them showing 3 entries for each
      day with different ItemID from TestTblB. I hope that helps. Sorry
      again, I didn't create the schema just pulling data. Thanks for your
      valuable input in these boards Erland, you have saved me a lot of
      time.

      Comment

      • Erland Sommarskog

        #4
        Re: One to Many Join causes Duplicates

        kj (kjaggi@hotmail .com) writes:[color=blue]
        > Right. Sorry about that. I need the first entry (ItemID) of the
        > matching CPEID to show where there is more than one match between
        > TestTblA and TestTblB. So for each day, I need a count for entries in
        > TestTblA and pull the first matching entry for CPEID from TestTblB
        > (itemID). So the count would be only 3 in this case but because there
        > are 2 entries TestTblB, it duplicates them showing 3 entries for each
        > day with different ItemID from TestTblB. I hope that helps. Sorry
        > again, I didn't create the schema just pulling data. Thanks for your
        > valuable input in these boards Erland, you have saved me a lot of
        > time.[/color]

        Here is a query which appears to give the result you are asking for.
        However, I like to stress that I know about nothing your real business
        problem, and this is a mere guess. You need to test this thoroughly, to
        see if you get the desired result.

        The change I have made is introduce a derived table. I don't know if
        you are acquainted with this feature in SQL, but this is a very powerful
        tool.


        SELECT A.ShipDate, A.CPEId,
        ItemId = CASE WHEN A.CPEId = 'TWMUA' THEN 'New - Single User'
        WHEN A.CPEID = 'TWMUB' THEN 'New - Multi User'
        WHEN A.CPEID = 'TWMUC' THEN 'New - Triple User'
        WHEN coalesce(B.ITEM ID, ' ') = ' ' THEN 'Unknown'
        ELSE B.ItemId
        END,
        MODEL_NO = CASE WHEN B.ITEMID = '1000-000043-000' THEN rtrim(C.DESCR)
        WHEN B.ITEMID = '1000-100002-001' THEN rtrim(C.DESCR)
        WHEN B.ITEMID = '1000-200005-000' THEN rtrim(C.DESCR)
        WHEN A.CPEId = 'TWMUA' THEN '1100'
        WHEN A.CPEID = 'TWMUB' THEN '1100'
        WHEN A.CPEID = 'TWMUC' THEN '1000SW'
        WHEN C.DESCR IS NULL THEN 'Unknown'
        ELSE 'Unknown'
        END,
        COUNT(A.phonenu m)
        FROM TestTblA A
        LEFT JOIN (SELECT CPEID, itemID = MIN(itemID)
        FROM TestTblB
        WHERE active = 'Y'
        GROUP BY CPEID) AS B ON A.CPEID = B.CPEID
        LEFT JOIN TestTblC C ON B.ItemId = C.ITEMID
        GROUP BY A.ShipDate, A.CPEId, B.ItemId, C.DESCR
        ORDER BY A.ShipDate, A.CPEId, B.ItemId, C.DESCR

        --
        Erland Sommarskog, SQL Server MVP, sommar@algonet. se

        Books Online for SQL Server SP3 at
        SQL Server 2025 redefines what's possible for enterprise data. With developer-first features and integration with analytics and AI models, SQL Server 2025 accelerates AI innovation using the data you already have.

        Comment

        Working...