I am using oracle package for one of the reports in out project.
When this package accessed concurrently, below query is causing deadlock.
Please suggest anyway to improve this query:
UPDATE temp_final t
SET notnullcol = ( select COALESCE(h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11,h12,h13,h14,h15,h16,h17,h18,h19,h20,h21,h22,h23,h23 )
from temp_final
where ( ( rowgroup = 'G' and mid = t.seid)
or ( rgroup = 'K' and t.rgroup = rgroup and t.mid = mid )
or ( rgroup = 'G' and t.rgroup = rgroup and t.mid = mid )
)
and rid = t.rid
and rtype = t.rtype
and sid = t.sid
and pid = t.pid AND rid = 9 AND rtype =1 AND sid = 'value' AND pid = 421 )
--5,034 rows updated.
Please note, temp_final is a temp table and all columns h* are used in calculation so schema of this table can not be used.
Related
I have a query , when I run the below query in Oracle
update cust_logs el
set batch_id = '3344'
from
client c
join port p on p.pid = c.pid
and
p.flag = 0
where
c.cid = el.cid
and
c.flag = 0
and
p.export = 1
and
el.trans_id is nul
I am getting error as
ORA-00933: sql command not properly ended.
I have tried
merge into cust_logs el
using (
select *
from client c
join port p
on (
p.pid = c.pid
and p.flag = 0
)
) "s"
on ((
c.cid = el.cid
and c.flag = 0
and p.export = 1
and decode(el.trans_id, nul, 1, 0) = 1
))
when matched then update set
batch_id = '3344'
Can you please let me know how to resolve
This is my sql fiddle
http://sqlfiddle.com/#!4/c9166/1
You can't join in an update.
You haven't said what the issue is with your merge attempt. But your on clause referring to the base table instead of the "s" alias. And the select * will cause an ambiguous identifier error as pid appears in both tables. And it's null not nul.
So this might be closer, at least:
merge into cust_logs el
using (
select c.cid, c.flag, p.export
from client c
join port p
on p.pid = c.pid
where p.flag = 0
) s
on (
s.cid = el.cid
and s.flag = 0
and s.export = 1
and decode(el.trans_id, null, 1, 0) = 1
)
when matched then update set batch_id = '3344'
But we don't have your tables or data to verify it.
If batch_id is a numeric column then the last line should be:
when matched then update set batch_id = 3344
I want count two columns using power bi to create a visual.my measure as below
Test Measure = COUNTA('Export'[Line])+COUNTA('Export'[Line 2]),
You need to create a calculated table with all the lines values, like this
TableValues =
DISTINCT ( UNION ( DISTINCT ( 'Table'[Line] ), DISTINCT ( 'Table'[Line_2] ) ) )
With that table done, you can write the following measure
CountValues =
VAR _CurrentValue =
SELECTEDVALUE ( TableValues[Line] )
VAR _C1 =
CALCULATE ( COUNTA ( 'Table'[Line] ), 'Table'[Line] = _CurrentValue )
VAR _C2 =
CALCULATE ( COUNTA ( 'Table'[Line_2] ), 'Table'[Line_2] = _CurrentValue )
RETURN
_C1 + _C2
The calculations count all the instances of a specific Line. It's important that the TableValues doesn't have any relationship with the other tables.
Output
Using 'TableValues'[Line] as the Line and CountValues as the metric.
I have a Task table and a Project table. I'm trying to update the Percent Complete field in the Project table with the value of the Percent Complete field from the Tasks table. But I only want to update the field when the sequence code from the Task table is equal to 1. My failed attempt looks like this:
UPDATE inv_projects prj
SET prj.percent_complete = (SELECT NVL(tsk.prpctcomplete, 0)
FROM prtask tsk
WHERE tsk.prprojectid = prj.prid)
WHERE (SELECT tsk.prwbssequence
FROM prtask tsk
WHERE prj.prid = tsk.prprojectid) = 1;
I'm getting the error:
Error starting at line : 1 in command -
...
Error report -
ORA-01427: single-row subquery returns more than one row
I'm not quite sure what's going wrong.
Presumably, you want the sequence filtering in the subquery, like so
UPDATE inv_projects prj
SET prj.percent_complete = (
SELECT NVL(tsk.prpctcomplete, 0)
FROM prtask tsk
WHERE tsk.prprojectid = prj.prid AND sk.prwbssequence = 1
)
This assumes that (prprojectid, prwbssequence) is unique in the task table, which seems consistent with your problem statement.
If there are projects without a task of sequence 1, and you don't want to update them, then use exists:
UPDATE inv_projects prj
SET prj.percent_complete = (
SELECT NVL(tsk.prpctcomplete, 0)
FROM prtask tsk
WHERE tsk.prprojectid = prj.prid AND sk.prwbssequence = 1
)
WHERE EXISTS (
SELECT 1
FROM prtask tsk
WHERE tsk.prprojectid = prj.prid AND sk.prwbssequence = 1
)
Or maybe your intent is to set the completion percent to 0 in this case; if so, move NVL() outside of the subquery:
UPDATE inv_projects prj
SET prj.percent_complete = NVL(
(
SELECT tsk.prpctcomplete
FROM prtask tsk
WHERE tsk.prprojectid = prj.prid AND sk.prwbssequence = 1
),
0
)
I want to run a multi-column correlated update of this kind:
UPDATE t1 t1_alias
SET (table_name, tablespace_name) = (
SELECT table_name, tablespace_name
FROM t2 t2_alias
WHERE t1_alias.table_name = t2_alias.table_name
);
But my attempt:
update customer up
set (customer_name, account, active) = (
select tmp.name, tmp.account, case when tmp.active = 'Yes' then 1 else 0 end
from customer_temp tmp
where up.agent = substr(tmp.agent, -4) and up.customer_code = tmp.code
);
... throws:
ORA-01407: cannot update ("FOO"."CUSTOMER"."CUSTOMER_NAME") to NULL
The source table customer_temp has no null values so I must be getting matches wrong. What is my error or misconception?
Presumably, there are some rows in the target table that have no match in the subquery.
You can avoid this with by adding an exists condition that filters out "unmatched" rows:
update customer up
set (customer_name, account, active) = (
select tmp.name, tmp.account, case when tmp.active = 'Yes' then 1 else 0 end
from customer_temp tmp
where up.agent = substr(tmp.agent, -4) and up.customer_code = tmp.code
)
where exists (
select 1
from customer_temp tmp
where up.agent = substr(tmp.agent, -4) and up.customer_code = tmp.code
);
I've been trying to execute below code, but it's not running. SQLFiddle gives
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.'EO' AS 'EO','EF'.'AC' AS 'AC','EO'' at line 2
I don't know what I'm doing wrong.
SELECT DISTINCT
'EO'.'EO' AS 'EO',
'EF'.'AC' AS 'AC',
'EO'.'WO' AS 'WO',
'EO'.'WOF' AS 'WOF',
'EF'.'PN_SN' AS 'PN_SN',
'EC'.'CD' AS 'CD',
'EF'.'PN' AS 'PN',
'EO'.'EOC' AS 'EOC',
'EO'.'STAT' AS 'STAT',
'EF'.'OVRD' AS 'OVRD',
'EF'.'SEL' AS 'SEL',
'EF'.'ED' AS 'ED',
'EF'.'SCHED' AS 'SCHED',
'EF'.'SAD' AS 'SAD',
'EF'.'SA' AS 'SA',
'EO'.'EOD' AS 'EOD'
FROM
EO,
EC,
EF
WHERE
(
'EO'.'EO' = 'EC'.'EO'
)
AND
(
'EF'.'PSN' = 'EC'.'PSN'
)
AND
(
'EO'.'WB' = 'Y'
)
AND
(
'EC'.'CONT' = 'W & B'
)
;
SELECT
'EC'.'RSTD' AS 'RSTD'
FROM EC RIGHT JOIN EF
ON ( 'EF'.'EO' = 'EC'.'EO' )
ON ( 'EF'.'AC' = 'EC'.'AC' )
ON ( 'EF'.'PN' = 'EC'.'PN' )
ON ( 'EF'.'PSN' = 'EC'.'PSN' )
Single quotes - ' - are for literals. Use double-quotes - " - for identifiers ...
SELECT
"EC"."RSTD"
FROM EC RIGHT JOIN EF
ON ( "EF"."EO" = "EC"."EO" )
ON ( "EF"."AC" = "EC"."AC" )
ON ( "EF"."PN" = "EC"."PN" )
ON ( "EF"."PSN' = "EC"."PSN" )
... or, even better, omit them altogether:
SELECT
EC.RSTD
FROM EC RIGHT JOIN EF
ON ( EF.EO = EC.EO )
ON ( EF.AC = EC.AC )
ON ( EF.PN = EC.PN )
ON ( EF.PSN = EC.PSN )
Obviously keep the single quotes when appropriate e.g. comparing a column to a literal:
where EO.WB = 'Y'
Also note that it is unnecessary to use a column alias when you're not changing the column name: select ec.rstd as rstd is redundant, and indeed annoying. Reserve their use for renaming columns e.g. select ec.rstd as my_new_col_name