Finding IDs in one table not in another - sql

I have two tables:
table1
ID | Name | Code
1 Joe 123
2 Sam 674
3 Mike 321
table2
ID | User Name| Code
1 Joe 123
2 Sam 674
3 Mike 321
4 John 457
5 Tim 235
Desired result:
4|John|457
5| Tim|235
Tabe1 and table2 code is identical. Table 1 code is a new field added thus contains no data for any record. Using the IDs as keys I took the codes from table2 and populated them in table1. However table1 has considerably less IDs then table2 so table2 has more codes then table1. I want to query which codes did not get transferred to table1. I thought it would be as simple as:
select *
from table2 t2
where t2.Code is not null and
t2.Code not in (select t1.Code from table1 t1 where t1.Code is not null);
This returns nothing which is strange to me. What do I need to adjust in this query? This is for oracle.

You might try the following:
SELECT t2.*
FROM table2 t2
WHERE NOT EXISTS ( SELECT 1 FROM table1 t1
WHERE t1.code = t2.code );

Related

Join two tables with switch case in order to avoid one to many join

I have two tables, t1 and t2.
Table t1:
Name address id
---- ------- --
rob 32 cgr 12
mary 31 lmo 42
tom axel St 2
Table t2:
ID Flag expense
-- ---- --------
12 Shop 1200
12 Educ 14000
42 educ 4000
Now I will have to create a table which will have attributes from t1 plus two more attributes that is expense in shop and expense in educ
Table t3
Name address id Shop_ex Educ_ex
---- ------- -- ------- -------
rob 32 cgr 12 1200 14000
mary 31 lmo 42 NULL 4000
tom axel st 2 NULL NULL
How to accomplish this?
I tried doing a left join t2 with switch case but it gives me multiple record as the join is becoming one to many.
select
t1.name, t1.address, t1.id,
case
when t2.flag = "shop" then t2.expense
else null
end as shop_ex
case
when t2.flag = "educ" then t2.expense
else null
end as educ_ex
from
t1
left join
t2 on (t1.id = t2.id)
It seems I will have to convert t2 table first before joining, to have a single record on the basis of flag. But I am not sure how to do that.
Please mind the tables are huge and optimized query will be nice.
Please suggest.
You only need to join the first table to the second one, twice:
SELECT t1.Name, t1.address, t1.id, t2a.expense AS Shop_ex, t2b.expense AS Educ_ex
FROM table1 t1
LEFT JOIN table2 t2a
ON t2a.ID = t1.id AND t2a.Flag = 'Shop'
LEFT JOIN table2 t2b
ON t2b.ID = t1.id AND t2b.Flag = 'Educ'
Demo

How to separate comma separate values and get aggregate of a column in SQL?

I have a table T1 as below
product_id val
123,567 5
999 4
999 3
and another table T2,
t_product_id // this maps to product_id in above table
123
999
In the final output, for t_product_id in table T2 I have to get value for it from T1. For duplicate product_ids (999) I want to get the min value, and for 123 I want to get 5
This is how output should look like
product_id value
123 5
999 3
My query ->
select t1.product_id, min(t1.value)
from T1 t1
group by t1.product_id
I am not sure what needs to be done next. How to separate comma separated values and check if 123 from T2 exists in T1 and get the value for it
it's not possible to keep only one product_id per row in table T1?
I think this would simplify matters for you. T1 would be:
123 | 5
567 | 5
999 | 4
999 | 3
Use join to only select the ids that exist in T2
select t1.product_id, min(t1.value)
from T1 t1 join T2 on (t1.product_id = t2.t_product_id)
group by t1.product_id

How to compare two tables and find the differences

