Sqlite Update query using a subquery - sql

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)

Related

SQL UPDATE statement with LEFT JOIN, GROUP BY and HAVING?

I need to update some rows in a table. I've created a Select statement to make sure I've got the rows I wanted to select.
I want to update task_status_id in the table task, and I've tried in various ways but always end up with a syntax error and have honestly no idea how to do so even though I've tried to follow others examples by using INNER JOIN and putting the select statement in parenthesis. Any help would be appreciated.
UPDATE statement to merge with the SELECT statement.
UPDATE task
SET task_status_id = (SELECT task_status_id
FROM task_status
WHERE task_type_id = 1
AND name = 'Completed');
WHERE
SELECT
t.task_id
FROM task t
LEFT JOIN user u
ON t.user_id = u.user_id
LEFT JOIN contract co
ON u.user_id = co.user_id
LEFT JOIN task_status ts
ON t.task_status_id = ts.task_status_id
WHERE co.status = 'Closed' AND
t.task_type_id = 1 AND
t.task_status_id != (SELECT task_status_id
FROM task_status
WHERE task_type_id = 1
AND name = 'Completed')
GROUP BY t.task_id
HAVING count(t.contract_id) <= 2;
First of all, it doesn't make sense to use LEFT JOIN contract co and then filter results using co.status = 'Closed', because if you're going to filter by a column from a joined table then you should use INNER JOIN (unless you're comparing to null in the filter).
Secondly, syntax here is incorrect - you should use not in instead of !=
AND t.task_status_id != (SELECT task_status_id
FROM task_status
WHERE task_type_id = 1
AND name = 'Completed')
However, since you already joined the task_status table you can replace the above block of code with the following (assuming that task_status_id is a unique column):
AND ts.name != 'Completed'
Either way, you should post sample data and expected result.

Update Row, Inner Join With Getting Top Row

Using MariaDB
I am trying to update the membershipRenewDate(In officiantsDetails Table) using the MAX value from renewDate(in officiantsRenewals Table)
Inner Join on officiant_id (same column name for both tables)
I have something like this but am getting syntax error.
UPDATE officiantsDetails offd
SET offd.membershipRenewDate = offr.renewDate
FROM (SELECT TOP (1) renewDate, officiant_id FROM officiantsRenewals ORDER BY renewDate DESC ) as offr
WHERE offd.officiant_id = offr.officiant_id
You should be able to use a Common Table Expression (CTE) for this. You use the WITH clause to define your "subquery", and then INNER JOIN the CTE to the table you want to update. It would look something like this:
WITH Top1RenewDate AS
(
SELECT TOP (1)
renewDate,
officiant_id
FROM officiantsRenewals
ORDER BY renewDate DESC
)
UPDATE offd
SET offd.membershipRenewDate = offr.renewDate
FROM officiantsDetails offd
INNER JOIN Top1RenewDate offr ON offd.officiants_id = offr.officiants_id
This same syntax will also work for SQL Server, although you'd need to add a ; before the WITH keyword on that system.
The syntax you are using is not MariaDB. In MariaDB, you can use a JOIN:
UPDATE officiantsDetails offd JOIN
(SELECT renewDate, officiant_id
FROM officiantsRenewals
ORDER BY renewDate DESC
LIMIT 1
) offr
ON offd.officiant_id = offr.officiant_id
SET offd.membershipRenewDate = offr.renewDate;
The syntax you are using is more reminiscent of SQL Server.

How to update a table with joining two other tables

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.

UPDATE query with inner joined query

I haved saved SELECT query. I need create update query to update table field with value from saved select query.
Im getting error "Operation must use an updatable query".
Problem is that saved select query result not contain primary key.
UPDATE [table] INNER JOIN
[saved_select_query]
ON [table].id_field = [saved_select_query].[my_field]
SET [table].[target_field] = [saved_select_query]![source_field]);
Im also try with select subquery instead of inner join, but same error.
Perhaps a DLookUp() will do the trick:
UPDATE [table] SET
[target_field] = DLookUp("source_field", "saved_select_query", "my_field=" & id_field)
... or, if the joined field is text ...
UPDATE [table] SET
[target_field] = DLookUp("source_field", "saved_select_query", "my_field='" & id_field & "'")
I'm not sure I completely understand what you are asking.
If you are asking what syntax to use when performing an update with an inner join.
UPDATE tableAlias
SET Column = Value
FROM table1 as tableAlias
INNER JOIN table2 as table2Alias on tableAlias.key = table2Alias.key
your query is incorrect , try this.
UPDATE [table]
SET [table].[target_field] = [table2].
[source_field])
from (select *from
[table] INNER JOIN
[saved_select_query] a
ON [table].id_field =
a.[my_field] )
table2
I got it to work using the following:
UPDATE [table]
SET [table].[target_Field] = (SELECT [source_field] FROM [Saved_Select_Query]
WHERE [table].[id_field] = [Saved_Select_Query].[my_field])
You can't use an JOIN on an UPDATE statement directly, so you need to join the tables in a subquery.

Invalid SQL Query

I have the next query that in my opinion is a valid one, but I keep getting error telling me that there is a proble on "WHERE em.p4 = ue.p3" - Unknown column 'ue.p3' in 'where clause'.
This is the query:
SELECT DISTINCT ue.p3
FROM
table1 AS ue INNER JOIN table2 AS e
ON ue.p3 = e.p3
WHERE
EXISTS(
SELECT 1 FROM (
SELECT (COUNT(*) >= 1) AS MinMutual
FROM table4 AS smm
WHERE
smm.p1 IN (
SELECT sem.p3 FROM table3 AS sem
INNER JOIN table2 AS em ON sem.p3 = em.p3
WHERE em.p4 = ue.p3 AND
sem.type = 'friends' AND em.p2 = 'normal' ) AND
smm.p5 IN (
15000,15151
)
) AS Mutual WHERE
Mutual.MinMutual = TRUE) LIMIT 11
If I execute the sub-query which is inside the EXISTS function, everything is O.K.
PLEASE HELP!
The reason for the error is that you can only reference one subquery layer down when correlating. Look at where the ue alias is defined, and count the number of FROM clauses until to you reach the next reference.
I re-wrote your query as:
SELECT DISTINCT ue.p3
FROM table1 AS ue
JOIN table2 AS e ON ue.p3 = e.p3
WHERE EXISTS(SELECT 1 AS MinMutual
FROM table4 AS smm
JOIN TABLE3 sem ON sem.p3 = smm.p1
AND sem.type = 'friends'
JOIN TABLE2 em ON em.p3 = sem.p3
AND em.p3 = ue.p3
AND em.p2 = 'normal'
WHERE smm.p5 IN (15000,15151)
GROUP BY ? --needs a group by clause, in order to use HAVING
HAVING COUNT(*) >= 1)
LIMIT 11
EXISTS returns true if satisfied -- it doesn't evaluate based on the subquery returning "true". There's no need for the additional subquery you have (which was causing problems anyway).
AFAIK, this kind of correlated query is not doable in mysql as of now. Join to a derived table as opposed to using the exists.