SQL join filling null columns - sql

I have two tables where I need to do a FULL JOIN. Table2 has many missing values that can be found in table1.
I need to combine (if column1, 2 and 3 are the same) and append the information from table2 filling the null values from table1.
TABLE1
Column1
Column2
Column3
measure1
measure2
A
B
DAY1
50
null
A
B
DAY2
10
null
TABLE 2
Column1
Column2
Column3
measure1
measure2
A
B
DAY1
null
100
A
null
DAY3
null
300
DESIRED RESULT
Column1
Column2
Column3
measure1
measure2
A
B
DAY1
50
100
A
B
DAY2
10
null
A
B
DAY3
null
300
In this case, I combined first rows from table1 and table2. For second row in table2 we lookup the value of column2 from table1.1.

You can use FULL OUTER JOIN here:
select
coalesce(t1.id, t2.id) id,
coalesce(t1.c1, t2.c1) c1,
coalesce(t1.c2, t2.c2) c2
from t1
full outer join t2 on t1.id = t2.id;
SQLize fiddle

Related

Query to get oldest date into a column?

I have some tables that look like this:
Table1
Column1
A
B
C
Table2
column1 Id1 Id2
A 2 100
A 3 101
B 2 100
B 3 101
C 2 100
Table3
Id2 Date Item Status
100 10/20/17 A1 A
101 10/21/17 A1 A
100 11/22/17 A2 I
101 11/23/17 A2 A
My query looks like this:
Select
Date, *
FROM TABLE1 T1
LEFT JOIN Table2 T2 ON
T1.Column1 = T2.Column2
LEFT JOIN Table3 T3 ON
T2.Id2 = T3.Id2
I would like to return the oldest date on table 3 where the status is A into the date column in my query.
I was able to return the oldest date with this query but can't integrate this into my query with joins.
SELECT
MIN(DATE)
FROM Table3
WHERE Id2 IN (100,101)
AND STATUS = 'A'
group by Id2, ITEM
How can I get the oldest date into the query that uses the join clauses?
Couple of ways to approach what I believe that you're trying to do.
I'd say the easiest would be to simplify your search parameters with a subquery of Table 3.
select * from table2 t2
LEFT JOIN
(Select Id2, Item, max(Date)from Table 3
WHERE Status = 'A'
GROUP BY Id2, Item)a ON t2.Id2 = a.Id2;
The subquery does the filtering and then a quick join to table 2 would get all your data.

Return null for second duplicate row in JOIN in Access

I saw few posts for avoiding duplicates. But this is not about removing duplicates from the output. but keeping duplicates from one table but avoiding rows repeating from the joining table.
So I have 2 table Table1 and Table2 . Table1 has duplicate values for ID column. Table2 does not have duplicate values for ID column. I wish to join both tables. In the result I want all rows from Table1 including duplicates but i do not want Table2 to repeat rows for each duplicate row in Table1.
Table1
ID Column2
1 A
1 B
2 C
3 D
Table2
ID Column3
1 X
2 Y
My Query is
Select A.ID,A.Column2,B.ID,B.Column3 from Table1 A LEFT JOIN Table2 B on A.ID=B.ID
The result i get is
ID Column2 ID Column3
1 A 1 X
1 B 1 X
2 C 2 Y
3 D null null
The expected result is
ID Column2 ID Column3
1 A 1 X
1 B null null
2 C 2 Y
3 D null null
i could not see any possibilities in access database .
Please let me know if there is any possibility of getting this result
I can not check right now, but you will get the idea:
select t11.id, t11.col2, t2.id, t2.col3
from (table1 t11
left join (select id, min(col2) as col2 from table1 group by id) t12
on t11.id = t12.id and t11.col2 = t12.col2)
left join table2 t2 on t12.id = t2.id

How to get the records from multiple tables?

