SQL query update conditional - sql

I have 2 tables
aaa
on_date datetime null,
channel_id numeric(10,0) null,
type_id numeric(10,0) null,
amount numeric(10,0) null
bbb
channel_id numeric(10,0) null
type_id numeric(10,0) null
I want to do an update to aaa.amount to 0 of
only the common channel_ids and type_ids when bbb.type_id is not
null
or
of only the common channel_ids and ALL the type_ids when the
bbb.type_id is null
aaa
2014-09-13 1 3 12
2014-09-13 1 4 16
2014-09-13 2 1 11
bbb (case 1. )
1 3
bbb (case 2. )
1 null
In first case only the first aaa record should have amount = 0
In the second case the first 2 records of aaa should have amount = 0
Thank you

Try this:
update a
set a.amount=0
from aaa a inner join bbb b
on a.channel_id=b.channel_id
where ((a.channel_id=b.channel_id and a.type_id=b.type_id and b.type_id IS NOT NULL)
OR (a.channel_id=b.channel_id and b.type_id IS NULL))

you need to somthing like following as per i understand your question
UPDATE
aa
SET
aa.amount = 0
FROM
aaa aa
JOIN
bbb bb ON aa.channel_id =bb.channel_id
WHERE
(aa.type_id = bb.type_id AND bb.type_id is not null )
OR
(bb.type_id is null)
Hope it will help to you.

Related

Oracle where clause condition not executing with subselect

I have a query as follows: Column c has a case statement for case 'P' and case 'M'.
The condition inside case 'P' is not working-> "and cs.case_id not in (SELECT case_id FROM case_status where (case_status_id = 16 and case_status_date < sysdate - 365)) AND cs.case_status_id = lk.id".
case Status for P is one to many so it's in a different table and for 'M' it's one to one so it's in the same cases table.
Also case status 'P' may or may not have record for a case.
SELECT
c.case_id,
c.id id,
lkp_alt.descr AS name,
(CASE WHEN c.case_type = 'P' THEN
(SELECT listagg(lk.descr, ', ') AS status
FROM case_status cs,
lkp_case_status lk
WHERE c.case_id = cs.case_id
and cs.case_id not in (SELECT case_id
FROM case_status
where (case_status_id = 16
and case_status_date < sysdate - 365)
)
AND cs.case_status_id = lk.id
GROUP BY c.case_id
)
ELSE (SELECT listagg(status, ', ') AS status
FROM (SELECT case_id,
lk.descr AS status
FROM lkp_hqmc_case_status lk
WHERE c.case_type = 'M'
AND lk.id = c.hamc_case_status
UNION
SELECT c.case_id,
lk.descr AS status
FROM lkp_fl_case_status lk
WHERE c.case_type = 'M'
AND lk.id = c.fl_case_status
)
GROUP BY case_id
)
END
) AS status
from cases c,
lkp_alt_show_cause_authority lkp_alt
WHERE c.ascAt = lkp_alt.id(+)
AND c.delete_date IS NULL
and not (nvl(c.hamc_case_status,0) = 8
and c.CLOSE_DATE < sysdate - 365
)
ORDER BY c.case_id desc
Case Table
id name hamc_case_status case_type
1 AAA 1 P
2 BBB 1 P
3 CCC 1 P
4 DDD 1 P
case_status
1 16 01-NOV-19 03.42.37.420000000 PM
1 5 01-NOV-19 03.42.37.420000000 PM
2 1 18-NOV-19 12.36.11.268000000 PM
2 3 18-NOV-19 12.36.11.268000000 PM
3 5 18-NOV-21 12.36.11.268000000 PM
3 16 18-NOV-21 12.36.11.268000000 PM
lkp_alt_show_cause_authority
id desc
1 TEST
2 TEST1
lkp_hqmc_case_status
id descr
1 status
2 status1..
8 Closed
lkp_fl_case_status
id descr
1 fstatus1
2 fstatus2
lkp_case_status
id Descr
1 Active
2 Intermediate
3 InActive
5 Prep
16 Closed
The output should show as follows:
Result:
id name status
2 BBB Active, InActive
3 CCC Prep, Closed
4 DDD
It shouldnt pull case with id = 1 since the case closed status 16 is in '01-NOV-19'
Output I get with this query is:
id name status
1 null
2 BBB Active, InActive
3 CCC Prep, Closed
4 DDD
It is a legacy application with 150 fields and multiple joins on reporting. I cannot change query with 'outer join'.
I have to use same format of tablea, tableb and use (+) for joins.
Thanks in advance.

