SQL Not Exists include null value - sql-server

I have two SQL Server tables :
Table 1
Id name description version
----------------------------------
1 Book1 Book 1 Title v1
2 Book2 Book 2 Title v2
3 Book3 Book 3 Title NULL
4 Book5 Book 5 Title v3
Table 2
Id name description version
----------------------------------
1 Book1 Book 1 Title v1
2 Book2 Book 2 Title v2
3 Book3 Book 3 Title NULL
4 Book4 Book 4 Title NULL
5 Book5 Book 5 Title NULL
I want to select all data from table 2 that doesn't exist in table 1 so I can
insert them into another table.
Here's the SQL query :
SELECT t2.name, t2.description, t2.version
FROM Table2 AS t2
WHERE
NOT EXISTS (SELECT t1.name, t1.description, t1.version
FROM Table1 as t1
WHERE t2.name = t1.name
AND t2.description = t1.description
AND t2.version = t1.version)
The result expected is this :
Id name description version
-----------------------------------
4 Book4 Book 4 Title NULL
5 Book5 Book 5 Title NULL
But I'm getting this instead:
Id name description version
---------------------------------
3 Book3 Book 3 Title NULL
4 Book4 Book 4 Title NULL
Why the null value are not evaluated in my NOT EXIST statement and are displayed?

NULL value when compare with anything will return FALSE. You will need to use IS NULL to handle
select t2.name, t2.description, t2.version
from Table2 as t2
WHERE NOT EXISTS
(
SELECT *
FROM Table1 as t1
WHERE t2.name = t1.name
AND t2.description = t1.description
AND (
t2.version = t1.version
OR (t2.version IS NULL AND t1.version IS NULL)
)
)

Optimized Query:
SELECT t2.name, t2.description, t2.version
FROM Table2 AS t2
LEFT JOIN Table1 AS t1
ON t2.name = t1.name
AND t2.description = t1.description
AND (t2.version = t1.version
OR (t2.version IS NULL AND t1.version IS NULL))
WHERE t1.ID is NULL

you can use EXCEPT for this problem.
you can use a common field from two tables and then select your data from that result.
look this following code :
SELECT name, description, version FROM Table2
EXCEPT
SELECT name, description, version FROM Table1
this code give you data that exist in table2 that not exist in table1.