Hi I am new to the Database, and i am trying to get the records from the multiple tables, but depending upon there selection following is my tables
Table1
Column1 Column2
1 10
2 25
3 23
4 15
5 7
Table2
Column1 Column2
2 15
3 13
5 17
Table3
Column1 Column2
2 45
Resultant Table should have records like
Column1 Column2
1 10
2 45
3 13
4 15
5 17
i am trying but not got the output yet. Any help or the direction to work out this output will be great help.
UPDATE
What i want is get the all rows from table1 then if table2 contains the matching records then it will remove the matching records form the resultset and add the table2 matching records and then same is repeated by table3.
SELECT t1.column1, COALESCE(t3.column2,t2.column2,t1.column2)
FROM t1
LEFT JOIN t2 on t1.column1=t2.column1
LEFT JOIN t3 on t1.column1=t3.column1
Please use the Below Code and Try
select * from table1 where column1 not in ( select column1 from table2 union select column1 from table3)
union
select * from table2 where column1 not in (select column1 from table3)
union
select * from table3
select x.col1,max(x.col2) from (
select * from #t1
union
select * from #t2
union
select * from #t3
)x
group by x.col1
see it in action

Resolved: Query that returns the output where one column matches all the values in another column

Using oracle developer, I've run a query that results in the following table. But I only want the results where column1 matches all the values columns 2 (3,4,8). So the output would be 2, 3, but not 4. I'm sure there is a way to bring this result about without hard coding it? I'm thinking its some sort of self-join?
select column1, column2
from table1
where column1 in (
select column1
from table2
where depth >= 100)
order by column2;
Output:
column1 column2
3 2
8 2
4 2
3 3
4 3
8 3
4 4
Table2
Column1 Area_Name Depth
1 Lake 40
2 River 50
3 Ocean 150
4 Cliff 150
5 Mountain 90
6 Construction 60
7 Building 50
8 Random 100
9 Also Random 50
10 Another one 80
Needed output:
column2
2
3
Ok, this is what I was looking for:
SELECT table1.column1
FROM table1
INNER JOIN table2
ON table1.column2 = table2.column2
WHERE table2.depth >= 100
GROUP BY boat_id
HAVING COUNT(*) >= (
select count(*)
from table2
where depth >= 100);
UPDATED
WITH qry AS (
SELECT column1, column2
FROM table1
WHERE column1 IN (
SELECT column1
FROM table2
WHERE depth >= 100)
)
SELECT t1.column2
FROM qry t1 LEFT JOIN qry t2
ON t1.column1 = t2.column1 AND t1.column2 = t2.column2
GROUP BY t1.column2
HAVING COUNT(*) = (SELECT COUNT(DISTINCT column1) FROM qry)
ORDER BY t1.column2
Output:
| COLUMN2 |
-----------
| 2 |
| 3 |
SQLFiddle

Joining two tables in a select

I have two tables:
TABLE 1
ID VALUE
1 ABC
2 DEF
3 GHI
4 JKL
5 XYZ
TABLE 2
ID T1_ID VALUE
1 1 A
2 1 B
3 2 A
4 3 A
5 3 B
6 4 B
I want to select all rows from TABLE 1 which have a TABLE 2 row for Values A AND B.
This would be rows 1 and 3 (not 2 because it has only A, not 4 because it has only B).
Can I do this without a subquery?
(Note: I also need to query against values in table 1 so I can't just query table 2.)
Tadaaah! Without a subquery.
select distinct
t1.*
from
Table1 t1
inner join Table2 t2a on t2a.t1_ID = t1.ID and t2a.VALUE = 'A'
inner join Table2 t2b on t2b.t1_ID = t1.ID and t2b.VALUE = 'B'
SELECT t1.ID,
t1.VALUE
FROM Table1 t1
JOIN Table2 t2
ON t1.ID = t2.T1_ID
WHERE t2.VALUE IN ( 'A', 'B' )
GROUP BY t1.ID,
t1.VALUE
HAVING COUNT(DISTINCT t2.VALUE) = 2