Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

sql joins

Posted on 2011-02-28
4
Medium Priority
?
271 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 15

Accepted Solution

by:
derekkromm earned 1200 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 400 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 400 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

609 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