You can use simply ISNULL() function:
SELECT t2.name, t2.description, t2.version
FROM Table2 AS t2
WHERE
NOT EXISTS (SELECT t1.name, t1.description, t1.version
FROM Table1 as t1
WHERE t2.name = t1.name
AND t2.description = t1.description
AND ISNULL(t2.version,0) = ISNULL(t1.version,0)

Related

How to replace some rows in a SELECT query from another SELECT

I have two tables:
T1:
ID Department ATTRIBUTES TEAM
--- ---------- ---------- ------
1 R&D Dress_Code NULL
2 R&D Dress_Code Web
3 R&D Food System
4 R&D Food NULL
5 R&D Color NULL
6 Marketing Food System
T2:
ID VAL
--- ----------
1 Smart
2 Casual
3 Beef
4 Chicken
5 Green
6 Fish
The purpose of T1 is to show all the department attributes.
If the TEAM is null, it is for everyone in that department. Sometimes a team has special settings which override the generic settings.
For example, I want to get the settings as a 'Web' team in R&D.
I can write:
SELECT T1.DEPARTMENT, T1.ATTRIBUTES, T1.TEAM, T2.VAL
FROM T1
LEFT JOIN T2 ON T1.ID = T2.ID
WHERE T1.DEPARTMENT = 'R&D' AND T1.TEAM = 'Web'
This will show one record which says dress code is casual.
But I want the result to be:
ATTRIBUTES VAL
---------- ------
Dress_Code Casual
Food Chicken
Color Green
Similarly for the 'System' team in R&D, the result would be smart dress code, beef, and green color.
I'm thinking first select all R&D results and then replace the rows with the above select results.
I need to write this as a stored procedure.
Any help is much appreciated!
Using CTE and row_number() :
with CTE as(
select
ATTRIBUTES,
VAL,
T1.TEAM,
row_number() over (partition by ATTRIBUTES order by team desc) rn
from t1 t1
inner join t2 t2 on t1.ID =t2.ID
AND ( T1.TEAM = 'Web' or T1.TEAM is null )
)
select
ATTRIBUTES ,
VAL
from cte where rn=1
order by val
OutPut :
SELECT
T1.DEPARTMENT,
T1.ATTRIBUTES,
T1.TEAM,
T2.VAL
INTO #temp
FROM T1
INNER JOIN T2 ON T1.ID = T2.ID
WHERE T1.TEAM is NULL
SELECT
T1.ATTRIBUTES,
T2.VAL
INTO #t2
FROM T1
INNER JOIN T2 ON T1.ID = T2.ID
WHERE T1.TEAM = 'Web'
UPDATE t
SET t.VAL=b.VAL
FROM #temp t
join #t2 b on b.ATTRIBUTES=t.ATTRIBUTES
SELECT
DEPARTMENT,
ATTRIBUTES,
TEAM,
VAL
FROM #temp
Even this too will help.

Conditional Inner Join in Oracle

I have an issue regarding conditional inner join. Kindly have a look at the following sqlfiddle.
http://sqlfiddle.com/#!4/6dc88
Here i'm trying to get all the same name. but if in case the id is 1 then also get the same name and the name which has id of 0. Your help will be appriciated. thanks. here is the example
Table1
Id | Name
1 | A
2 | B
3 | C
Table2
Id | Name
1 | a
2 | b
3 | c
0 | d
Here is what i expect
A | a
B | b
C | c
A | d
Edit: Sorry i forget to mention my query, here is what i have tried so far..
select t1.name, t2.name from table1 t1
inner join table2 t2 on
CASE
WHEN t1.id = t2.id_copy and t1.id = 1 THEN
0
else
t1.id
END = t2.id_copy
Thanks in advance.
Assuming you mean that if table2.id is 0 then it should match with table1.id = 1, then this should do the trick:
with table1 as (select 1 id, 'A' name from dual union all
select 2 id, 'B' name from dual union all
select 3 id, 'C' name from dual),
table2 as (select 1 id, 'a' name from dual union all
select 2 id, 'b' name from dual union all
select 3 id, 'c' name from dual union all
select 0 id, 'd' name from dual)
select t1.name, t2.name
from table1 t1
inner join table2 t2 on (t1.id = case when t2.id = 0 then 1 else t2.id end);
NAME NAME_1
---- ------
A a
B b
C c
A d
If there's more complicated logic around what decides how non-matching (t1.id = t2.id) rows in table2 match with table1, then you'll have to explain the logic.
Personally I would prefer to use something simpler, like:
select t1.name, t2.name from table1 t1
inner join table2 t2
on t1.id = t2.id_copy or (t1.id = 1 and t2.id_copy = 0)

How to use SQL(Max) function

I have 2 tables
Table 1:
id name adress
1 John New York
2 Jane London`
... and so on
Table 2:
id fila date
1 43 01/01/2010
1 39 10/01/2011
1 55 23/12/2012
2 10 01/01/2008
2 15 02/02/2010`
.... and so on
I want to get data like this
id fila name adress date
-----------------------------------------
1 55 John New York 23/12/2012
2 15 Jane London 02/02/2010
..... and so on.
Thanks
ok. what you are really looking for is "What is the latest date in table2 for each of my rows in Table1". So to answer the question:
select *
From Table1
inner join (
select id, max(fila) as maxfila
from Table2
group by id
) as maxdates
on Table1.id = maxdates.id
inner join Table2 on Table2.id = maxdates.id AND Table2.fila = maxdates.maxfila
Try this:
;with cte as
(select id, max(fila) maxfila
from table2
group by id)
select t1.id, t1.name, t1.address, t2.fila, t2.date
from table1 t1
left join table2 t2 on t1.id = t2.id
inner join cte c on t1.id = c.id
where t2.fila = c.maxfila
Try this
Select t1.id, t1.name, t1.address, t2.maxfila
from table1 t1
left outer join
(select id, max(fila) maxfila
from table2
group by id) t2
select t1.id, t1.name t1.address, max(t2.fila),
(select top 1 date from table2 order by fila desc where table2.id = t1.id)
from table1 t1 inner join
table2 t2 on t1.id = t2.id

Case function in MS SQL

I have a table of format
id name flag
----------------
11 Jack 1
11 Jill 2
23 Joe 1
23 John 2
23 Sam 1
46 White 2
46 Dan 2
57 Dave 1
I am trying to return the table of format (based on the flag for particular id) like
id name_1 name_2
------------------
11 Jack Jill
23 Joe John
23 Sam John
46 NULL White
46 NULL Dan
57 Dave NULL
I tried this query but it does not give the expected result.
select id,
case flag when 1 then name end as name_1,
case flag when 2 then name end as name_2
from temp;
You want a join, not a select case. Something like this, assuming null is possible in either column:
SELECT a.id, a.name fname, b.name lname from
(SELECT id, name FROM temp WHERE flag = 1) a
full outer join
(SELECT id, name FROM temp WHERE flag = 2) b on a.id = b.id
Not sure what you expect in this case though:
46 NULL White
46 NULL Dan
I don't think you're going to be able to use case for this. This should do the trick, however:
select t1.id, t1.name as name_1, t2.name as name_2
from temp t1
inner join temp t2 on t1.id = t2.id and t1.flag = 1 and t2.flag = 2
I'm not sure you can get your partial result from the same query, so you could always do a union to find these...
select t1.id, t1.name as name_1, t2.name as name_2
from temp t1
inner join temp t2 on t1.id = t2.id and t1.flag = 1 and t2.flag = 2
union
select t1.id, t1.name as name_1, t2.name as name_2
from temp t1
left join temp t2 on t1.id = t2.id and t1.flag = 1 and t2.flag = 2
where t2.id is null
union
select t2.id, t1.name as name_1, t2.name as name_2
from temp t2
left join temp t1 on t1.id = t2.id and t1.flag = 1 and t2.flag = 2
where t1.id is null
This will get totally screwed up if there are 3 rows with the same ID :)

