I'm trying to update a column in a table that makes a join on itself to filter out data.
Initially, the piece of code was for SQL Server and I tried to change it so it run in Vertica. I'm getting this error:
ERROR: Syntax error at or near "inner"
update REPORT.sub_2018_ALL a
inner join REPORT.sub_2018_ALL p
on a.MBR_ID= p.MBR_ID and a.NAME = p.NAME
set RESULT = 'F'
where p.STATUS_REASON = 'Submitted' and a.REVIEW_RESULT is null
I'm not sure if it's because of the alias or if it's not possible to update when a table joins on itself. Any help is appreciated.
I think you want something like this:
update REPORT.sub_2018_ALL a
set RESULT = 'F'
where a.REVIEW_RESULT is null and
exists (select 1
from REPORT.sub_2018_ALL p
where a.MBR_ID = p.MBR_ID and
a.NAME = p.NAME and
p.STATUS_REASON = 'Submitted'
);
For the record, your original syntax would not work on SQL Server, although it might work on MySQL.
Related
I am getting an error on the query I'm running to get a count in MariaDB. This is the error:
Error Code: 1060. Duplicate column name 'id_number'
And this is my SQL code:
SELECT COUNT(*) as count FROM (
SELECT * FROM ((cr.customers
INNER JOIN (progress_notes_details
INNER JOIN progress_notes ON progress_notes_details.progress_note_id = progress_notes.id_number)
ON customers.id_number = progress_notes.c_id)
INNER JOIN open_balances ON progress_notes_details.id_number = open_balances.progress_notes_detail_id)
INNER JOIN
customer_payer_xref ON customers.id_number = customer_payer_xref.c_id
WHERE
(((progress_notes_details.qb_isbillable) IS NULL
OR (progress_notes_details.qb_isbillable) <> 1)
AND ((progress_notes_details.date_of_visit) BETWEEN coverage_start AND coverage_end)
AND ((progress_notes_details.dynamics_status) = 3)
AND ((customer_payer_xref.payer_id) = 23)
AND ((customer_payer_xref.primary_secondary_account_type) = 1))
) AS qdat
Can this be resolved via aliases? If so, it's unclear to me where to add them. In the main query? In the subquery?
Also, to clarify, I just inherited this code - and yes, it's bracket-happy.
Alias customers table as c and use it as c.id_number at one of the places it will remove that duplicate error as this is the only table you are using multiple times with idnumber hence duplicate
Remove the outer query:
SELECT COUNT(*)
FROM ((cr.customers . . .
Clearly, you have tables with the same column name. This causes a problem with SELECT *.
All the parentheses are probably not needed for the JOINs as well.
I have three tables(A, B, and C) and would like to update null values in A.appointment_id with values in C.tc_appointment_id. Table A and C can be joined via table B. Blue arrows represent joins, the red arrow represents the update I am trying to achieve.
I am able to join the three tables together and have tried to modify my select statement into an update. My successful select statement and unsuccessful update follow.
--Working select
select A.tc_ASN_id,A.appointment_id, B.appt_obj_id, B.appointment_id, C.appointment_id, C.tc_appointment_id from B
join A on B.appt_obj_id = A.asn_id
join C on C.appointment_id = B.appointment_id
where C.appt_status < '9' and A.appointment_id is null;
--Update attempt that ends with SQL Error: ORA-00933: SQL command not properly ended
update asn set appoinmtent_id = ilm_appointments.tc_appointment_id
join ilm_appointment_objects on ilm_appointment_objects.appt_obj_id = asn.asn_id
join ilm_appointments on ilm_appointments.appointment_id = ilm_appointment_objects.appointment_id
where ilm_appointments.appt_status < '9' and asn.appointment_id is null;
The expected result is to update all null values for A.appointment_id to be updated with the values from C.tc_appointment_id.
UPDATE...JOIN syntax is not supported in Oracle. You could use a correlated subquery instead.
Consider:
UPDATE A
SET A.appointment_id = (
SELECT C.tc_appointment_id
FROM B
INNER JOIN C ON C.appointment_id = B.appointment_id
WHERE B.appt_obj_id = A.asn_id
)
WHERE A.appointment_id IS NULL;
Beware that the subquery must return a unique record, otherwise you will get an error like "subquery returned more than one row". Given your sample data this seems to be the OK.
I would like to update columns in Table A based on values in Table B. Using below format, but getting syntax error.
update TableA
set
TableA.MOdule_id =TableB.MOdule_id
from TableA
inner join
TableB
on TableA.end_Slot_id =TableB.Slot_Id
where TableA.Slot_Id = 'AAA';
It would be great help, if anyone can help on this.
A quick search for "informix sql update" returns two reference manual pages that describe the syntax for the UPDATE command. Neither one indicates support for the nonstandard FROM clause.
Standard SQL uses a correlated subquery for the purpose. Your query should look something like
update TableA
set MOdule_id =
(select TableB.MOdule_id
from TableB
on TableA.end_Slot_id = Slot_Id)
where Slot_Id = 'AAA'
and exists (
select 1
from TableB
on TableA.end_Slot_id = Slot_Id
and TableA.Slot_Id = 'AAA'
);
The EXISTS clause ensures that only rows that exist in B are applied to A. Without it, any missing rows would be updated to NULL.
I'm trying to put together a query that updates a field within a table. I'm attempting to run a sub select query that gives me a number, and then use that number that resulted from the sub-query as part of the criteria for the update query.
USE EMMS
Update [2_import_VZW_tbl_SMTN]
set [2_import_VZW_tbl_SMTN].[Client_ID] =[tbl_Foundation_Account].[Client_ID]
where ([tbl_Foundation_Account].[Foundation_Account_ID] =
(Select TOP 1 tbl_Foundation_Account.Foundation_Account_ID
FROM tbl_Foundation_Account
INNER JOIN [2_Import_tbl_AWCDSU]
ON tbl_Foundation_Account.Foundation_Account_ID =
[2_Import_tbl_AWCDSU].[ECPD Profile ID]))
My issue is I keep receiving this error
The multi-part identifier
tbl_Foundation_Account.Foundation_Account_ID" could not be bound.
Am I using the sub-query incorrectly? When I've received this error before, it's been because of some ambiguity in the table or field names, but this time I've checked for all that and it should be fine. Can anyone explain what SQL sin I have committed?
On the error
The multi-part identifier
tbl_Foundation_Account.Foundation_Account_ID" could not be bound.
This is because the table column [tbl_Foundation_Account].[Client_ID] does not exists in the scope of outer UPDATEquery .
The only table the outer query has an inkling about is [2_import_VZW_tbl_SMTN] and it does not have a column like [tbl_Foundation_Account].[Client_ID].
It is akin to writing a column name with a typo or like you said
When I've received this error before, it's been because of some
ambiguity in the table or field names
Please try a query like below.
Note that I am using Inner query syntax and ensuring that a single value is returned by using
select top 1 [Client_ID]
in the inner query. rest of the query syntax is same.
USE EMMS
Update [2_import_VZW_tbl_SMTN]
set [2_import_VZW_tbl_SMTN].[Client_ID] =
(
select top 1 [Client_ID]
from [tbl_Foundation_Account]
where [Foundation_Account_ID] =
(
Select TOP 1 a.Foundation_Account_ID
FROM tbl_Foundation_Account a
INNER JOIN [2_Import_tbl_AWCDSU] b
ON a.Foundation_Account_ID = b.[ECPD Profile ID]
)
)
Another poster submitted this answer earlier, but then deleted it. I was able to try it before they deleted it and it works exactly how I needed it to work. I will use this as the right answer unless someone else can tell me why this is a bad Idea.
USE EMMS
Update [2_import_VZW_tbl_SMTN]
set [2_import_VZW_tbl_SMTN].[Client_ID] = [tbl_Foundation_Account].[Client_ID]
from [tbl_Foundation_Account]
where ([tbl_Foundation_Account].[Foundation_Account_ID] =
(Select TOP 1 tbl_Foundation_Account.Foundation_Account_ID
FROM tbl_Foundation_Account
INNER JOIN [2_Import_tbl_AWCDSU]
ON tbl_Foundation_Account.Foundation_Account_ID = [2_Import_tbl_AWCDSU].[ECPD Profile ID]))
Sorry if the title is unclear. Basically I'm trying to select certain records from multiple tables then update a certain column value for the returned records.
T-SQL Implementation
UPDATE
CUSTOMERS
SET
LIKES_US = 'Y'
FROM
RESTAURANT REST INNER JOIN CUSTOMERS CUST ON REST.LINK_ID = CUST.LINK_ID
WHERE
REST.REST_TYPE = 'Diner' AND CUST.LIKES_US IS NULL
Oracle
UPDATE
(SELECT CUST.LIKES_US
FROM CUSTOMERS CUST INNER JOIN RESTAURANT REST ON CUST.LINK_ID=REST.LINK_ID
WHERE REST.REST_TYPE = 'Diner' AND CUST.LIKES_US IS NULL) NEW_CUST
SET
NEW_CUST.LIKES_US = 'Y';
I am tried doing the same thing in Teradata as I did in Oracle but I get the following error:
Executed as Single statement. Failed [3707 : 42000] Syntax error, expected something like a name or a Unicode delimited identifier or an 'UDFCALLNAME' keyword between the 'UPDATE' keyword and '('.
Elapsed time = 00:00:00.003
STATEMENT 1: Unknown failed.
I looked online for the solution but had no luck.
Have you tried the following syntax with Teradata:
UPDATE CUSTOMERS C1
FROM (SELECT C2.LINK_ID
FROM CUSTOMERS C2
INNER JOIN RESTAURANTS R2
ON C2.LINK_ID = R2.LINK_ID
WHERE R2.REST_TYPE = 'DINER'
AND C2.LIKES_US IS NULL) D1
SET LIKES_US = 'Y'
WHERE C1.LINK_ID = C2.LINK_ID
I think that in this specific case, the below query will perform a little better since it needs one less join.
UPDATE C
FROM CUSTOMERS C, RESTAURANTS R
SET LIKES_US = 'Y'
WHERE
C.LINK_ID = R.LINK_ID
AND R.REST_TYPE = 'DINER'
AND C.LIKES_US IS NULL