Solved

sql joins

Posted on 2011-02-28
4
264 Views
Last Modified: 2012-05-11
I need to join three tables. I want all 4 records from tableA and all matching records from tableB and tableC.  I end up with only 1 record.  In the end I only want 4 records and the data that matches.
SELECT   ui.ID, csct.description, csc.contact_type_acronym, csc.active, csc.date_added, ui.LastName + ', ' + ui.FirstName AS contactname, csct.acronym, csct.id as csctid
FROM         dbo.TableA AS csct LEFT OUTER JOIN   dbo.TableB AS csc ON csct.acronym = csc.contact_type_acronym 
LEFT OUTER JOIN dbo.TableC AS ui ON ui.ID = csc.contact_id
WHERE(csc.service_id = 196975) AND (csc.active = 1) AND (csct.ServiceLineID = '5') <-- this gives me the 4 records I need
ORDER BY csctid

Open in new window

0
Comment
Question by:lantervj
4 Comments
 
LVL 15

Accepted Solution

by:
derekkromm earned 300 total points
ID: 34997573
Adding items to the where clause that are based on tables that are left joined basically results in an inner join on those columns.

If you move the 2 "csc.service_id = ..." and "csc.active = 1" clauses to the join portion, it should return the desired results.

SELECT   ui.ID, csct.description, csc.contact_type_acronym, csc.active, csc.date_added, ui.LastName + ', ' + ui.FirstName AS contactname, csct.acronym, csct.id as csctid
FROM         dbo.TableA AS csct LEFT OUTER JOIN   dbo.TableB AS csc ON csct.acronym = csc.contact_type_acronym and csc.service_id = 196975 and csc.active = 1
LEFT OUTER JOIN dbo.TableC AS ui ON ui.ID = csc.contact_id
WHERE  (csct.ServiceLineID = '5') <-- this gives me the 4 records I need
ORDER BY csctid

Open in new window

0
 
LVL 22

Assisted Solution

by:Thomasian
Thomasian earned 100 total points
ID: 34997577
SELECT ui.ID, csct.description, csc.contact_type_acronym, csc.active, csc.date_added, ui.LastName + ', ' + ui.FirstName AS contactname, csct.acronym, csct.id as csctid
FROM dbo.TableA AS csct LEFT OUTER JOIN
     dbo.TableB AS csc ON csct.acronym = csc.contact_type_acronym 
                          AND (csc.service_id = 196975) AND (csc.active = 1) LEFT OUTER JOIN
     dbo.TableC AS ui ON ui.ID = csc.contact_id
WHERE csct.ServiceLineID = '5'
ORDER BY csctid

Open in new window

0
 
LVL 8

Assisted Solution

by:raulggonzalez
raulggonzalez earned 100 total points
ID: 34997605
Hi,

Your problem cannot be the joins, the solution should be in the WHERE clause because you reference there

(csc.service_id = 196975) AND (csc.active = 1)

which don't belong to TableA csct ...

Do SELECT * and check manually the values to see it better.


Cheers
0
 

Author Closing Comment

by:lantervj
ID: 34997864
Fast, accurate results.  I like it.
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question