I have two tables like below. I am using SQL DB.
Table1:
Id Name
1 leo
2 lin
3 kim
4 ken
5 jon
6 kelly
Table2:
Id Name Date
1 leo 2019-04-11
2 lin 2019-04-17
3 kim 2019-02-15
4 ken 2018-04-11
I am trying to compare this two table and find out which name in table1 does not have a Date.
I need this output.
Result:
Id Name
5 jon
6 kelly
Could anyone please help me with this query.
Use a LEFT JOIN:
SELECT * FROM Table1
LEFT JOIN Table2
ON Table1.Name = Table2.Name
WHERE Table2.Name is NULL
Or you can use WHERE NOT EXISTS:
SELECT * From Table1
WHERE NOT EXISTS (SELECT 1 FROM Table2 WHERE Table1.Name = Table2.Name)
Or you can use WHERE NOT IN:
SELECT * From Table1
WHERE Name NOT IN (SELECT Name FROM Table2)
Try this below:
SELECT Id,Name FROM Table1
EXCEPT
SELECT Id,Name FROM Table2

Selecting distinct rows based on column

A particular query of mine results data this way.
Id Size
123 1
123 1
123 2
123 2
134 1
134 1
134 2
I want the results get me the count eliminating the duplicate size like
Id Size
123 1
123 2
134 1
134 2
Above was result of joining two tables. Problem is I cant use distinct in this case.
Here is how tables are
Table1:
Id Created ... .. .. ..
123 date1 ....
134 date2 ....
Table2:
Id Size
123 1
123 2
134 1
134 2
I have my query that select from Table1 based on CreatedDate, its like this
select count(*)
from table1
join table2
on table1.id = table2.id
where table1.creates between '' and ''.
How do you get the distinct sizes.
If I use select count(distinct table2.size), it only returns 1 and 2 for all rows.
SELECT DISTINCT Id, Size
FROM table1
This should give you a list of distinct Id and Size combinations.
select count(distinct table1.id, table2.size)
from table1
join table2
on table1.id = table2.id
where table1.creates between '' and ''
see it working live in an sqlfiddle
Sometimes the solution is so obvious... :)
UPDATE: another way
select count(*) from (
select distinct table1.id, table2.size
from table1
join table2
on table1.id = table2.id
where table1.creates between '' and ''
) sq

SQL Server - How to display values that differ in two similar tables

I have 2 tables in SQL Server (ver:2008). Both tables have similar structure. Second table has few extra columns. I want to write a query that displays the differences in these tables (for matching fields). Output should display columns from both tables side by side (for comparison).
I tried using EXCEPT which is displaying the results from table 1 that don't match the results in table2. But I need to display both results side-by-side.
Thank you.
You simply need to select the fields you wish to display:
select t1.*, t2.*
from table1 t1
inner join table2 t2 on t1.ID = t2.ID
where t1.field1 <> t2.field1 OR t1.field2 <> t2.field2
OR ...
For example, if you have 2 tables, Products1 and Products2, both with columns Name, Price, Color etc., and you want to list all different products, you can use this:
SELECT Name FROM Products1
UNION
SELECT Name FROM Products2
OR, you can use FULL JOIN keyword, it returns rows when there is a match in one of the tables. For example:
The “Cars” table:
P_Id Name Color Quality City
---- -------- ----- ------- -----------
1 BMW Red good Las Vegas
2 Lexus Blue bad Los Angeles
3 Mercedes Green good MIami
The "Orders" table:
O_Id OrderNo P_Id
---- ------- ----
1 123 3
2 234 3
3 345 1
4 456 1
5 689 18
Now we want to list all the cars and all the people with their orders.
We use:
SELECT Cars.Name, Cars.Color, Orders.OrderNo
FROM Cars
FULL JOIN Orders
ON Cars.P_Id=Orders.P_Id
ORDER BY Cars.Name
The result:
Cars Color OrderNo
-------- ----- -------
BMW Red 345
BMW Red 456
Mercedes Green 123
Mercedes Green 234
Lexus Blue -
- - 689
Give this a shot. It assumes that there's one column (a) that is identical between the tables, and the rest (b, c, ...) are potentially different.
SELECT t1.a, t2.a, t1.b, t2.b, t1.c, t2.c, ...
FROM table1 t1
JOIN table2 t2
ON t1.a = t2.a
AND NOT (t1.b = t2.b AND t1.c = t2.c ...)
SELECT * FROM
(
SELECT * FROM TABLE1
EXCEPT
SELECT * FROM TABLE2
) T1
UNION ALL
SELECT * FROM
(
SELECT * FROM TABLE2
EXCEPT
SELECT * FROM TABLE1
) T2