Update oracle table by joining 3 tables - oracle

using the below query to update table A by joining with two other tables E and S.
The below query is throwing an error in the 'From' part.
Found 'FROM': Expecting:end-of-input -or- LOG -or- RETURN RETURINNG -or- WHERE -or- ,
UPDATE A
SET SEGMENT_TYPE_CUR = CASE
WHEN E.ID IS NOT NULL AND E.SEGMENT_1 IS NOT NULL THEN
E.SEGMENT_TYPE
ELSE
NULL
END,
CID_CUR = CASE
WHEN E.ID IS NOT NULL AND E.SEGMENT_1 IS NOT NULL THEN
E.CUST_ID
ELSE
NULL
END,
RID_CUR = CASE
WHEN E.ID IS NOT NULL AND E.SEGMENT_1 IS NOT NULL THEN
E.RID
ELSE
NULL
END,
SEGMENT_1_CUR = CASE
WHEN E.ID IS NOT NULL AND E.SEGMENT_1 IS NOT NULL THEN
E.SEGMENT_1
ELSE
S.SEGMENT_1
END,
SEGMENT_2_CUR = CASE
WHEN E.ID IS NOT NULL AND E.SEGMENT_1 IS NOT NULL THEN
E.SEGMENT_2
ELSE
S.SEGMENT_2
END,
SEGMENT_3_CUR = CASE
WHEN E.ID IS NOT NULL AND E.SEGMENT_1 IS NOT NULL THEN
E.SEGMENT_3
ELSE
S.SEGMENT_3
END FROM TEST a LEFT JOIN RESTATE E on a.ID = E.ID LEFT JOIN (SELECT *
FROM REFERENCE_SEGMENT
WHERE CURRENT_IND = 'Y'
AND SEG_IND = 'Y') S ON a.SUB_SEGMENT_CD = S.SEGMENT_REF_KEY
WHERE a.SEGMENT_1 = 'BIZ'
and TO_CHAR(a.update_date, 'yyyymm') = '202101'

I am creating a temp table by joining the source tables. Then using the temp table in my merge statement I am updating the target table.

Related

cast to value type 'System.Int32' failed because the materialized value is null

