How to update a table with joining two other tables - sql

We have three tables here, Session table which is connected to Green_fact table with Session_id field. Time_session table is the third table which is made of SessionDate from Session table with a primary key for every single day.
How can I populate the Date_ID field in Green_fact table.
the code below is what I think use, but it doesn't work properly
update green_fact
inner join "SESSION" on green_fact."SESSION_ID" = "SESSION"."SessionID"
inner join "TIME_SESSION" on "TIME_SESSION"."SESSION_DATE" = "SESSION"."SessionDate"
set green_fact."DATE_ID" = "TIME_SESSION"."ID" where green_fact."SESSION_ID" = "SESSION"."SessionID";

Oracle doesn't allow join in update. You can use correlated subqueries:
update green_fact gf
set DATE_ID = (select ts.ID
from SESSION s join
TIME_SESSION ts
on ts.SESSION_DATE = s.SessionDate
where gf.SESSION_ID = s.SESSIONID
)
where exists (select ts.ID
from SESSION s join
TIME_SESSION ts
on ts.SESSION_DATE = s.SessionDate
where gf.SESSION_ID = s.SESSIONID
);
The exists may not be necessary, if all the rows match.

In Oracle you can either update a table or an updatetable query, i.e. UPDATE tablename SET ... or UPDATE (SELECT ... FROM ...) SET ....
update
(
select gf.date_id, time_session.id as time_session_id
from green_fact gf
inner join session s on gf.session_id = s.sessionid
inner join time_session ts on ts.session_date = s.sessiondate
)
set date_id = time_session_id;
This will work, provided the DBMS sees it guaranteed that the query produces one row only per green_fact record (which it should, because of the primary and foreign keys).

Use below query for update from two tables :
UPDATE green_fact SET green_fact."DATE_ID" = A.Id
FROM
(
SELECT "TIME_SESSION"."ID" Id , "SESSION"."SessionID" SessionID
FROM "TIME_SESSION"
JOIN "SESSION" ON "TIME_SESSION"."SESSION_DATE" =
"SESSION"."SessionDate"
) A
WHERE green_fact."SESSION_ID" = A.SessionID;

Oracle doesn't allow joins to be used in UPDATE statements, but sometimes one can rewrite such a statement as a MERGE (you don't specify the version of Oracle you're using, but since 10g one can omit the WHEN MATCHED or WHEN NOT MATCHED clauses of the MERGE):
MERGE INTO green_fact gf
USING (
SELECT s."SessionID", ts.session_date, ts.id
FROM session s INNER JOIN time_session ts
ON s."SessionDate" = ts.session_date ) ts1
ON ( gf.session_id = ts1."SessionID" )
WHEN MATCHED THEN
UPDATE
SET gf.date_id = ts1.id;
Hope this helps.
By the way, I cannot stress enough that mixed-case object names in Oracle are a bad idea. But maybe you're dealing with legacy data and don't have a choice.

Related

Delete with subquery that produces two columns (in Postgresql)

I have to delete a few records that match two columns calculated with a subquery.
I can properly see them with this query:
select * from user_assignments as ua,
(
select user_assignments.user_id as uid,
job_selection as jid
from user_assignments
join job_selections on job_id = jobs.id
join data on job_selections.data_id = data.id
where data.my_column IS NULL
) as sq
where sq.uid = ua.user_id AND ua.job_selection_id = sq.jid;
This works, and I see the 7 assignments I want to delete.
However, deleting is not as easy as changing the SELECT by DELETE...
If I do:
delete from user_assignments as ua,
(
...
) as sq
where sq.uid = ua.user_id AND sq.jid = ua.job_selection_id;
I get:
ERROR: syntax error at or near ","
I've tried quite an assortment of combinations, yet I can't get it to work. I imagine it must be quite simple, but I'm quite a newbie in SQL.
Basically, I have a subquery that properly produces two columns that I can use for a SELECT FROM user_assignments and now I want to DELETE FROM user_assignments the records that I know I can SELECT.
Any hints would be very appreciated. Thank you in advance.
Use in or exists:
delete from user_assignments ua
where exists (select 1
from user_assignments ua2 join
job_selections js
on ua2.job_id = js.id join
data d
on js.data_id = d.id
where d.my_column IS NULL and
ua.user_id = sq.uid and ua.job_selection_id = sq.jid
);
Oh, I got it (I think).
Kuddos to this tutorial this tutorial and particularly, the section SQL delete records using subqueries with alias.
If someone else is interested, what I did was:
DELETE FROM user_assignments ua
WHERE EXISTS(
SELECT user_assignments.user_id as uid,
user_assignments.job_selection as jid
FROM user_assignments
join job_selections on job_id = jobs.id
join data on job_selections.data_id = data.id
WHERE data.my_column IS NULL
AND ua.user_id = uid
AND ua.job_selection = jid
)
This query also works fine with SELECT * FROM user_assignments

SQL How to optimize insert to table from temporary table

