SQL select query in update - sql

I am trying to fix this query:
I have an update query.
UPDATE controll_SZHEAD14
LEFT JOIN [outgoing] ON controll_SZHEAD14.sa_code = [outgoing].Account
SET [outgoing].Account = controll_SZHEAD14.sa_code, [outgoing].Buyer = controll_SZHEAD14.sa_buyername, [outgoing].startdate = controll_SZHEAD14.sa_date, [outgoing].Finance = controll_SZHEAD14.sa_tedat, [outgoing].maxdate = controll_SZHEAD14.sa_esdat, [outgoing].[25alap] = controll_SZHEAD14.sa_summary
WHERE (((controll_SZHEAD14.sa_code)>"BA14/01997"));
So if I run this query above, than refresh my data, but only from BA14/01997 till now. So in this example this will refresh 66 row. (66 row affected)
I have this simple select SQL:
SELECT TOP 1 Account FROM [outgoing] ORDER BY Account DESC;
If I run this above I got the last account, in this example:BA14/01997
So I would like to take this two query "update+select" together.
UPDATE controll_SZHEAD14
LEFT JOIN [outgoing] ON controll_SZHEAD14.sa_code = [outgoing].Account
SET [outgoing].Account = controll_SZHEAD14.sa_code, [outgoing].Buyer = controll_SZHEAD14.sa_buyername, [outgoing].startdate = controll_SZHEAD14.sa_date, [outgoing].Finance = controll_SZHEAD14.sa_tedat, [outgoing].maxdate = controll_SZHEAD14.sa_esdat, [outgoing].[25alap] = controll_SZHEAD14.sa_summary
WHERE (((controll_SZHEAD14.sa_code)>"SELECT TOP 1 Account FROM [outgoing] GROUP BY Account DESC;"));
The above query does not work properly. When I run, I got 0 row affected, but I expect the same as the first update query.
Any idea how to fix this?

Enclose the SELECT statement in ( ) brackets
UPDATE controll_SZHEAD14
LEFT JOIN [outgoing]
ON controll_SZHEAD14.sa_code = [outgoing].Account
SET [outgoing].Account = controll_SZHEAD14.sa_code,
[outgoing].Buyer = controll_SZHEAD14.sa_buyername,
[outgoing].startdate = controll_SZHEAD14.sa_date,
[outgoing].Finance = controll_SZHEAD14.sa_tedat,
[outgoing].maxdate = controll_SZHEAD14.sa_esdat,
[outgoing].[25alap] = controll_SZHEAD14.sa_summary
WHERE controll_SZHEAD14.sa_code > (
SELECT TOP 1 Account
FROM [outgoing]
ORDER BY Account DESC
);

Related

How to Combine two queries into one in Oracle SQL

