Comparing two columns one of which is null - oracle

I have three tables as follow
TABLE1
ID VALUE
1 NULL
TABLE2
ID VALUE
1 1
TABLE3
ID VALUE
1 10
I'm trying to do the following:
Compare the two fields (ID) in tables 1 & 2 if one is null return null then return value from TABLE3.

It seems you are looking for something like this:
select table3.id, table3.value
from table1 join table2 on table1.id = table2.id
join table3 on table1.id = table3.id
where table1.value is null or table2.value is null
If this isn't what you had in mind, you need to give more details about your requirement.

Try this query:
SELECT t1.id,
(CASE WHEN t1.value IS NULL OR t2.value IS NULL
THEN t3.value
END) VALUE
FROM table_1 t1
JOIN table_2 t2
ON t1.id = t2.id
JOIN table_3 t3
ON t1.id = t3.id

Related

SQL Server 2012: Update table with inner join after sorted

I am using SQL Server 2012. I have a table called table1 like below:
Id col1 col2 col3 Name
1 a b abc null
2 b c mno null
And I have another table table2, like below:
Id col1 col2 col3 Name
1 % % abc Name1
2 a % abc Name2
3 % b abc Name3
4 a b abc Name4
I have to update Name column in Table1 From Name column in Table2 based on columns: col1, col2 and col3.
The Id = 1 in the table1 finds all 4 matches in the table because I am using like operator in col1 and col2 to compare(why I am using like is if it didn't find exact match it should accept % as a match).
Now my problem is if exact match is there for the columns col1, col2 and col3 in the table, it should consider that only not the rows with '%' value. For example, for the Id=1 in the table1, the result should be from id=4 in the table2.
I tried with following query:
UPDATE table1
SET name = t2.Name
FROM (SELECT TOP 1
t1.id, t2.name
FROM table1 t1
INNER JOIN table2 t2
ON t1.col3 = t2.col3 AND t1.col1 LIKE t2.col1
AND t1.col2 LIKE t2.col2
ORDER BY t2.col1, t2.col2) AS t3
WHERE id = t3.id;
But I am not getting result which I expected. And also, there are 8,000,000 records are there in table1 so it should not affect performance.
Please help to fix this issue.
At a first try, I suggest this:
UPDATE table1
SET name = t2.Name
FROM (SELECT TOP(1) *
FROM (SELECT
t1.id, t2.name, t2.col1, t2.col2, 2 As ord
FROM table1 t1
INNER JOIN table2 t2
ON t1.col3 = t2.col3 AND t1.col1 LIKE t2.col1
AND t1.col2 LIKE t2.col2
UNION ALL
SELECT
t1.id, t2.name, t2.col1, t2.col2, 1 As ord
FROM table1 t1
INNER JOIN table2 t2
ON t1.col3 = t2.col3 AND t1.col1 = t2.col1
AND t1.col2 = t2.col2
) DT
ORDER BY ord, col1, col2) AS t3
WHERE id = t3.id;

Want to change SQL query to LINQ

I want to change the below simple SQL query into LINQ , how do I change it ?
select * from table1 where isPaid = 'true' and Id in (select Id from table2 where EmployeeId = 12)
similar to this ?
from pa in db.PaymentAdvices
where pa.IsPaid == true
orderby pa.PaidDate descending
select pa;
Here code linq to sql:
from t1 in table1
join t2 in table2 on t1.Id equals t2.Id
where t2.EmployeeId = 12
select t1
Hope usefull!
if the field isPaid has datatype is Boolean:
from t1 in table1
join t2 in table2 on t1.Id equals t2.Id
where t2.EmployeeId = 12 and t1.isPaid == true
select t1
if the field isPaid has datatype is String:
from t1 in table1
join t2 in table2 on t1.Id equals t2.Id
where t2.EmployeeId = 12 and t1.isPaid.Equals("true")
select t1

ORACLE SQL: unable to left join 3 tables

How do I join 3 tables with left outer join? I was able to do left outer join between table1 and table2, but not table3.
I tried the following, but don't know how to join with table3.
select tab1.id, tab2.status, tab3.job_history
from table1 tab1
left outer join table2 tab2 on tab1.id=tab2.id
where tab1.job_title='accounting'
My table schemas are:
table 1:
id number(5) primary key,
status_code number(5),
job_title varchar2(20)
name varchar2(30)
table 2:
status_code number(5) primary key,
status varchar2(15)
table 3:
id number(5)
job_history varchar2(20)
Conditions:
table1.status_code can be null
table1.id may be not have any match for table3.id
I want to find the record in table1 that has table1.job_title = 'accounting' or in table3 has table3.job_history = 'accounting' when table1.id = table3.id and also get the table2 status with table1.status_code = table2.status_code
I suspect you're wanting to join table2 to table1 on status_code instead of id, as there is no ID column in table2. To join the third table, just add another LEFT OUTER JOIN (or any other JOIN).
select
tab1.id,
tab2.status,
tab3.job_history
from
table1 tab1
left outer join
table2 tab2 on tab2.status_code = tab1.status_code
left outer join
table3 tab3 on tab3.id = tab1.id
where
tab1.job_title='accounting' or tab3.job_history = 'accounting'
Since you have not same fields on all tables, to match your conditions, you probably will find easier way to run join like:
select
tab1.id,
tab2.status,
tab3.job_history
from
table1 tab1,
table2 tab2,
table3 tab3
where
tab1.job_title='accounting'
--ADD ADITIONAL FILTERING HERE
Query with join on 3 tables is look like this:
select
tab1.id,
tab2.status,
tab3.job_history
from
table1 tab1
left outer join
table2 tab2 on tab1.id=tab2.id
left outer join
table3 tab3 on tab3.id = tab1.id
where
tab1.job_title='accounting'
This SQL assumes that there is a one-to-one relationship between table 1, table 2 and table 3
select tab1.id, tab2.status, tab3.job_history
from table1 tab1
left outer join table2 tab2 on tab2.status_code = tab1.status_code
left outer join table3 tab3 on tab3.id = tab1.id
where tab1.job_title = 'accounting' or tab3.job_history = 'accounting'
Table 3 looks like it contains some form of job history, so it is likely there is going to more than one record in Table 3 for each record on Table 1. If this is the case you will need to perform a sub-query to find all people that currently or previously have a job title of accounting, i.e.
select tab1.id, tab2.status, tab1.job_title
from table1 tab1
left outer join table2 tab2 on tab2.status_code = tab1.status_code
where tab1.job_title = 'accounting' or
tab1.id in (select id from tab3 where job_history = 'accounting')

ORACLE : Update multiple columns error : Can not update string to NULL

I have a simple update stament as:
UPDATE table_1 t1 SET (t1.a, t1.b, t1.c) =
(SELECT t2.x, t2.y, t3.z
FROM table_2 t2, table_3 t3, table_4 t4
WHERE
1 = 1
AND t2.id = t3.id
AND t2.id1 = t4.id1
AND t2.name = t1.name
) WHERE EXISTS
(SELECT 1 FROM t2
WHERE
1 = 1
AND t2.name = t1.name
)
Inner SELECT query gives me results. But when I execute whole UPDATE query, it fails with error:
ORA-01407 "cannot update (%s) to NULL"
It also says that update on t1.a to NULL not possible. Please let me know, if I have made any mistake in this update query.
Please note that I can not use MERGE statement because there is a possibility that t2.name = t1.name condition may give me more than one entry from table t2. And in this case merge statement may fail with error can not get stable set of rows. If there is any other better appraoch let me know.
Update: t1.a has NOT NULL constraints. And my inner query returns results which do not have t2.x as NULL.
Problem can be that your SQL in SET part is more selective than SQL in EXITS. If so, change your exists to
... EXISTS ( SELECT 1 from
FROM table_2 t2, table_3 t3, table_4 t4
WHERE
t2.id = t3.id
AND t2.id1 = t4.id1
AND t2.name = t1.name
Also all your x,y, or z columns should have not null value if you a,b,c are defined as not null (all x should be not null if t1.a is not null etc)
I found the reason why I was getting that error. With condition t2.name = t1.name I was getting two rows in table table_1. For first row I had value t2.x from inner select query. For second row I did not. Thus I was getting the error. I modified my query as below:
UPDATE table_1 t1 SET (t1.a, t1.b, t1.c) =
(SELECT t2.x, t2.y, t3.z
FROM table_2 t2, table_3 t3, table_4 t4
WHERE
1 = 1
AND t2.id = t3.id
AND t2.id1 = t4.id1
AND t2.name = t1.name
) WHERE t1.id, t1.name IN
(SELECT t1.id, t1.name FROM table_2 t2, table_3 t3, table_4 t4
WHERE
1 = 1
AND t2.id = t3.id
AND t2.id1 = t4.id1
AND t2.name = t1.name
)
Hope it will help others.

How to use join on three tables

I have three tables
Table1 Table2 and Table3.
Table1 having column ID.
Table2 having column names ID,Name.
Table three having column name Name.
Now i want to retrive ID from table1 which is there in Table2 by so that the name associated with ID in table to should be in Table3.
Table1.ID=Table2.ID(Table2.Name=Table3.Namw).
Without using IN operator.Only joins.
select table1.id, table2.name
from table1
join table2 on table2.id = table1.id
join table3 on table3.name = table2.name
select distinct t1.ID
from Table1 t1
,Table2 t2
,Table3 t3
where t1.ID = t2.ID
and t2.Name = t3.Name
;
or
select t1.ID
from Table1 t1
where exists (
select 1
from Table2 t2
,Table3 t3
where t1.ID = t2.ID
and t2.Name = t3.Name
);

Resources