I created procedure where dynamically collecting from various projects (Databases) some records into temporary table and from that temporary table I am inserting into table. With WHERE statement , but unfortunately when I checked with Execution plan I find out, that this query part take a lot of load. How can I optimize this INSERT part or WHERE statement ?
INSERT INTO dbo.PROJECTS_TESTS ( PROJECTID, ANOTHERTID, DOMAINID, is_test)
SELECT * FROM #temp_Test AS tC
WHERE NOT EXISTS (SELECT TOP 1 1
FROM dbo.PROJECTS_TESTS AS ps WITH (NOLOCK)
WHERE ps.PROJECTID = tC.projectId
AND ps.ANOTHERTID = tC.anotherLink
AND ps.DOMAINID = tC.DOMAINID
AND ps.is_test = tC.test_project
)
I think you'd be better served by doing a JOIN than EXISTS. Depending on the cardinality of your join condition (currently in your WHERE) you might need DISTINCT in there too.
INSERT INTO dbo.PROJECTS_TESTS ( PROJECTID, ANOTHERTID, DOMAINID, is_test)
SELECT <maybe distinct> tC.* FROM #temp_Test AS tC
LEFT OUTER JOIN FROM dbo.PROJECTS_TESTS AS ps on
ps.PROJECTID = tC.projectId
AND ps.ANOTHERTID = tC.anotherLink
AND ps.DOMAINID = tC.DOMAINID
AND ps.is_test = tC.test_project
where ps.PROJECT ID IS NULL
or something like that

SQL updating master table with updates from update table

I am trying to update my master table from my updates table, What it wrong with the below query.
UPDATE master_table
SET master_table.description = master_table_import.description
FROM master_table_import
WHERE master_table.user_id = master_table_import.user_id
You are missing a join between the tables. Try something like this:
UPDATE mt SET mt.Description = mti.Description
FROM master_table mt
INNER JOIN master_table_import mti
ON mt.user_id = mti.user_id;
It is always good idea to use aliases for tables. To update you have join your target table with source table:
UPDATE mt
SET description = mti.description
FROM master_table mt INNER JOIN master_table_import mti
WHERE mt.user_id = mti.user_id

SQL Server update statement error

I am writing a script to update the duplicate contact and all its tables where it is referenced.
One of the update statements that I have is the following:
/* update the contactid, and the compcontactid on the compcontact table */
UPDATE cmpc
SET cmpc.contactid = tt.contactid,
cmpc.compcontactid = (SELECT MAX(cc.compcontactid)
FROM compcontact cc
INNER JOIN #tempDupContacts tdup ON tdup.contactid = cc.contactid
INNER JOIN #tempTable tt ON tt.namefml = tdup.namefml)
FROM compcontact cmpc
INNER JOIN #tempDupContacts tdup ON tdup.contactid = cmpc.contactid
INNER JOIN #tempTable tt ON tt.namefml = tdup.namefml
However, when I run the script (script is way tooo long to post here), I get the following error:
Msg 2601, Level 14, State 1, Line 255
Cannot insert duplicate key row in object 'dbo.compcontact' with unique index 'XPKcompcontact'. The duplicate key value is (A000UZCU, A00JTCAP, X00GM2NF).
Can anyone explain why this is happening, and what the fix would be?
Is this because It is attempting to update the value that has the
You are attempting to update a unique key value to a value that already exists in the column. This is not allowed, since each value in the unique key must be unique.
As #sion_corn said, you're trying to update a column having unique key constraint.
Try adding a WHERE clause in your update statement to exclude the value which already exists.
For example:
UPDATE cmpc
SET cmpc.contactid = tt.contactid,
cmpc.compcontactid = (SELECT MAX(cc.compcontactid)
FROM compcontact cc
INNER JOIN #tempDupContacts tdup ON tdup.contactid = cc.contactid
INNER JOIN #tempTable tt ON tt.namefml = tdup.namefml
WHERE cc.compcontactid <> cmpc.compcontactid)
FROM compcontact cmpc
INNER JOIN #tempDupContacts tdup ON tdup.contactid = cmpc.contactid
INNER JOIN #tempTable tt ON tt.namefml = tdup.namefml
WHERE cmpc.contactid <> tt.contactid

Sqlite Update query using a subquery

I have to update table test_test column "testconsent_id" with the id value of table test_groupedconsent, where the patient_id in test_test and patient_id in test_groupedconsent table match and
also creation_date in both table match.
I'm using the below query but getting error -- "near "as": syntax error".
what is wrong with the query?
Update test_test as Tinner join (select id,patient_id,creation_date from test_groupedconsent) as Aon A.patient_id = T.patient_id and A.creation_date = T.creation_dateset T.testconsent_id = A.id;
You cannot use a join directly in an UPDATE statement.
You have to use a correlated subquery to look up the desired value (in the subquery, you can do whatever you want, but in this case, you don't even need a join):
UPDATE test_test
SET testconsent_id = (SELECT id
FROM test_groupedconsent
WHERE patient_id = test_test.patient_id
AND creation_date = test_test.creation_date);
sounds like it took the 'as' after join for the joined tables so either put as in the (... as ...) or bring "ON COMMAND" before as!
like this -> (table1 join table2 on table1.fiel = table2.field) as something
UPDATE TABLE test_test
SET testconsent_id =
(SELECT testconsent_id FROM test_groupedconsent AS A, test_test AS B
WHERE A.patient_id = B.patient_id AND A.creation_date = B.A.creation_date)