Solved

sql joins

Posted on 2011-02-28
4
258 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Fast, accurate results.  I like it.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

762 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now