Limited T-SQL Join

This should be simple enough, but somehow my brain stopped working.
I have two related tables:
Table 1:
ID (PK), Value1
Table 2:
BatchID, Table1ID (FK to Table 1 ID), Value2
Example data:
Table 1:
ID Value1
1 A
2 B
Table 2:
BatchID Table1ID Value2
1 1 100
2 1 101
3 1 102
1 2 200
2 2 201
Now, for each record in Table 1, I'd like to do a matching record on Table 2, but only the most recent one (batch ID is sequential). Result for the above example would be:
Table1.ID Table1.Value1 Table2.Value2
1 A 102
2 B 201
The problem is simple, how to limit join result with Table2. There were similar questions on SO, but can't find anything like mine. Here's one on MySQL that looks similar:
LIMITing an SQL JOIN
I'm open to any approach, although speed is still the main priority since it will be a big dataset.
WITH Latest AS (
SELECT Table1ID
,MAX(BatchID) AS BatchID
FROM Table2
GROUP BY Table1ID
)
SELECT *
FROM Table1
INNER JOIN Latest
ON Latest.Table1ID = Table1.ID
INNER JOIN Table2
ON Table2.BatchID = Latest.BatchID
SELECT id, value1, value2
FROM (
SELECT t1.id, t2.value1, t2.value2, ROW_NUMBER() OVER (PARTITION BY t1.id ORDER BY t2.BatchID DESC) AS rn
FROM table1 t1
JOIN table2 t2
ON t2.table1id = t1.id
) q
WHERE rn = 1
Try
select t1.*,t2.Value2
from(
select Table1ID,max(Value2) as Value2
from [Table 2]
group by Table1ID) t2
join [Table 1] t1 on t2.Table1ID = t1.id
Either GROUP BY or WHERE clause that filters on the most recent:
SELECT * FROM Table1 a
INNER JOIN Table2 b ON (a.id = b.Table1ID)
WHERE NOT EXISTS(
SELECT 1 FROM Table2 c WHERE c.Table1ID = a.id AND c.BatchID > b. BatchID
)

Resources