We have an error in production, luckily have a manual solution for this, however I have to run below two queries every morning to fix the error. This is so manual, I want to automate this and combine two queries into one. However we only have this error in production and not in DEV or QA, if I mess up with the combining query that will end up a chaos, So I need your expertise.
1st Query brings project numbers
select id, ugenProjectNumber
from unifier_uxpecai
where (pecaiChecklistNumber = 0 or pecaiChecklistNumber is null)
or (pecaiChecklistItemNumber = 0 or pecaiChecklistItemNumber is null)
2nd Query fix broken links between action items and list items, I manually put 1st query results unique project numbers into second query and run the second query per each unique project numbers.
update unifier_uxpecai pai
set (pai.pecaiChecklistNumber, pai.pecaiChecklistItemNumber) =
(
select pcl.id, pcli.id
from unifier_uxpecl pcl
inner join unifier_uxpecl_lineitem pcli on pcli.uuu_tab_id = 0 and
pcli.record_id = pcl.id
where pcl.ugenProjectNumber = 'GL-16-161010-143502'
and pcli.pecItemActionItemBPC = pai.id
)
where exists
(
select pcli.pecItemActionItemBPC
from unifier_uxpecl pcl
inner join unifier_uxpecl_lineitem pcli on pcli.uuu_tab_id = 0 and
pcli.record_id = pcl.id
where pcl.ugenProjectNumber = 'GL-16-161010-143502'
and pcli.pecItemActionItemBPC = pai.id
)
and (pai.pecaiChecklistNumber = 0 or pai.pecaiChecklistItemNumber = 0)
You can incorporate the logic into the queries:
update unifier_uxpecai pai
set (pai.pecaiChecklistNumber, pai.pecaiChecklistItemNumber) =
(select pcl.id, pcli.id
from unifier_uxpecl pcl join
unifier_uxpecl_lineitem pcli
on pcli.uuu_tab_id = 0 and pcli.record_id = pcl.id
where pcl.ugenProjectNumber in (select ugenProjectNumber
from unifier_uxpecai
where (pecaiChecklistNumber = 0 or pecaiChecklistNumber is null) or
(pecaiChecklistItemNumber = 0 or pecaiChecklistItemNumber is null
) and
pcli.pecItemActionItemBPC = pai.id
)
where exists
(
select pcli.pecItemActionItemBPC
from unifier_uxpecl pcl join
unifier_uxpecl_lineitem pcli
on pcli.uuu_tab_id = 0 and
pcli.record_id = pcl.id
where pcl.ugenProjectNumber in (select ugenProjectNumber
from unifier_uxpecai
where (pecaiChecklistNumber = 0 or pecaiChecklistNumber is null) or
(pecaiChecklistItemNumber = 0 or pecaiChecklistItemNumber is null
) and
pcli.pecItemActionItemBPC = pai.id
) and
(pai.pecaiChecklistNumber = 0 or pai.pecaiChecklistItemNumber = 0)

Oracle vs MS SQL UPDATE using IN SELECT

This SQL works fine on MS SQL Server but produces an error "ORA-00907: missing right parenthesis".
SQL:
UPDATE DELIVERY
SET VISIBLE = 0
WHERE DELIVERY.ID
IN
(
SELECT DELIVERY.ID FROM delivery WHERE DELIVERY.VISIBLE = 1
EXCEPT
SELECT DELIVERY.ID FROM delivery LEFT JOIN inventory ON INVENTORY.DELIVERYID = DELIVERY.ID
WHERE ((DELIVERY.VISIBLE = 1) AND (INVENTORY.VISIBLE = 1)) AND (INVENTORY.INVENTORYSTATE = 3)
);
Is there a way to get this to work on Oracle or is an UPDATE using an IN with SELECT statements just conceptually wrong?
In Oracle, IN Can also work, just change the EXCEPT to MINUS in Oracle
http://www.techonthenet.com/sql/update.php
http://docs.oracle.com/cd/B19306_01/server.102/b14200/statements_10007.htm
http://docs.oracle.com/cd/E17952_01/refman-5.0-en/exists-and-not-exists-subqueries.html
An Sample Example for this will be
UPDATE DELIVERY
SET VISIBLE = 0
WHERE DELIVERY.ID
IN
(
(SELECT DELIVERY.ID FROM delivery WHERE DELIVERY.VISIBLE = 1)
MINUS
(SELECT DELIVERY.ID
FROM delivery LEFT JOIN inventory ON INVENTORY.DELIVERYID = DELIVERY.ID
WHERE (((DELIVERY.VISIBLE = 1) AND (INVENTORY.VISIBLE = 1))
AND (INVENTORY.INVENTORYSTATE = 3)))
);
I think you can modify this query so that it works in both SQL Server and Oracle:
UPDATE delivery d
SET d.visible = 0
WHERE d.visible = 1
AND NOT EXISTS ( SELECT 1 FROM inventory i
WHERE i.deliveryid = d.id
AND i.visible = 1
AND i.inventorystate = 3 )
This will update DELIVERY setting visible = 0 wherever visible = 1 but there is no corresponding record in INVENTORY with visible = 1 and inventorystate = 3.

Sql query taking forever to execute

Following is query that i am trying to execute and it is taking forever to execute.
SELECT DISTINCT (CLN_CLNDR_DT)FROM CLN_CLNDR CLN,
SYG_SYSTEM_GLOBALS SYG,CTD_CSH_TRNSCTN_DTLS CTD WHERE CLN.CLN_BP_ID = SYG.SYG_BP_ID AND CLN.CLN_ENTITY_ID = CTD.CTD_ENTITY_ID AND CLN_DT_SEQ_NUM
IN
(SELECT DISTINCT (CLN1.CLN_DT_SEQ_NUM)
FROM CLN_CLNDR CLN1,
SBD_SYS_BSNS_DTS SBD,
SYG_SYSTEM_GLOBALS SYG,
CTD_CSH_TRNSCTN_DTLS CTR
WHERE SBD.SBD_BSNS_DT = CLN1.CLN_CLNDR_DT
AND CLN1.OU_ID = SBD.OU_ID
AND CLN1.CLN_BP_ID = SYG.SYG_BP_ID
AND CLN1.CLN_ENTITY_ID = CTR.CTD_ENTITY_ID
);
When i try to run just the query inside in , it easily returns result as
(522,470,419,417,553,582,305,361)
SELECT DISTINCT (CLN1.CLN_DT_SEQ_NUM)
FROM CLN_CLNDR CLN1,
SBD_SYS_BSNS_DTS SBD,
SYG_SYSTEM_GLOBALS SYG,
CTD_CSH_TRNSCTN_DTLS CTR
WHERE SBD.SBD_BSNS_DT = CLN1.CLN_CLNDR_DT
AND CLN1.OU_ID = SBD.OU_ID
AND CLN1.CLN_BP_ID = SYG.SYG_BP_ID
AND CLN1.CLN_ENTITY_ID = CTR.CTD_ENTITY_ID;
Also when i try to execute directly the full query replacing the query inside in block
with the result of inner query that is (522,470,419,417,553,582,305,361),
It is working fine.
As i am new to this , i don't know what is going on .
Please help me with this.
I don't think you need all the tables in the subquery since you are already using them to filter rows in the outer query. Try this:
SELECT DISTINCT (CLN_CLNDR_DT)
FROM CLN_CLNDR CLN,
SYG_SYSTEM_GLOBALS SYG,
CTD_CSH_TRNSCTN_DTLS CTD
WHERE CLN.CLN_BP_ID = SYG.SYG_BP_ID
AND CLN.CLN_ENTITY_ID = CTD.CTD_ENTITY_ID
AND CLN_DT_SEQ_NUM
IN
(SELECT DISTINCT (CLN1.CLN_DT_SEQ_NUM)
FROM CLN_CLNDR CLN1,
SBD_SYS_BSNS_DTS SBD,
WHERE SBD.SBD_BSNS_DT = CLN1.CLN_CLNDR_DT
AND CLN1.OU_ID = SBD.OU_ID
);

Speed up SQL query with nested query

I hope you guys can help me. I have the following SQL query, I think it's not very heavy but it takes about 5 minutes to complete. Please let me know if you have another way to complete this:
update rep
set rep.StatusID= 2,
rep.Available= 1,
rep.User= #user
from dbo.MCP_Rep_Compensation_Plan rep
left join dbo.MCP_Compensation_Plan compensationplan on compensationplan.Compensation_Plan_ID = #compplan_id and compensationplan.Active = 1
left join dbo.MRRS_Cycle actualcycle on actualcycle.CycleID = compensationplan.CycleID and actualcycle.Active = 1
left join dbo.MRRS_Cycle lastcycle on lastcycle.Consecutive = actualcycle.Consecutive -1 and lastcycle.Active = 1
where rep.Active = 1 and rep.ID_Compensation_Plan = #compplan_id and exists(
select OrderID
from dbo.MCP_Orders
where Active = 1 and Order_cycle = lastcycle.CycleID and OrderRepID = rep.RepID
and Order_Status in(28,30))
I do see some places your query can be rewritten, but I'm not sure how much it will help without looking at your execution plans and ensuring you have the appropriate indices in place.
For example, make sure you include actual join criteria in your first join to the compensationplan table. Do this by joining on compensationplan.Compensation_Plan_ID and rep.ID_Compensation_Plan.
Also, I don't see the need for the OUTER JOINs since you're using some of those tables in your correlated exist subquery.
Here is the updated query:
update rep
set rep.StatusID= 2,
rep.Available= 1,
rep.User= #user
from dbo.MCP_Rep_Compensation_Plan rep
join dbo.MCP_Compensation_Plan compensationplan on
compensationplan.Compensation_Plan_ID = rep.ID_Compensation_Plan and compensationplan.Active = 1
join dbo.MRRS_Cycle actualcycle on
actualcycle.CycleID = compensationplan.CycleID and actualcycle.Active = 1
join dbo.MRRS_Cycle lastcycle on
lastcycle.Consecutive = actualcycle.Consecutive -1 and lastcycle.Active = 1
where rep.Active = 1
and rep.ID_Compensation_Plan = #compplan_id
and exists(
select OrderID
from dbo.MCP_Orders
where Active = 1
and Order_cycle = lastcycle.CycleID
and OrderRepID = rep.RepID
and Order_Status in(28,30)
)

Subquery return more than one value

I have the following query which gives gives req_no and order_no. order_no is a from a sub_query which you can see from below sql. For a few req_no there are more than one order_no's and because of that I get ORA-01427: single-row subquery returns more than one row.
I would like to display both the order_no for one req_no, how can I achieve this?
Any help is highly appreciable.
Thanks
P.S. Our client's one database is still Oracle 8i.
SELECT max_qst.req_no,
(SELECT DISTINCT max_odr.order_no
FROM maximo_orders max_odr,
maximo_order_revisions max_odv,
maximo_order_items max_odi,
maximo_order_dates max_odd,
maximo_requisition_order max_rqo,
maximo_requisition_details max_req
WHERE max_req.req_no = max_qst.req_no
AND max_req.req_yr = max_qst.req_yr
AND max_odr.order_no = max_odi.order_no
AND max_odi.order_item_id = max_odd.order_item_id
AND max_req.requisition_item_id = max_rqo.requisition_item_id
AND max_rqo.order_schedule_id = max_odd.order_schedule_id
AND max_odv.order_no = max_odi.order_no
AND max_odv.revision_no =
(SELECT MAX (max_alias.revision_no)
FROM maximo_order_revisions max_alias
WHERE max_alias.order_no = max_odv.order_no)
AND maximo_order_item (max_odi.order_no,
max_odv.revision_no,
max_odi.order_item_id
) = 'CONFIRMED'
)
FROM maximo_requisitions max_qst, maximo_requisition_details max_qsd
WHERE max_qst.qst_id = max_qsd.qst_id
AND max_qst.enter_date = '2001'
AND max_qst.req_no = 'PUR_12WX'
Update 1
Desired out put.
REQ_No ORDER_NO
PUR_12WX PR_9078
PUR_12WX PR_9079
Use a join instead of a correlated sub-query.
I've removed the max_qst references from the sub-query and moved them to the join predicate.
I've also just changed it to use a LEFT JOIN. This allows for the possibility of there being no order_no values returned.
SELECT
max_qst.req_no,
sub_query.order_no
FROM
maximo_requisitions max_qst
INNER JOIN
maximo_requisition_details max_qsd
ON max_qst.qst_id = max_qsd.qst_id
LEFT JOIN
(
SELECT DISTINCT
max_odr.order_no,
max_req.req_no,
max_req.req_yr
FROM
maximo_orders max_odr,
maximo_order_revisions max_odv,
maximo_order_items max_odi,
maximo_order_dates max_odd,
maximo_requisition_order max_rqo,
maximo_requisition_details max_req
WHERE
max_odr.order_no = max_odi.order_no
AND max_odi.order_item_id = max_odd.order_item_id
AND max_req.requisition_item_id = max_rqo.requisition_item_id
AND max_rqo.order_schedule_id = max_odd.order_schedule_id
AND max_odv.order_no = max_odi.order_no
AND max_odv.revision_no = (SELECT MAX (max_alias.revision_no)
FROM maximo_order_revisions max_alias
WHERE max_alias.order_no = max_odv.order_no)
AND maximo_order_item (max_odi.order_no, max_odv.revision_no, max_odi.order_item_id) = 'CONFIRMED'
)
suq_query
ON max_qst.req_no = sub_query.req_no
AND max_qst.req_yr = sub_query.req_yr
WHERE
max_qst.enter_date = '2001'
max_qst.req_no = 'PUR_12WX'
You can use
DISTINCT
or
WHERE ROWNUM = 1
But I'd suggest you investigate why you're getting more than one row returned: Have you misunderstood the data, have you missed a join, is there erroneous or duplicate data etc etc.