Problems updating query with inner join - oracle

Can anyone please assist in getting this one query to work. I am trying to update status of a column in a table having joined to other tables
Here is the query
update
(select I.account_id, I.sts, I.name_id, CI.CRM_TYPE, I.comments
from PPInters I
inner join DW.CUST CT
on I.account_id = CT.account_id
where
I.sts is null
AND I.comments IS NOT NULL
AND CT.CUSTTYPe = 'INTNL') T
SET T.STS = 'D'
WHERE T.account_id IN (2000208927,380166014,190180447,166078041,105029075 )
I am getting "ORA-01779: cannot modify a column which maps to a non key-preserved table" error
What I am trying to do here is to set I.STS = 'D' for some 700 records pulled up using this query
select I.account_id, I.sts, I.name_id, CI.CRM_TYPE, I.comments
from PPInters I
inner join DW.CUST CT
on I.account_id = CT.account_id
where
I.sts is null
AND I.comments IS NOT NULL
AND CT.CUSTTYPe = 'INTNL'
I appreciate it

Assumming that account_id is a primary key kolumn in table PPInters,
that is it's value uniquely identifies records in this table:
UPDATE PPInters
SET STS = 'D'
WHERE account_id IN (
select I.account_id
/*, I.sts, I.name_id, CI.CRM_TYPE, I.comments */
from PPInters I
inner join DW.CUST CT
on I.account_id = CT.account_id
where
I.sts is null
AND I.comments IS NOT NULL
AND CT.CUSTTYPe = 'INTNL'
)
AND account_id IN (2000208927,380166014,190180447,166078041,105029075 )

Related

Joining 3 tables doesnt work, should I use a subquery for the below

I have this query.
SELECT re_current_benefits.prospect_nbr, qo_quote.quote_id, hra_ind, quote_type, effective_date, quote_expiration_ind, mhs_lob_code
FROM re_current_benefits
INNER JOIN qo_quote ON qo_quote.prospect_nbr = re_current_benefits.prospect_nbr
WHERE hra_ind = 'Y' AND quote_type = 'R' AND quote_expiration_ind IS NULL AND mhs_lob_code NOT IN 'DTL1' AND mhs_lob_code NOT IN 'DTL2'
And I need to pull the prospect_nbr from here, I also need to add a filter saying effective_date = renewal_date but if I do that by joining all 3 tables together I get 0 results which should not be the case.
SELECT prospect_nbr, rmr_run_status, renewal_date
FROM re_group
WHERE rmr_run_status = ANY ('S', 'W')

Get STATUS from table by which join in oracle

I have a query for which based on CRNO I get the STATUS from another table. So the below query is
select a.crno, a.crno_date, a.state, a.status_rank from R4G_OSP.ENODEB a
Where a.crno is not null
and a.crno = 'R4G-MH-NLD-7718'
and a.status_rank is not null
order by 4 asc;
and STATUS table query is
select * from APP_WFM.WFM_CANDIDATE_STATUS where rank = 20
So, now I want to join the query and get the status in the first query. How should I do that
The sample data of both query is below
QUERY 1 SAMPLE DATA
QUERY 2 SAMPLE DATA
Please suggest how should I get the STATUS by joining it
You can use JOIN. Manual Here
select a.crno,
a.crno_date,
a.state,
a.status_rank,
APP_WFM.WFM_CANDIDATE_STATUS.STATUS
from R4G_OSP.ENODEB a
join APP_WFM.WFM_CANDIDATE_STATUS on APP_WFM.WFM_CANDIDATE_STATUS.RANK = a.status_rank
Where a.crno is not null
and a.crno = 'R4G-MH-NLD-7718'
and a.status_rank is not null
order by 4 asc;
If you want only rank 20 add AND a.status_rank = 20 before ORDER BY
Select a.crno,
a.crno_date,
a.state,
a.status_rank
from R4G_OSP.ENODEB a
join APP_WFM.WFM_CANDIDATE_STATUS on APP_WFM.WFM_CANDIDATE_STATUS.RANK = a.status_rank
and a.crno = 'R4G-MH-NLD-7718'
and a.status_rank is not null
order by 4 asc;
Based on the sample data of the table provided it is seen that the primary key relationship is set on WFM.WFM_CANDIDATE_STATUS.RANK and status_rank of another table so you can easily join between those columns

cant update a column from table it returns single-row subquery returns more than one