identify NULL and update for same key column in oracle

I have a test table having below details:
ID Key_COLUMN final_Value
1 aaa 1234
2 bbb 2345
3 bbb NULL
4 ccc 456
5 ccc 145
Desired Output:
--final_value updated from NULL to 2345 based key_column (bbb)
ID Key_COLUMN final_Value
1 aaa 1234
2 bbb 2345
3 bbb 2345
4 ccc 456
5 ccc 145
Identify KEY column having NULL and value and update NULL with the value.
this update requied on huge amount of data
Please assist.
You can use window functions:
select t.*,
coalesce(final_value, max(final_value) over (partition by key_column)) as imputed_final_value
from t;
If you wanted an update -- to actually change the data -- you can use a correlated subquery:
update t
set final_value = (select t2.final_value
from t t2
where t2.key_column = t.key_column and
t2.final_value is not null and
rownum = 1
)
where final_value is null;

How to build query? Any idea?

3 tables:
Mark table:
student_id sa_id marks
1 1 75
1 2 80
1 3 100
2 4 85
2 5 90
2 6 60
course table:
course_code sat_id name_code
AAA 100 1 midterm1
AAA 100 2 midterm2
AAA 100 3 final
BBB 200 4 midterm1
BBB 200 5 midterm2
BBB 200 6 final
transform table:
sa_id sat_id
1 1
2 2
3 3
4 4
5 5
6 6
select course.course_code, mark.marks
from mark
left outer join transform on transform.sa_id = mark.sa_id
left outer join course on course.sat_id = transfrom.sat_id
where course.name_code = 'midterm1'
At the above query only midterm1 result, also we can extract mid2 and final
select mark.student_id,course.course_code, mark.marks, course.name_code
from mark
left outer join transform on transform.sa_id = mark.sa_id
left outer join course on course.sat_id = transfrom.sat_id
order by mark.student_id, course.course_code
Result will give:
student_id course_code marks name_code
1 AAA 100 75 midterm1
1 AAA 100 80 midterm2
1 AAA 100 100 final
2 BBB 200 85 midterm1
2 BBB 200 90 midterm2
2 BBB 200 60 final
So how to build query that should be
student_id course_code midterm1 midterm2 final
1 AAA 100 75 80 100
2 BBB 200 85 90 60
You can use case when , group by (and a fake aggregation function)
select
student_id
, course_code
, max(case when name_code ='midterm1' then mark else null) midterm1
, max(case when name_code ='midterm2' then mark else null) midterm2
, max(case when name_code ='final' then mark else null) final
from mark
left outer join transform on transform.sa_id = mark.sa_id
left outer join course on course.sat_id = transfrom.sat_id
group by student_id, course_code
order by mark.student_id, course.course_code
But if you don't like aggregation functio you can use a 3 seleft join on mark
select
mm1.student_id
, mm1.course_code
, mm1.mark midterm1
, mm2.mark midterm2
, mm3.mark finale
from mark as mm1
left join mark as mm2 on mm1.student_id = mm2.student_id and mm1.course_code = mm2.course_code
left join mark as mm3 on mm1.student_id = mm3.student_id and mm1.course_code = mm3.course_code
left outer join transform on transform.sa_id = mm1.sa_id
left outer join course on course.sat_id = transfrom.sat_id
where mm1.name_code = 'midterm1'
and mm2.name_code = 'midterm2'
and mm3.name_code = 'final'

How to Add Order to an existing table?