I have a Linq Query but it gives above error I guess due to NULL as I have Module,Block and semester as nullable int when semester column is having value than Module and Block would be null and when Module and block would be null than semster is having value How to handle the below query.
var test1 = (from c in db.StudentCoursesAssigned
join e in db.Years
on c.Year_Id equals e.Id into table1 from e in table1.DefaultIfEmpty()
join cc in db.Courses
on c.Course_Id equals cc.Course_Id into table2 from cc in table2.DefaultIfEmpty()
join g in db.grades
on c.Grade equals g.Id into table3 from g in table3.DefaultIfEmpty()
join m in db.Moduels
on c.Module_Id equals m.Id into table4 from m in table4.DefaultIfEmpty()
join p in db.Programs
on c.Program_Id equals p.Id into table5 from p in table5.DefaultIfEmpty()
join b in db.Blocks
on c.Block_Id equals b.Id into table6 from b in table6.DefaultIfEmpty()
join s in db.Semesters
on c.Semster_Id equals s.Semester_Id into table7 from s in table7.DefaultIfEmpty()
join ss in db.Students
on c.Student_id equals ss.Student_Id into table8 from ss in table8.DefaultIfEmpty()
select new
{
Student_Name=ss.Student_FName,
Course=cc.Course_Name,
Active=c.Active,
Course_start_date=c.Enrolment_Start,
Course_End_date=c.Enrolment_End,
Grade=g.Name,
Module=m.Name,
Program=p.Program_Title,
Year=e.Id,
Semester=s.Semester_Title,
blocks=b.Id
}).ToList();
That is the right Linq query which solves
var test1 = (from c in db.StudentCoursesAssigned
join e in db.Years
on c.Year_Id equals e.Id into table1 from e in table1.DefaultIfEmpty()
join cc in db.Courses
on c.Course_Id equals cc.Course_Id into table2 from cc in table2.DefaultIfEmpty()
join g in db.grades
on c.Grade equals g.Id into table3 from g in table3.DefaultIfEmpty()
join m in db.Moduels
on c.Module_Id equals m.Id into table4 from m in table4.DefaultIfEmpty()
join p in db.Programs
on c.Program_Id equals p.Id into table5 from p in table5.DefaultIfEmpty()
join b in db.Blocks
on c.Block_Id equals b.Id into table6 from b in table6.DefaultIfEmpty()
join s in db.Semesters
on c.Semster_Id equals s.Semester_Id into table7 from s in table7.DefaultIfEmpty()
join ss in db.Students
on c.Student_id equals ss.Student_Id into table8 from ss in table8.DefaultIfEmpty()
select new
{
Student_Name = ss.Student_FName == null ? "No Value" : ss.Student_FName,
Course = cc.Course_Name == null ? "No Value" : cc.Course_Name,
Active = c.Active ,
Course_start_date = c.Enrolment_Start,
Course_End_date = c.Enrolment_End ,
Grade = g.Name == null ? "No Value" : g.Name,
Module = m.Name == null ? "No value" : m.Name,
Program=p.Program_Title == null ? "No Value" : p.Program_Title,
Year=e.Name,
Semester=s.Semester_Title == null ? "No Value" : s.Semester_Title,
blocks=b.Name == null ? "No Value":b.Name,
student_Id=ss.Student_Id,
id=c.Id,
}).ToList();
```

Oracle update subquery of records

I need to update a list of record given by a subquery, sniffing around on the web I tried this structure:
UPDATE
(
SELECT
a.COL1
FROM
TABLE1 a,
TABLE2 b
WHERE
a.field1 = b.field1
) update_tbl
SET
update_tbl.COL1 = 'VALUE'
But it returns to me this Oracle error:
-> ORA-01779: Cannot modify a column which maps to a non key-preserved table
My query is the following one:
UPDATE
(
SELECT
imp.*
FROM table1 imp
JOIN table2 sp ON imp.id_p = sp.id_p
JOIN table3 cs ON sp.id_s = cs.id_s
JOIN table4 cb ON cb.id_c = cs.id_c
WHERE
imp.id_b = cb.id_b
AND (
(to_char(imp.p,'yyyymm') < to_char(cb.data_in,'yyyymm')) OR
(cb.data_fi IS NOT NULL AND to_char(imp.p,'yyyymm') > to_char(cb.data_fi,'yyyymm'))
)
and (
(imp.v is not null) or
(imp.v_s is not null and imp.v_s <> 0) or
(imp.imp_co is not null and imp.imp_co <> 0) or
(imp.imp_acc is not null and imp.imp_acc <> 0)
)
) i
SET
i.v = null,
i.v_s = 0,
i.imp_co = 0,
i.imp_acc = 0,
i.ID_S_CONT = 'N',
i.ID_T_COMP = 'P',
i.date_upd = SYSDATE,
i.user_upd = 'SeR'
The subquery return 82 rows (tested now), and I do want to modify only that rows, What am I doing wrong?
I think you are updating to imp table. so you can try MERGE like below
MERGE INTO
IMP A
USING
( SELECT
imp.*
FROM table1 imp
JOIN table2 sp ON imp.id_p = sp.id_p
JOIN table3 cs ON sp.id_s = cs.id_s
JOIN table4 cb ON cb.id_c = cs.id_c
WHERE
imp.id_b = cb.id_b
AND (
(to_char(imp.p,'yyyymm') < to_char(cb.data_in,'yyyymm')) OR
(cb.data_fi IS NOT NULL AND to_char(imp.p,'yyyymm') >
to_char(cb.data_fi,'yyyymm'))
)
and (
(imp.v is not null) or
(imp.v_s is not null and imp.v_s <> 0) or
(imp.imp_co is not null and imp.imp_co <> 0) or
(imp.imp_acc is not null and imp.imp_acc <> 0)
)) B
ON (A.ID =B.ID)
WHEN MATCHED THEN
UPDATE SET
A.v = null,
A.v_s = 0,
A.imp_co = 0,
A.imp_acc = 0,
A.ID_S_CONT = 'N',
A.ID_T_COMP = 'P',
A.date_upd = SYSDATE,
A.user_upd = 'SeR'
Will update the table given by subquery. here A.ID =B.ID use the primary key.

Oracle left join not working as expected

I'm having a problem with a left join. I have tow tables: Customers and WeeklyShop with each customer's weekly summary
I want to select all customers that did not buy in certain period of time.
My code
SELECT c.CUSTOMER_ID
FROM CUSTOMER c
left join (
SELECT distinct(w.CUSTOMER) as id
FROM WEEKLYSHOP w
WHERE w.WEEK_START >= beginDate
and w.WEEK_END <= endDate
) a
on c.CUSTOMER_ID = a.id
WHERE a.id = null
I was expecting this to exclude all the customers in the subquery, leaving me with the ones I need, but it's returning nothing.
If I run only the subquery, it returns the customers with data in that period correctly.
I hope I made myself clear, if there's something needed to add just tell me. Any help will be highly appreciated.
You should use IS NULL instead of = operator to check if a value is not set:
SELECT c.CUSTOMER_ID
FROM CUSTOMER c
left join (
SELECT distinct(w.CUSTOMER) as id
FROM WEEKLYSHOP w
WHERE w.WEEK_START >= beginDate
and w.WEEK_END <= endDate
) a
on c.CUSTOMER_ID = a.id
WHERE a.id IS null;
Example of NULL in PL/SQL context:
DECLARE
v_bool BOOLEAN;
BEGIN
v_bool := 1 = NULL;
IF NOT v_bool THEN -- same as IF v_bool = false THEN
dbms_output.put_line('False.');
ELSIF v_bool IS NULL THEN
dbms_output.put_line('NULL.');
END IF;
IF (NOT v_bool) IS NULL THEN
dbms_output.put_line('NOT v_bool is also NULL when v_bool is NULL.');
END IF;
END;
The output:
NULL.
NOT v_bool is also NULL when v_bool is NULL.

Dynamic query to dynamic data

I am new to the oracle database, I am trying to execute the following query
select o.id as ovaid ,
(case when(select count(m.cid) from ovamapper m where m.id = o.id and m.solutionid = 1)>0 then 1 else 0 end) as sol1,
(case when(select count(m.cid) from ovamapper m where m.id = o.id and m.solutionid = 2)>0 then 1 else 0 end) as sol1,
(case when(select count(m.cid) from ovamapper m where m.id = o.id and m.solutionid = 3)>0 then 1 else 0 end) as sol1 from ovatemplate o order by o.id
Instead of static values for solutionid , I would like to select it from other table.
Any help on this is really appreciated
you could use
join
to table that contain the solutionid. ex
Select * from ovatemplate JOIN solutiontable ON (solutiontable.ovaid=ovatempate.ovaid)
after that, change the static values to solutionid
Try this query
select o.id as ovaid ,
count(case when solutionid = 1 then m.cid else null end) as sol1 ,
count(case when solutionid = 2 then m.cid else null end) as sol2 ,
count(case when solutionid = 3 then m.cid else null end) as sol3
from ovamapper m , ovatemplate o
where m.id = o.id
group by o.id
order by o.id
If you dont need the aggregations as columns you should probably do that instead
select o.id as ovaid , solutionid , count(*) as sol
from ovamapper m , ovatemplate o
where m.id = o.id
and m.solutionid in (1,2,3)
group by o.id , solutionid
order by o.id

Oracle update Query does not work

I have an ORACLE query which does not work for this query.
UPDATE emp a set a.Enq_Status = 'PROGRESS'
where exists (select * from emp a, Data b
WHERE a.SA_Enq_Status is NULL
and a.EQ_Status = 'ACTIVATED'
and a.Activation_Return_Code = 0
and a.Alert_Code > 0
and a.User_Id = b.User_Id
and (b.Is_Associate is NULL or b.Is_Associate = 0)
and (b.Stk_Schd is NULL)
and (b.Stk_Dis_Amt is NULL)
);
My Problem is I want to update mutiple records in the table emp (A) to status to 'PROGRESS'.But when executing this query all records in a.Enq_status is changes.Please help me in this.Updation is not correct.Please help me in this
You have specified table emp both in the update query and the subquery too, Oracle will treat these as seperate tables.
You need a correlated subquery:
UPDATE emp a
set a.Enq_Status = 'PROGRESS'
where exists (select 'X'
from Data b
WHERE a.SA_Enq_Status is NULL
and a.EQ_Status = 'ACTIVATED'
and a.Activation_Return_Code = 0
and a.Alert_Code > 0
and a.User_Id = b.User_Id
and (b.Is_Associate is NULL or b.Is_Associate = 0)
and (b.Stk_Schd is NULL)
and (b.Stk_Dis_Amt is NULL));
You could probably just update the emp table without the need for the subquery though if you link to the Data table in the where clause...
Your update statement will update all the records in emp table because you don't specify the records to update. If your sub-query returns at least one row, the all the emp records will be updated. If it returns no rows, then none of the records will be updated.
Change your update like this:
UPDATE emp SET Enq_Status = 'PROGRESS'
WHERE id in
(SELECT a.id
FROM emp a, Data b
WHERE a.SA_Enq_Status is NULL and a.EQ_Status = 'ACTIVATED'
and a.Activation_Return_Code = 0 and a.Alert_Code > 0
and a.User_Id = b.User_Id and (b.Is_Associate is NULL or b.Is_Associate = 0)
and (b.Stk_Schd is NULL)and (b.Stk_Dis_Amt is NULL)
);
Try:
UPDATE emp a
SET a.enq_status = 'PROGRESS'
WHERE a.sa_enq_status IS NULL
AND a.eq_status = 'ACTIVATED'
AND a.activation_return_code = 0
AND a.alert_code > 0
AND EXISTS
(SELECT 'X'
FROM data b
WHERE a.user_id = b.user_id
AND ( b.is_associate IS NULL OR b.is_associate = 0 )
AND b.stk_schd IS NULL
AND b.stk_dis_amt IS NULL);

Resources