When i execute the following query, i get the message like
Ora-01427 single-row subquery returns more than one row
I am trying to update "City" column in Table A From another table.
How would I do this?
table A: Name, PER_code(it also has duplicated value or null), city, PD_code
table B: Name, PER_code(No duplicated value,Maybe null), city, Postal_code
The update statement:
UPDATE A
SET (A.city) =
(SELECT B.city
FROM B
INNER JOIN A
ON A.per_code=B.per_code
WHERE A.per_code is not null)
Since there are duplicate values but you select only one field, you modify your query from
UPDATE A SET (A.city) = (SELECT B.city FROM B INNER JOIN A ON
A.per_code=B.per_code WHERE A.per_code is not null)
to
UPDATE A SET (A.city) = (SELECT DISTINCT B.city FROM B INNER JOIN A ON
A.per_code=B.per_code WHERE A.per_code is not null)
The distinct operator will allow you to keep a single value if it's duplicated in the table B. If there are multiple distinct values, you will have to look at your data and make a decision about which value should be used in the other table.
You can also try MERGE INTO selecting DISTINCT records.
MERGE INTO A d
USING
( SELECT DISTINCT per_code, city FROM B ) s
ON ( s.per_code = d.per_code )
WHEN MATCHED THEN UPDATE SET d.City = s.City
WHERE d.per_code IS NOT NULL;
I think you intend a correlated subquery:
UPDATE A
SET city = (SELECT B.city
FROM B
WHERE A.per_code = B.per_code
)
WHERE A.per_code is not null;
EDIT:
The above should work given the constraints in the original question. If it does not, it is easily adaptable:
UPDATE A
SET city = (SELECT B.city
FROM B
WHERE A.per_code = B.per_code AND rownum = 1
)
WHERE A.per_code is not null;

Insert Records in a Table without duplicating the records

I have this MySQL Statement. I want to execute this SQL repeatedly to insert the result into a Third table withoud duplicating the Records in Third Table.
SELECT `Client_ID`, `Client_RFID_Number`
FROM ciom_master AS a
WHERE (`Client_Active` ='Y' OR `Client_Active` ='y')
AND NOT EXISTS (
SELECT (`Client_Check_Out`)
FROM `cio_master` AS b
WHERE a.Client_ID = b.Client_ID
and Client_Check_Out = CURDATE() )
AND NOT EXISTS (
SELECT (`Client_Check_In`)
FROM `cio_master` AS c
WHERE a.Client_ID = c.Client_ID
and Client_Check_In = CURDATE())
I am attempting following Statement but it is throwing error
INSERT IGNORE INTO cio_alert (`Client_ID`, `Client_RFID_Number`, `Client_Check_Out`, `Client_Check_In`)
SELECT `ciom_master`.`Client_ID`, `ciom_master`.`Client_RFID_Number`,`cio_master`.`Client_Check_Out`, `cio_master`.`Client_Check_In` FROM ciom_master
INNER JOIN `cio_master` ON `ciom_master`.`Client_ID` = `ciom_master`.`Client_ID`
WHERE EXISTS
(SELECT `Client_ID`, `Client_RFID_Number`
FROM ciom_master AS a
WHERE (`Client_Active` ='Y' OR `Client_Active` ='y')
AND NOT EXISTS (
SELECT (`Client_Check_Out`)
FROM `cio_master` AS b
WHERE a.Client_ID = b.Client_ID
and Client_Check_Out = CURDATE() )
AND NOT EXISTS (
SELECT (`Client_Check_In`)
FROM `cio_master` AS c
WHERE a.Client_ID = c.Client_ID
and Client_Check_In = CURDATE())
AND (
SELECT `Client_Check_Out` FROM cio_master
WHERE ((`Client_Check_Out` IS NOT NULL AND `Client_Check_In` IS NULL)
OR (`Client_Check_Out` IS NULL AND `Client_Check_In` IS NULL)))
AND (
SELECT `Client_Check_In` FROM cio_master
WHERE ((`Client_Check_Out` IS NOT NULL AND `Client_Check_In` IS NULL)
OR (`Client_Check_Out` IS NULL AND `Client_Check_In` IS NULL)))
)
You didnt mention exist or not exist condition at the last select statement... u simply put the and condition without any exist or not exist or in or not in. Try to put any of the valid condition over there. And also in the first select statement y r u using same table twice and joining without any alias try to check that one also once.

How to merge data from multiple rows into single column in new table?

How do I merge data from multiple rows in one table to a single column in a new table?
create table new_paragraphs
(
id NUMBER
paragraph CLOB
);
create table old_paragraphs
(
id
paragraph CLOB
);
merge into new_paragraphs a
using (select * from old_paragraphs) b
on (id = id)
when matched then
update set a.paragraph = a.paragraph || b.paragraph;
-- Results in error: unable to get a stable set of rows in the source tables
The above throws an exception.
It would work if id were a primary key in at least *old_paragraphs* (or if it were unique for each id found in *new_paragraph*)
Other than that, you want to use aliases in on (id = id) so that it reads on (a.id = b.id):
merge into new_paragraphs a
using (select * from old_paragraphs) b
on (a.id = b.id)
when matched then
update set a.paragraph = a.paragraph || b.paragraph;
Why are you doing a MERGE here? Why not a simple UPDATE (assuming ID is the primary key of both tables)
UPDATE new_paragraphs a
SET paragraph = (select a.paragraph || b.paragraph
from old_paragraphs b
where a.id = b.id)
WHERE EXISTS (SELECT 1
FROM old_paragraphs b
WHERE a.id = b.id)

Resources