I have table called Products. Let say this is my table,
ID Name ParentID
-- --- --------
1 a NULL
2 b NULL
3 a1 1
4 a2 1
5 b2 2
6 b2 2
Now I need to add [Order] Column with respect to ParentID,
ID Name ParentID Order
-- --- -------- ----
1 a NULL NULL
2 b NULL NULL
3 a1 1 1
4 a2 1 2
5 b2 2 1
6 b2 2 2
Creating [Order] is trivial but inserting record is a bit tricky part
UPDATE [Products]
SET [Products].[Order] = PTT.[Order]
FROM
[Products]
INNER JOIN (SELECT ID, ROW_NUMBER() OVER (PARTITION BY PT.ParentID ORDER BY ID) AS [Order]
FROM [Products] PT
WHERE PT.ParentID IS NOT NULL) AS PTT ON PTT.ID = [Products].ID

need help sql query

I am having one query given below in that Employee, ShiftAllocation, ShiftMaster (two for two shift: Shift1 and shift2) table
SELECT
Em.EmployeeId, Em.EmployeeName,
Sa.ShiftAllocationDate, Sa.ShiftId2, Sa.ShiftId, Sm.ShiftName,
Sm2.ShiftName AS ShiftName2, Sa.ShiftAllocationId
FROM
ShiftAllocation AS Sa
INNER JOIN
EmployeeMaster AS Em ON Sa.EmployeeId = Em.EmployeeId
LEFT OUTER JOIN
ShiftMaster AS Sm2 ON Sa.ShiftId2 = Sm2.ShiftId
LEFT OUTER JOIN
ShiftMaster AS Sm ON Sa.ShiftId = Sm.ShiftId
I am getting this output:
ShiftAlld EmployeeId EmployeeName ShiftAllDate shiftId2 ShifId ShiftName ShiftName2
1 19 XYZ 2011-08-01 NULL 1 General NULL
2 19 XYZ 2011-08-02 NULL 1 General NULL
3 19 XYZ 2011-08-02 NULL -1 NULL NULL
4 19 XYZ 2011-08-02 NULL 1 General NULL
5 19 XYZ 2011-08-02 NULL -2 NULL NULL
6 19 XYZ 2011-08-02 NULL 1 General NULL
I want
shiftId -1 value should assign ShiftName as Week Off
ShiftId -2 value should assign ShiftName as Holiday
ShiftId2 NULL value should assign ShiftName2 as Not Assign
I want this output:
ShiftAlld EmployeeId EmployeeName ShiftAllDate shiftId2 ShifId ShiftName ShiftName2
1 19 XYZ 2011-08-01 NULL 1 General NotAssign
2 19 XYZ 2011-08-02 NULL 1 General NotAssign
3 19 XYZ 2011-08-02 NULL -1 WeekOff NotAssign
4 19 XYZ 2011-08-02 NULL 1 General NotAssign
5 19 XYZ 2011-08-02 NULL -2 Holiday NotAssign
6 19 XYZ 2011-08-02 NULL 1 General NotAssign
You can use a case something like this (untested so there may be any number of typos in there).
select Em.EmployeeId,
Em.EmployeeName,
Sa.ShiftAllocationDate,
Sa.ShiftId2,
Sa.ShiftId,
case when sa.ShiftId = -1 then 'Week Off'
when sa.ShiftId = -2 then 'Holiday'
else sm.ShiftName
end as ShiftName,
case when sm2.ShiftId is null then 'Not assigned'
else Sm2.ShiftName
end as ShiftName2,
Sa.ShiftAllocationId
from ShiftAllocation as Sa
inner join EmployeeMaster as Em
on Sa.EmployeeId = Em.EmployeeId
left outer join ShiftMaster as Sm2
on Sa.ShiftId2 = Sm2.ShiftId
left outer join ShiftMaster as Sm
on Sa.ShiftId = Sm.ShiftId
Although I am not sure if it would be the best way to do it; you can try using CASE in your sql to arrive at a solution.
Edit : This http://www.dba-oracle.com/t_case_sql_clause.htm should help you if you want to try.