I am getting the
ORA-00920: invalid relational operator
message for below query. Please help on this.
MERGE INTO PS_CT_IQN_ACC_STG STG USING PS_CTS_IQN_BU_RATE RT ON (STG.BUSINESS_UNIT = RT.BUSINESS_UNIT)
WHEN MATCHED THEN
UPDATE
SET STG.STANDARD_RATE = RT.STANDARD_RATE,
STG.STANDARD_HOURS = RT.HOURS_PER_DAY,
STG.CURRENCY_CD = RT.CURRENCY_CD
WHERE STG.PROCESS_INSTANCE = 22195604
AND RT.EFFDT =
(SELECT MAX(EFFDT)
FROM PS_CTS_IQN_BU_RATE RT1
WHERE RT.BUSIN ESS_UNIT = RT1.BUSINESS_UNIT
AND RT1.EFFDT <= sysdate
)
AND STG.STANDARD_RATE = 0
AND STG.STANDARD_HOURS = 0
AND STG.BUSINESS_UNIT IN
( SELECT DISTINCT BUSINESS_UNIT FROM PS_CTS_IQN_BU_RATE
);
Try to run this query seperate first
SELECT MAX(EFFDT)
FROM PS_CTS_IQN_BU_RATE RT1
WHERE RT.BUSIN ESS_UNIT =
RT1.BUSINESS_UNIT
AND RT1.EFFDT <= sysdate
Use IN here, I guess max are multiples by the above query
..... AND RT.EFFDT = (select max...)
I need to update the column of a temp table from the count of a field from another table. I was trying to do this with the following query, but I get a syntax error.
How should I write this query?
Thanks!
UPDATE #ResultadosTest
SET PalletsReservados = COUNT(pe.paen_numero)
FROM #ResultadosTest RT, dba.spro_palletencab pe
WHERE pe.pate_tempor = RT.pate_tempor
AND pe.expo_codigo = RT.expo_codigo
AND pe.WeekLinId = RT.WeekLinId
AND pe.plde_codigo = RT.plde_codigo
AND pe.paen_estado = 1
AND ISNULL(pe.LoteCargaId, 0) <> 0
AND ISNULL(pe.PREMOPID, 0) <> 0
GROUP BY
pe.pate_tempor, pe.expo_codigo, pe.WeekLinId, pe.plde_codigo
The error is:
Line 10, Incorrect syntax near the keyword 'GROUP' (42000,156)
You can't directly use an aggregate function at update statement. Instead you can you a derived table
Update #ResultadosTest
SET PalletsReservados = ppp.countpaen_numero
From (Select Count(pe.paen_numero) as countpaen_numero ,pe.pate_tempor,spro_palletencab,........
FROM #ResultadosTest RT,dba.spro_palletencab pe
Where pe.pate_tempor=RT.pate_tempor
AND pe.expo_codigo=RT.expo_codigo
AND pe.WeekLinId=RT.WeekLinId
AND pe.plde_codigo=RT.plde_codigo
AND pe.paen_estado=1 AND IsNull(pe.LoteCargaId,0)<>0 AND IsNull(pe.PREMOPID,0)<>0
GROUP BY pe.pate_tempor,pe.expo_codigo,pe.WeekLinId,pe.plde_codigo ) ppp
Where ppp.pate_tempor = Field..
First group, then join
Update #ResultadosTest
SET PalletsReservados = pe.Cnt
FROM #ResultadosTest RT
JOIN (
SELECT pate_tempor, expo_codigo, WeekLinId, plde_codigo, Count(pe.paen_numero) cnt
FROM dba.spro_palletencab pe
WHERE paen_estado=1 AND IsNull(LoteCargaId,0)<>0 AND IsNull(PREMOPID,0)<>0
GROUP BY pate_tempor,expo_codigo,WeekLinId,plde_codigo
) pe ON pe.pate_tempor=RT.pate_tempor
AND pe.expo_codigo=RT.expo_codigo
AND pe.WeekLinId=RT.WeekLinId
AND pe.plde_codigo=RT.plde_codigo;
should work like:
UPDATE RT
SET PalletsReservados = pe.ctr
FROM #ResultadosTest RT
INNER JOIN
(
SELECT pate_tempor
,expo_codigo
,WeekLinId
,plde_codigo
,Count(pe.paen_numero) as ctr
FROM dba.spro_palletencab
WHERE paen_estado = 1
AND ISNULL(LoteCargaId,0) <> 0
AND ISNULL(PREMOPID,0) <> 0
GROUP BY pe.pate_tempor
,pe.expo_codigo
,pe.WeekLinId
,pe.plde_codigo
) pe
ON pe.pate_tempor = RT.pate_tempor
AND pe.expo_codigo = RT.expo_codigo
AND pe.WeekLinId = RT.WeekLinId
AND pe.plde_codigo = RT.plde_codigo
I am here trying to use UPDATE query with JOIN and SELECT, But I am facing 3706 error.
here is the error line -
UPDATE A0619IL3549_D_RPT.RPT_IS_ELIGIBILITY A2
FROM
(SELECT A.REP_CD, A.GEO_CD,A.MONTH_ID, B.IC_PRD AS IC_PAYOUT_FLAG,
COALESCE(CASE
WHEN UPPER(B.IC_PRD) = 'SEMESTERLY' THEN A.CURR_SEMESTER_FLAG
WHEN UPPER(B.IC_PRD) = 'QUARTERLY' THEN A. CURR_QUARTER_FLAG
ELSE '0'
END,'0') IC_PAYOUT_FLAG
FROM
A0619IL3549_D_RPT.RPT_IS_ELIGIBILITY A,
(
SELECT DISTINCT LEFT(B. REP_CD, POSITION('-' IN B.REP_CD)-1) AS REP_CD,C.GEO_CD,IC_PRD FROM
A0619IL3549_D00_IC_MAIN.ICDM_GEO_REP_PROD A, A0619IL3549_D00_IC_MAIN.ICDM_DIM_REP B,A0619IL3549_D00_IC_MAIN.ICDM_DIM_GEO_HIER C
WHERE
A.REP_SK=B.REP_SK
AND A.GEO_SK=C.GEO_SK
AND A.RUN_ID = (SELECT MAX(RUN_ID) FROM A0619IL3549_D00_IC_MAIN.ICDM_GEO_REP_PROD)
) B
WHERE
A.REP_CD = B.REP_CD
AND
A.GEO_CD = B.GEO_CD)A1
SET A2.IC_PAYOUT_FALG = A1.IC_PAYOUT_FLAG
WHERE
A1.REP_CD = A2.REP_CD
AND
A1.MONTH_ID = A2.MONTH_ID
AND
A.GEO_CD = A2.GEO_CD
LEFT is not a valid Teradata function before TD15.10, it's ODBC SQL, which is automatically translated, but only for SELECT.
Use valid syntax instead:
SUBSTRING(B. REP_CD FROM 1 FOR POSITION('-' IN B.REP_CD)-1)
I am new to SQLDeveloper. Please help me in correcting the merge syntax.I want to merge the data from act_sl tavle into act_sls_0 table
I am using 2 joins in From Clause.
But I am Getting Errors :
Error(13,13): PL/SQL: ORA-00969: missing ON keyword
Error(4,1): PL/SQL: SQL Statement ignored
merge into act_sls_0 using(
( select * from
(select a_0.asp, gadim.uic as ga, pmm_styleid, week, colorcode , sizecode, storenum, units_asly,retail_asly,
cost_asly ,units_asregly,retail_asregly,units_asmkdly,retail_asmkdly,units_as_pln,retail_as_pln
from act_sls
join dimension w on w.externalkey = 'W'||substr(WEEK,3,2)||'_'||substr(WEEK,5,2)
join a_0 on ( w.externalkey between startweek and endweek)
join dimension strdim on strdim.externalkey = (case when length(storenum) <= 4 then RPAD('STR-', 8 - length(storenum), '0') else 'STR-' END) || storenum
join store_info_0 str on store = strdim.uic
join dimension gadim on gadim.externalkey = decode(store_type, 'RETAIL', 'GA-01', 'ECOM', 'GA-02', '')
where trendweeks < 6)t1
join(select distinct asp, product, ga, color, sstyl_shade, pmm_styleid from aplc_1 aplc
join apc_1 apc using (asp,product,color)
where activeitem = 1 and ga!=0 and color != 0)t2
on (t1.asp = t2.asp and
t1.pmm_styleid = t2.pmm_styleid
and (case when length(t1.colorcode) <= 4 then RPAD('SHD-', 8 - length(t1.colorcode), '0') else 'SHD-' END) || t1.colorcode = t2.sstyl_shade
and t1.ga = t2.ga))src
on (trg.asp = src.asp and trg.pmm_styleid = src.pmm_styleid and trg.colorcode = src.colorcode and trg.ga = src.ga)
when matched THEN
update SET
when matched THEN
update SET
trg.UNITS_ASLY = src.UNITS_ASLY,
trg.RETAIL_ASLY = src.RETAIL_ASLY,
trg.COST_ASLY = src.COST_ASLY,
trg.UNITS_ASREGLY = src.UNITS_ASREGLY,
trg.RETAIL_ASREGLY = src.RETAIL_ASREGLY,
trg.UNITS_ASMKDLY = src.UNITS_ASMKDLY,
trg.RETAIL_ASMKDLY = src.RETAIL_ASMKDLY,
trg.UNITS_AS_PLN = src.UNITS_AS_PLN,
trg.RETAIL_AS_PLN = src.RETAIL_AS_PLN
when not matched then insert
(trg.asp,trg.ga, trg.pmm_styleid, trg.colorcode,trg.sizecode,trg.storenum,trg.week,trg.UNITS_ASLY,trg.RETAIL_ASLY ,trg.COST_ASLY , trg.UNITS_ASREGLY ,trg.RETAIL_ASREGLY ,
trg.UNITS_ASMKDLY ,trg.RETAIL_ASMKDLY ,trg.UNITS_AS_PLN ,trg.RETAIL_AS_PLN )
values
(src.asp,src.ga, src.pmm_styleid, src.colorcode,src.sizecode,src.storenum,src.week,src.UNITS_ASLY,src.RETAIL_ASLY,src.COST_ASLY,src.UNITS_ASREGLY,
src.RETAIL_ASREGLY, src.UNITS_ASMKDLY, src.RETAIL_ASMKDLY, src.UNITS_AS_PLN, src.RETAIL_AS_PLN);
The format for a merge statement is:
merge into <target_table> tgt
using <table_name or subquery> src
on (<join conditions between the target and source datasets>)
when matched then
update set ...
when not matched then
insert (...)
values (...);
Quite apart from the fact that your source subquery is incorrect (there are errors around where the t1 and t2 are; too many brackets, inappropriately trying to alias something etc), whilst you have join conditions inside your subquery, you're completely missing the ON clause for the merge itself.
You need to define the join conditions that match the data returned by your source subquery to your target table.
Now the error is : src.ga - Invalid Identifier
Although I have ga table in Trg table and src table .
merge into act_sls_0 trg using( select * from
(select a_0.asp, gadim.uic as ga, pmm_styleid, week, colorcode , sizecode, storenum, units_asly,retail_asly,
cost_asly ,units_asregly,retail_asregly,units_asmkdly,retail_asmkdly,units_as_pln,retail_as_pln
from act_sls
join dimension w on w.externalkey = 'W'||substr(WEEK,3,2)||'_'||substr(WEEK,5,2)
join a_0 on ( w.externalkey between startweek and endweek)
join dimension strdim on strdim.externalkey = (case when length(storenum) <= 4 then RPAD('STR-', 8 - length(storenum), '0') else 'STR-' END) || storenum
join store_info_0 str on store = strdim.uic
join dimension gadim on gadim.externalkey = decode(store_type, 'RETAIL', 'GA-01', 'ECOM', 'GA-02', '')
where trendweeks < 6)t1
join(select distinct asp, product, ga, color, sstyl_shade, pmm_styleid from aplc_1 aplc
join apc_1 apc using (asp,product,color)
where activeitem = 1 and ga!=0 and color != 0)t2
on (t1.asp = t2.asp and
t1.pmm_styleid = t2.pmm_styleid
and (case when length(t1.colorcode) <= 4 then RPAD('SHD-', 8 - length(t1.colorcode), '0') else 'SHD-' END) || t1.colorcode = t2.sstyl_shade
and t1.ga = t2.ga))src
on (trg.asp = src.asp and trg.pmm_styleid = src.pmm_styleid and trg.colorcode = src.colorcode and trg.ga = src.ga)
when matched THEN
update SET
trg.UNITS_ASLY = src.UNITS_ASLY,
trg.RETAIL_ASLY = src.RETAIL_ASLY,
trg.COST_ASLY = src.COST_ASLY,
trg.UNITS_ASREGLY = src.UNITS_ASREGLY,
trg.RETAIL_ASREGLY = src.RETAIL_ASREGLY,
trg.UNITS_ASMKDLY = src.UNITS_ASMKDLY,
trg.RETAIL_ASMKDLY = src.RETAIL_ASMKDLY,
trg.UNITS_AS_PLN = src.UNITS_AS_PLN,
trg.RETAIL_AS_PLN = src.RETAIL_AS_PLN
when not matched then insert
(trg.asp,trg.ga, trg.pmm_styleid, trg.colorcode,trg.sizecode,trg.storenum,trg.week,trg.UNITS_ASLY,trg.RETAIL_ASLY ,trg.COST_ASLY ,trg.UNITS_ASREGLY ,trg.RETAIL_ASREGLY ,
trg.UNITS_ASMKDLY ,trg.RETAIL_ASMKDLY ,trg.UNITS_AS_PLN ,trg.RETAIL_AS_PLN )
values
(src.asp,src.ga, src.pmm_styleid, src.colorcode,src.sizecode,src.storenum,src.week,src.UNITS_ASLY,src.RETAIL_ASLY,src.COST_ASLY,src.UNITS_ASREGLY,
src.RETAIL_ASREGLY, src.UNITS_ASMKDLY, src.RETAIL_ASMKDLY, src.UNITS_AS_PLN, src.RETAIL_AS_PLN);
commit;