I'm trying to make this query work but I'm getting errors:
UPDATE SIRADATOK T1 INNER JOIN (SELECT sirid, MAX(SzolgaltatasokDatumig) AS MAXDATE
FROM ADOK GROUP BY SIRID HAVING MAX(SzolgaltatasokDatumig)<>' ' AND
MAX(SzolgaltatasokDatumig) IS NOT NULL) AS T2 ON T1.SIRID=T2.sirid
SET MegvaltasIdeje = MAXDATE;
The error I get:
"Operation must use updatable query"
I tried this way too:
UPDATE T2
SET T1.MegvaltasIdeje = T2.MAXDATE
FROM SIRADATOK T1, (SELECT sirid, MAX(SzolgaltatasokDatumig) AS MAXDATE
FROM ADOK GROUP BY SIRID HAVING MAX(SzolgaltatasokDatumig)<>' '
AND MAX(SzolgaltatasokDatumig) IS NOT NULL) T2
WHERE T1.SIRID=T2.sirid
but I get:
"Syntax error (missing operator) in query expression 'T2.MAXDATE FROM SIRADATOK T1'."
using June7 comment, I created the following query that it works:
UPDATE
SIRADATOK AS T1
SET
T1.MegvaltasIdeje = MID(
DMAX(
"SzolgaltatasokDatumig", "Adok",
"SzolgaltatasokDatumig<>' '
AND SzolgaltatasokDatumig IS NOT NULL AND SIRID=" & T1.SIRID
),
1,
10
)
WHERE
DMAX(
"SzolgaltatasokDatumig", "Adok",
"SzolgaltatasokDatumig<>' '
AND SzolgaltatasokDatumig IS NOT NULL AND SIRID=" & T1.SIRID
) Is Not Null
And DMAX(
"SzolgaltatasokDatumig", "Adok",
"SzolgaltatasokDatumig<>' '
AND SzolgaltatasokDatumig IS NOT NULL AND SIRID=" & T1.SIRID
)<> ' '
I added an extra MID for the SzolgaltatasokDatumig column, because the column is a DateTime and I need just the Date
I know that is not the best optimized query, but this is just for a one-time use and It works.
thanks again,
June7
Related
I have a query that runs perfectly n SQL server but in Oracle, I get the following error:
ORA-00923: FROM keyword not found where expected
SELECT
MyApps.AppConfigurationId,
MyApps.GUID,
MyApps.AppName,
MyApps.BinaryData,
MyApps.Color1,
MyApps.UserCreatorName,
MyApps.CreatedAt,
MyApps.UserUpdaterName,
MyApps.UpdatedAt,
MyApps.UserPublisherName,
MyApps.LastPublishedAt,
MyApps.AppConfigStateId,
MyApps.AppConfigStateLabel
FROM
(
SELECT
ROW_NUMBER() OVER (
PARTITION BY "OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON15_T266"."ID"
ORDER BY LastestPublishInfo."PUBLISHEDAT" DESC
) AS RowNum,
"OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON15_T266"."ID" AS AppConfigurationId,
"OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON15_T266"."GUID",
"OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON15_T266"."NAME" AS AppName,
"OSADMIN_OSDEV1"."OSUSR_4BQ_APPICON1"."BINARYDATA",
"OSADMIN_OSDEV1"."OSUSR_4BQ_COLORPA1"."COLOR1",
UserCreator."NAME" AS UserCreatorName,
"OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON15_T266"."CREATEDAT",
UserUpdater."NAME" AS UserUpdaterName,
"OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON15_T266"."UPDATEDAT",
LastestPublishInfo.PublisherName AS UserPublisherName ,
LastestPublishInfo."PUBLISHEDAT" AS LastPublishedAt,
"OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON15_T266"."APPCONFIGSTATEID",
"OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON20"."LABEL" AS AppConfigStateLabel
FROM "OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON15_T266" JOIN "OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON20"
ON ( "OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON15_T266"."APPCONFIGSTATEID" = "OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON20"."ID" AND
"OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON15_T266"."PARENTAPPCONFIGID" IS NULL AND
"OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON15_T266"."ISACTIVE" = 1
)
JOIN "OSADMIN_OSDEV1"."OSSYS_USER_T266" UserCreator
ON ( "OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON15_T266"."CREATEDBY" = UserCreator."ID")
LEFT JOIN "OSADMIN_OSDEV1"."OSSYS_USER_T266" UserUpdater
ON ( "OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON15_T266"."UPDATEDBY" = UserCreator."ID")
LEFT JOIN "OSADMIN_OSDEV1"."OSUSR_4BQ_COLORPA1"
ON( "OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON15_T266"."COLORPALETTEID" = "OSADMIN_OSDEV1"."OSUSR_4BQ_COLORPA1"."ID")
LEFT JOIN
(
SELECT "OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON16"."APPCONFIGURATIONID",
"OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON16"."PUBLISHEDAT",
UserPublisher."NAME" AS PublisherName
FROM "OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON16" LEFT JOIN "OSADMIN_OSDEV1"."OSSYS_USER_T266" UserPublisher
ON ( "OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON16"."PUBLISHEDBY" = UserPublisher."ID")
WHERE "OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON16"."PUBLISHEDAT" IS NOT NULL
AND
"OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON16"."ISACTIVE" = 0
) LastestPublishInfo
ON (LastestPublishInfo.AppConfigurationId = "OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON15_T266"."ID")
LEFT JOIN "OSADMIN_OSDEV1"."OSUSR_4BQ_APPICON1"
ON( "OSADMIN_OSDEV1"."OSUSR_4BQ_APPICON1"."APPCONFIGURATIONID" = "OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON15_T266"."ID" )
) MyApps
WHERE MyApps.RowNum = 1
ORDER BY
MyApps.UpdatedAt DESC
I already tried to check syntax follow some articles but I'm not able to solve it.
Added " around de the aliases, tried to replace attribute names in the subquery not to match the parent but nothing worked.
Hope you could help me with this since I went to the point I have no clue about what to try next!
Cheers
Using the following web: https://rextester.com/l/oracle_online_compiler
I tryed your SQL and got the same error as you.
I then started simplifing it up to:
SELECT 1 AS RowNum
FROM blah
and still getting the same error.
I think RowNum is a reserved word in Oracle, you can not use it as a field alias.
Please help me in the code, I get an error
Incorrect syntax near the keyword 'FROM'
SELECT
produkt.Twr_Kod as kod,
(SELECT
ISNULL(SUM(zasoby.TwZ_Ilosc), 0) + ISNULL(SUM(trs_ilosc), 0)
FROM
cdn.TraSElem
WHERE
trs_typ = 3 AND TrS_TrEIdWydania = 0
AND TrS_DataOpe > GETDATE() AND zasoby.TwZ_TwrId = TrS_TwrId
AND zasoby.TwZ_MagId = 1
FROM
CDN.TwrZasoby as zasoby
WHERE
zasoby.Twz_TwrId = product.Twr_twrid) AS zasoby,
CONVERT(NUMERIC(10, 0), produkt.Twr_IloscMin) AS ilosc_minimalna
FROM
CDN.Towary AS product
LEFT JOIN
CDN.Towary AS produkt ON product.Twr_TwrId = produkt.Twr_TwrId
GROUP BY
product.Twr_TwrId, produkt.Twr_Kod, produkt.Twr_IloscMin
ORDER BY
kod
SQLs kinda need to look like this:
SELECT columns FROM tables_or_queries WHERE predicates GROUP BY grouping_keys ORDER BY columns
This pattern can be nested both inside the SELECT region and the FROM region:
SELECT
column1,
(SELECT columnA FROM table WHERE where_predicates) as column2,
column3
FROM
table
INNER JOIN
(SELECT columnA, columnB FROM table WHERE where_predicates) as nested_query
ON join_predicates
WHERE
where_predicates
But you can't have multiple FROM regions etc.. To get more targeted advice youre going to have to tell us what you're hoping your query does.. Right now, we can only say to sort out the basic syntax errors the compilation process is complaining about
You seem to have multiple FROM statements in a single SELECT statement. Every SELECT can have only one FROM clause. Glancing through your query, I think it's going to give you performance nightmares. Use joins instead of using inline queries. Will improve performance.
SELECT
produkt.Twr_Kod as kod,
(SELECT
ISNULL(SUM(zasoby.TwZ_Ilosc), 0) + ISNULL(SUM(trs_ilosc), 0)
**FROM
cdn.TraSElem
WHERE
trs_typ = 3 AND TrS_TrEIdWydania = 0
AND TrS_DataOpe > GETDATE() AND zasoby.TwZ_TwrId = TrS_TwrId
AND zasoby.TwZ_MagId = 1
FROM
CDN.TwrZasoby as zasoby**
WHERE
zasoby.Twz_TwrId = product.Twr_twrid) AS zasoby,
CONVERT(NUMERIC(10, 0), produkt.Twr_IloscMin) AS ilosc_minimalna
FROM
CDN.Towary AS product
LEFT JOIN
CDN.Towary AS produkt ON product.Twr_TwrId = produkt.Twr_TwrId
GROUP BY
product.Twr_TwrId, produkt.Twr_Kod, produkt.Twr_IloscMin
ORDER BY
kod
I am trying to update two fields in my table tbl_prev_data using subquery because I had to use the aggregate SUM. But I don't know why it says
Incorrect Syntax near 't'`
where t is a temp variable that holds the results of the subquery. Am I missing something ? Below is my query.
UPDATE tbl_prev_data
SET Original_Value = t.Original_Value
SET Pre_Val = t.PreWDV
FROM tbl_prev_data
INNER JOIN
(SELECT
Asset_Group_Code,
ISNULL(SUM(Original_Value),0) AS 'Original_Value',
ISNULL(SUM(Prev_Val),0) AS 'PreWDV'
FROM
tbl_Asset_Master
GROUP BY
Asset_Group_Code, DOP
HAVING
(YEAR(DOP) != YEAR(GETDATE()) AND MONTH (DOP) > 3) t ON tbl_prev_data.Asset_Group_Code = t.Asset_Group_Code
You have to specify SET only once:
UPDATE tbl_prev_data
SET Original_Value = t.Original_Value,
Pre_Val = t.PreWDV
FROM tbl_prev_data INNER JOIN ...
Other than that you also need to remove the redundant left parenthesis from the HAVING clause:
HAVING (YEAR(DOP) != YEAR(GETDATE()) AND MONTH (DOP) > 3
-------^
it seems a problem with closing bracket.
(SELECT Asset_Group_Code, ...
is not closed with ) before t.
I have this sql:
Select
t1.field01,
t1.field02,
t2.name02,
t2.surname02,
from
lib01/FirstFile as t1,
lib02/SecondFile (lib02/ThirdFile) as t2
where
t1.field01 = t2.field02 (or t3.filed02)
I need to have the condition that if t1.field02 is equal to "AX" I have to read the second file (lib02/SecondFile), if t1.field02 is equal to "BX" I have to read by the third file (lib02/ThirdFile).
Select
CASE
WHEN ((Select t1.field02 from lib01/FirstFile as t1 ) = AX)
THEN Select t2.name02, t2.surname02
from lib02/SecondFile (lib02/ThirdFile) as t2
CASE
WHEN ((Select t1.field02 from lib01/FirstFile as t1 ) = BX)
THEN Select t3.name02, t3.surname02
from (lib02/ThirdFile) as t3
END
Change the code after each THEN clause if that retrieve is not what you want, but that is the logic.
And try to avoid use old syntax for join
Trying to execute this query I've this error:
Update failed. 3810: Column/parameter 'edw_workarea.A.A' does not exist.
I'm using Teradata database.
UPDATE A
FROM EDW_WORKAREA.bs A,
(
SELECT stg.ccir_ind_id,
stg.orig_ccir_id
FROM EDW_WORKAREA.se stg
INNER JOIN best_svc bc
ON stg.ccir_ind_id = bc.ccir_ind_id
WHERE stg.deliverability_score < '6'
AND stg.ccir_ind_id IN (SELECT stg.ccir_ind_id
FROM EDW_WORKAREA.se stg
WHERE stg.orig_ccir_id IS NULL
OR stg.orig_ccir_id = ''
GROUP BY
stg.ccir_ind_id
HAVING COUNT(*) = 1)
) t
SET A.orig_ccir_id = t.orig_ccir_id
WHERE A.ccir_ind_id = t.ccir_ind_id;
All the tables and columns exist in database. And subquery in t was executing successfully alone.
Can any one point me where the error is ?
On Teradata, you shouldn't qualify your columns in the SET clause, so change your SQL to something like:
update EDW_WORKAREA.bs
from (
select ...
) t
set orig_ccir_id = t.orig_ccir_id
where EDW_WORKAREA.bs.ccir_ind_id = t.ccir_ind_id;