473,666 Members | 2,225 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(),'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.
Jul 20 '05 #1
3 12905
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.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, 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
4448
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
4861
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
10046
by: Dam | last post by:
Using SqlServer : Query 1 : SELECT def.lID as IdDefinition, TDC_AUneValeur.VALEURDERETOUR as ValeurDeRetour FROM serveur.Data_tblDEFINITIONTABLEDECODES def, serveur.Data_tblTABLEDECODEAUNEVALEUR TDC_AUneValeur where def.TYPEDETABLEDECODES = 4
8
4165
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, JV104FZ.APPTINM.APNO20, JV104FZ.APPTINM.APQY05, JV104FZ.APPTINM.PONO01, JV104FZ.APPTINM.PONO05, JV104FZ.APPTINM.PONO19, COALESCE(JV104FZ.POPTOL.POCD01, ' '), COALESCE(JV104FZ.POPTOL.POCD13, ' '), JV104FZ.POPTOL.systimestamp,...
3
19470
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 have the same type of operator, without using the OUTER JOIN syntax ?
10
2681
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 * from table where id in (1,3,4); But I may want to extract a large number of rows, many thousands
5
32350
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 using Inner Join on cols N1 and n2. But result is giving duplicate rows. Can anyone suggest how do u i remove those duplicate rows? or may be a better way to do the above work... Thanks
18
5030
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 there are NO indexes on the table. However, when we index email the query takes forever. FACTS - The problem is very "data specific". I can not recreate the
4
14265
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 all rows where period_ = 3. The allocation table for info has 210 rows. I have two scripts below. The first script where I specify a period on a join, brings back 1853 lines and works. The second script where I specify the period in the...
0
8356
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8866
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8550
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8639
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6192
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5663
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4198
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4366
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1772
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.