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.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Join 3 tables

Author  Topic 

shamilton
Starting Member

2 Posts

Posted - 2009-08-12 : 12:45:01
I am having troubles joining 3 tables.

The tables are as follows.

Matter – contains a list of matter numbers which are unique in this table.
Client – contains a list of each client numbers and has a corresponding matter number which joins the client number to the matter table.
UDF – contains user defined fields for each matter number. There is a specific udfindex i want to join to the tables. This table will have a number multiple instances of the same matter number listed for each UDFindex and in some cases the udfindex record doesn’t exist for the matter number.

My code below seems to return a query of approximately 15000 records when it should be returning about 21000 record. The reason for this is i believe the the WHERE statement is looking in the UDF table for udfindex = 7. And in doing so i assume this is not including the matter numbers from the matter table if that matter doesn’t have a record with udfindex = 7 in the UDF table. Is there any way to include the matter and for the udvalue simply leave it blank if it doesn’t exist in the UDF table? Hope this makes sense. Thank you.

SELECT TOP 100 PERCENT dbo.matter.mmatter, dbo.client.clname1 AS clname, dbo.udf.udvalue
FROM dbo.matter LEFT OUTER JOIN
dbo.client ON dbo.matter.mclient = dbo.client.clnum
LEFT OUTER JOIN dbo.udf ON dbo.matter.mmatter = dbo.udf.udjoin
WHERE (dbo.matter.mclosedt > '01/01/2005') AND (dbo.udf.udfindex = '7')
ORDER BY dbo.matter.mclosedt

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-08-12 : 13:08:53
SELECT TOP 100 PERCENT dbo.matter.mmatter, dbo.client.clname1 AS clname, dbo.udf.udvalue
FROM dbo.matter LEFT OUTER JOIN
dbo.client ON dbo.matter.mclient = dbo.client.clnum
LEFT OUTER JOIN dbo.udf ON dbo.matter.mmatter = dbo.udf.udjoin AND dbo.udf.udfindex = '7'
WHERE (dbo.matter.mclosedt > '01/01/2005')
ORDER BY dbo.matter.mclosedt



No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

shamilton
Starting Member

2 Posts

Posted - 2009-08-12 : 13:22:25
Incredible. Thank you so much.
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-08-12 : 13:33:19
welcome


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page
   

- Advertisement -