SQL - using JOIN while filling missing values with NULL - sql

The most related question I looked into was this but sadly I did not get a solution for my problem there.
I have two tables, both have a similar column. The only difference is that one column is missing a few values. I want to join the tables, so that for the missing value in one column, the join will show the missing values.
Ill provide an example since this might be confusing -
table 1 table 2
ID count ID count
1 9 1 2
2 2 2 1
3 1
I want the result to be
table 3
ID count2 count1
1 2 9
2 1 2
3 NULL 1
However, using LEFT OUTER JOIN I could only achieve the table "table 3" without the row for id 3, because it has no representation in table 2.
Can you help me with my problem?

A left join would work for your sample data, I'm guessing you want to know what to do if you move the row with id 3 into table 2 so that your query will show all ids. To show all rows from both tables, use a FULL OUTER JOIN:
SELECT CASE WHEN t1.id IS NULL THEN t2.id ELSE t1.id END AS id,
t2.count as count2, t1.count as count1
FROM t1
FULL OUTER JOIN t2 ON t2.id = t1.id

Related

Multiple table join query - IDs and data tables

SOLVED turns out it was my where clause which was throwing off the results, I changed this out and added the where clause to the ON statement
I need some help.
I have a table with 25 million IDs and 4 tables with IDs and data. I need to create a new table with these 25 million IDs as well as the associated table data from the 4 tables. Each data table will not contain the full 25 million IDs. So as an example;
ID Table:
ID
A
B
Table 1
ID
measure_a
measure_b
B
1
3
Table 2
ID
measure_f
measure_g
A
3
4
etc..
Expected output:
ID
measure_a
measure_b
measure_f
measure_g
A
3
4
NULL
NULL
B
NULL
NULL
1
3
The most important thing is the 25 million IDs are in the final table. I've tried multiple joins but end up with a hugely reduced number of IDs which I believe is due to the IDs which don't match on the join condition being filtered out.
Any help is greatly appreciated.
You would use left joins:
select ids.id, t1.measure_a, t1.measure_b, t2.measure_f, t2.measure_g
from ids left join
table1 t1
on ids.id = t1.id left join
table2 t2
on ids.id = t2.id;

Count when value is in a column in another table

I have two tables
id
val
1
a
1
b
1
c
2
d
2
e
3
f
and
id
1
2
What I want is a count of the number of times an ID appears from the first table ONLY IF if it exists in the second table. How can I do this?
Example output:
id
count
1
3
2
2
3
0
Would you like to show ids that do not exist in the first table?
I made it show according to the ids that exist in the first table, if you want it to show up please comment below
select tb7.id, COUNT(tb6.id) as count
from Table_6 tb6 inner join Table_7 tb7 on tb6.id = tb7.id
group by tb7.id
You can use left outer join and count as follows:
Select t1.id, count(t2.id) as cnt
From table1 t1 left join
table2 t2
on t1.id = t2.id
Group by t1.id;

Duplicate rows in left join

I have 2 tables. There are about 100000 of null in one column, other values are integer, total values are about 200000. Another table has only the integer value. When I use the left join on this column, it gave me a lot of duplicates rows. Is it ok to use left join here?
Table 1:
Column 1
2
3
5
null
null
Table 2:
Column 1
1
2
3
so on
Your example is really odd. Why would anyone have null values in an ID field? But anyway.
If you need fields from table 2 in the resultset as you say above then you must use an INNER JOIN not a LEFT JOIN
Something like:
SELECT DISTINCT a.id, a.name, b.someOtherField
FROM Table1 a
INNER JOIN Table2 b ON a.id = b.id
Please note: Since only the ID field of table 1 has null values there will be no records selected from table 1 with id IS NULL because they have no equivalent in table 2. Adding the DISTINCT keyword helps in case this query would still produce duplicates.

How to create query who will be sum or not values from tables

I have table like this
Table1
ID Name Value fk_table
1 edd 3 1
2 tom 1 2
3 emi 2 NULL
And second table
Table2
ID VALUE
1 3
2 3
How to create query who will be sum value from table1 and table2 if is a foreign key, or will output only value from table1 if isn't fk
return $this->getEntityManager()
->createQuery('SELECT t1.name, SUM(t1.value as value +t2.value)
FROM AcmeBlogBundle:Table1 t1
LEFT JOIN t1.table2 t2')
->execute();
I want result like this
edd 6
tom 5
emi 2
I use doctrine and Symfony2
1 - You haven't specified the conditions you are joining on
2 - SUM function does not work like that - it sums aggregate on whole column, you just need a +
3 - Using COALESCE to ensure the missing values are replaced with 0
SELECT T1.Name, COALESCE(T1.VALUE,0) + COALESCE(T2.VALUE,0)
FROM Table1 T1
LEFT JOIN Table2 T2 ON T1.fk_table = T2.ID
SQLFiddleDemo (SQL Server in example, but should also work fine with other SQL flavors)
Your problem is that NULL 'zaps the life out of everything'. Adding it to any other value results in NULL. You should use COALESCE(t2.value, 0) to ensure it results a valid integer that doesn't interfere with the sum.

How can I JOIN tables conditionally?

I am working with a badly designed database and I ran into some problem.
I have two tables that I need to join on a 'not unique ID'.
The software that currently works with the database sets a '0 value' in the id if there is an error. This means if I try to join them a massive amount of records is joined on the 0 values.
Underneath an example of two tables and their not unique id fields i want to join them on
tbl1 tbl2
-----------
2 2
3 6
4 5
0 3
0 4
6 0
5 0
----------
What I want to achieve is this
tbl1 tbl2
-----------
2 2
3 3
4 4
0 * (no join)
0 * (no join)
6 6
5 5
----------
In other words, I don't want the '0 values' from tbl1 to join with all other '0 values' from tbl2. I still want to have the record tbl1 without a join though.
Is this possible in 1 query?
extra information: SQL SERVER 2005 and there is no option to make the ID's unique.
You did not show us the full structure of the tables so I need to make up some columns, but it basically goes like this:
select *
from tbl1
join tbl2
on tbl1.id = tbl2.t1_id
and tbl1.that_flag <> 0
Note the and condition that is part of the join condition.
This should do the trick
SELECT * FROM tbl1
LEFT JOIN tbl2 ON tbl1.value = tbl2.value and tbl2.value <> 0
this will give null values on the two that you put as *
SELECT tbl1.id, tbl2.id
FROM dbo.tbl1
LEFT OUTER JOIN dbo.tbl2
ON tbl1.id = NULLIF(tbl2.id, 0);