简体   繁体   中英

SQL Query Left Outer Join To Current Query

I have a query and I want to look up the values from other table as a reference but not mess up my current query results. I think I have to use a Outer Left Join, but not sure how to incorporate that with my current query.

My current query looks similar to this:

SELECT a.primary_key,
       a.phase,
       b.project_number,
       c.LENGTH,
       d.color
  FROM TableA a,
       TableB b,
       TableC c,
       TableD d
 WHERE     c.primary_key = a.PROJECT_ID
       AND b.primary_key = a.PROJECT_ID
       AND b.primary_key = d.project_ID
       AND (c.date IS NULL OR c.number IS NULL)
       AND d.color IN ('black','red','blue')
ORDER BY 1

Now, that gives me a table of 50 results. 'TableContacts' has the look up value to my b.project_number. So say my table of 50 results, only 10 of them have b.project_number, I need the lookup values from 'TableContacts' to also show in my results, but I don't want that to affect my results and cut it down to 10, I still need my original 50 results, just with that additional information. Help?

Just add the CONTACTS table to your joins:

SELECT a.primary_key,
       a.phase,
       b.project_number,
       c.LENGTH,
       d.color, 
       ct.lookup_value --<< this is from the CONTACTS table
FROM TableA a
  JOIN TableB b ON b.primary_key = a.PROJECT_ID
  JOIN TableC c ON c.primary_key = a.PROJECT_ID
  JOIN TableD d ON b.primary_key = d.project_ID
  LEFT JOIN contacts ct ON ct.some_column = b.project_Number --<< this is the outer join to the CONTACTS table
WHERE (c.date IS NULL OR c.number IS NULL)
  AND d.color IN ('black','red','blue')
ORDER BY 1

As you obfuscated your table and column names it's hard to guess how exactly the join condition on the CONTACTS table should look like.

You could use this approach:

Create a table with the results of your current query:

create table t1 as 
SELECT a.primary_key,
       a.phase,
       b.project_number,
       c.LENGTH_col,
       d.color
FROM a,b,c,d
WHERE c.primary_key = a.PROJECT_ID
       AND b.primary_key = a.PROJECT_ID
       AND b.primary_key = d.project_ID
       AND (c.date_col IS NULL OR c.number_col IS NULL)
       AND d.color IN ('black','red','blue')
ORDER BY 1;

Use a union to get the desired results:

select t1.primary_key, t1.phase, t1.project_number, t1.length_col, t1.color, TableContacts.lookup_column
from t1, TableContacts
where t1.project_number = TableContacts.project_number
UNION
select t1.primary_key, t1.phase, t1.project_number, t1.length_col, t1.color, null
from t1 where t1.project_number is null;

Illustration by creating dummy data:

select * from a;
PRIMARY_KEY | PROJECT_ID | PHASE
1   100 Phase-1
2   200 Phase-2
3   300 Phase-3
4   400 Phase-4
5   500 Phase-5

select * from b;
PRIMARY_KEY | PROJECT_NUMBER
100 null
200 2000
300 3000
400 null
500 5000

select * from c;
PRIMARY_KEY | NUMBER_COL | LENGTH_COL | DATE_COL
100 null    99  null
200 null    99  null
300 null    99  null
400 null    99  null
500 null    99  null

select * from d;
PROJECT_ID  | COLOR
100 black
200 red
300 blue
400 black
500 yellow

select * from TableContacts;
PROJECT_NUMBER  | LOOKUP_COLUMN
1000    l-1000
2000    l-2000
3000    l-3000
4000    l-4000
5000    l-5000

Existing query in question returns this:

SELECT a.primary_key,
       a.phase,
       b.project_number,
       c.LENGTH_col,
       d.color
FROM a,b,c,d
WHERE c.primary_key = a.PROJECT_ID
       AND b.primary_key = a.PROJECT_ID
       AND b.primary_key = d.project_ID
       AND (c.date_col IS NULL OR c.number_col IS NULL)
       AND d.color IN ('black','red','blue')
ORDER BY 1;

PRIMARY_KEY | PHASE | PROJECT_NUMBER | LENGTH_COL | COLOR
1   Phase-1 null    99  black
2   Phase-2 2000    99  red
3   Phase-3 3000    99  blue
4   Phase-4 null    99  black

The goal is to populate the lookup_column where project_number is not null. Running the union query provided at start of answer:

select t1.primary_key, t1.phase, t1.project_number, t1.length_col, t1.color, TableContacts.lookup_column
from t1, TableContacts
where t1.project_number = TableContacts.project_number
UNION
select t1.primary_key, t1.phase, t1.project_number, t1.length_col, t1.color, null
from t1 where t1.project_number is null;

PRIMARY_KEY | PHASE | PROJECT_NUMBER | LENGTH_COL | COLOR | LOOKUP_COLUMN
1   Phase-1 null    99  black   null
2   Phase-2 2000    99  red     l-2000
3   Phase-3 3000    99  blue    l-3000
4   Phase-4 null    99  black   null

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM