Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
kt
Yak Posting Veteran
88 Posts |
Posted - 2014-01-27 : 17:45:01
|
select a.[Cuno], [doc_type], [Doc_name]from [assembly] a left join [assembly_Doc] docON a.id = doc.assembly_idleft join [assembly_Recog] reON a.id = re.assembly_idwhere [activate] =1and doc.doc_type = 1 I only have two records for doc_type =1 in the [assembly_Doc] table. Without joining the third table [assembly_Recog], the results is corrected, 2 records returned. I need to join with [assembly_Recog] table, but when it joined, I got the duplicate records. It listed twice. pls see the results below and help.ThanksCusNo DocType DocNameCUS-022 1 B1489-01 Rev 1CUS-022 1 B1489-01 Rev 1CUS-022 1 B149-02 Rev 2CUS-022 1 B149-02 Rev 2 |
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2014-01-27 : 17:54:03
|
It helps if you provide sample data, but, based on what you said, there are two rows in the assembly_Recog table that match each of the IDs from the assembly table.A simple solution could be to add a DISTINCT to the select. But, it would probably be better to understand the data in the tables and why it is causing duplicates and join to the correct row. By, that I mean, what if there are two rows because that tables tracks changes. You would, probably, want to join to the most recent version (by date) to get the correct data from that table. |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2014-01-28 : 03:13:03
|
quote: Originally posted by kt select a.[Cuno], [doc_type], [Doc_name]from [assembly] a left join [assembly_Doc] docON a.id = doc.assembly_idleft join [assembly_Recog] reON a.id = re.assembly_idwhere [activate] =1and doc.doc_type = 1 I only have two records for doc_type =1 in the [assembly_Doc] table. Without joining the third table [assembly_Recog], the results is corrected, 2 records returned. I need to join with [assembly_Recog] table, but when it joined, I got the duplicate records. It listed twice. pls see the results below and help.ThanksCusNo DocType DocNameCUS-022 1 B1489-01 Rev 1CUS-022 1 B1489-01 Rev 1CUS-022 1 B149-02 Rev 2CUS-022 1 B149-02 Rev 2
the current way query is written it will effectively behave as an INNER JOIN as you've field from left joined table used in WHERE clauseselect a.[Cuno], [doc_type], [Doc_name]from [assembly] a left join [assembly_Doc] docON a.id = doc.assembly_idand doc.doc_type = 1 left join [assembly_Recog] reON a.id = re.assembly_idwhere [activate] =1 seehttp://weblogs.sqlteam.com/jeffs/archive/2007/05/14/criteria-on-outer-joined-tables.aspx------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
 |
|
|
|
|
|
|