473,320 Members | 1,694 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

One to Many Join causes Duplicates

kj
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(),'TWMUA','(408)-555-1211')
insert into TestTblA values (getdate(),'TWMUA','(408)-555-1212')
insert into TestTblA values (getdate(),'TWMUB','(408)-555-1211')
insert into TestTblA values (getdate(),'TWMUB','(408)-555-1212')
insert into TestTblA values (getdate(),'TWMUB','(408)-555-1213')
insert into TestTblA values (getdate(),'TWMUC','(408)-555-1211')
insert into TestTblA values (getdate(),'TWMUC','(408)-555-1212')
insert into TestTblA values (getdate(),'TWMUC','(408)-555-1213')
insert into TestTblA values (getdate(),'WWEXI','(408)-555-1211')
insert into TestTblA values (getdate(),'WWEXI','(408)-555-1212')
insert into TestTblA values (getdate(),'WWEXI','(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.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 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.phonenum)
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.ITEMID
GROUP BY A.ShipDate,A.CPEId,B.ItemId,C.DESCR
ORDER BY A.ShipDate,A.CPEId,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.
Jul 20 '05 #1
3 12892
kj (kj****@hotmail.com) writes:
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.


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, so****@algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #2
kj
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.
Jul 20 '05 #3
kj (kj****@hotmail.com) writes:
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.


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.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.phonenum)
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, so****@algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: robert | last post by:
running 8.1.7 server, 8.1.6 client. i *thought* inner join should not return nulls, but not only that, but i get way more rows than i'm expecting. assume: order table: order_number
4
by: thilbert | last post by:
All, I have a perplexing problem that I hope someone can help me with. I have the following table struct: Permission ----------------- PermissionId Permission
3
by: Dam | last post by:
Using SqlServer : Query 1 : SELECT def.lID as IdDefinition, TDC_AUneValeur.VALEURDERETOUR as ValeurDeRetour FROM serveur.Data_tblDEFINITIONTABLEDECODES def,...
8
by: xixi | last post by:
when i create a join view like this create view JV104FZ.APJTINM1 (APAM32, APNO20, APQY05, PONO01, PONO05, PONO19, POCD01, POCD13, systimestamp, loginname, id ) as select JV104FZ.APPTINM.APAM32,...
3
by: Martin | last post by:
Hello everybody, I have the following question. As a join clause on Oracle we use " table1.field1 = table2.field1 (+) " On SQL Server we use " table1.field1 *= table2.field1 " Does DB2...
10
by: Steve Atkins | last post by:
I have a large table (potentially tens or hundreds of millions of rows) and I need to extract some number of these rows, defined by an integer primary key. So, the obvious answer is select *...
5
by: asgars | last post by:
i have two tables, tab1 having N1 col and tab2 N2 col. now N1 is subset of N2. I need the information from tab2 (having N2) of all rows having the matching entry in N1 in tab1. For this i am...
18
by: Dave | last post by:
Guys I am really stuck on this one. Any help or suggestions would be appreciated. We have a large table which seemed to just hit some kind of threshold. They query is somewhat responsive when...
4
by: Jane T | last post by:
I appreciate how difficult it is to resolve a problem without all the information but maybe someone has come across a similar problem. I have an 'extract' table which has 1853 rows when I ask for...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.