ORA-00920: invalid relational operator for Merge - sql

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...)

Related

how to use more than 1 sub query in hive

I am executing the below query getting the error.
FAILED: SemanticException [Error 10249]: Line 13:15 Unsupported SubQuery Expression 'master_cd': Only 1 SubQuery expression is supported.
SELECT
cfs.roll_no,
max(cclas.crdm_cd) as crdm_cd,
max(cclas.kjtm_cd) as kjtm_cd
FROM cust_focus cfs
LEFT JOIN cust_class cclas
ON (cfs.CF_CLAS_NO = cclas.CLAS_NO
AND cfs.DFS_CD = cclas.DFS_CD
AND cclas.D_AREA = 'US'
AND cclas.active_flag = 'Y')
WHERE cfs.roll_no NOT IN (SELECT roll_no FROM class_hist)
AND UPPER(TRIM(cfs.D_AREA)) = 'US'
AND (cfs.master_cd IN (SELECT msk5.msk5_master_cd from msk5_mst_tbl as msk5 WHERE cfs.master_cd=msk5.msk5_master_cd and msk5_m_code=9)
OR cfs.master_cd IS NULL)
group by cfs.roll_no;
Could you please help me how to resolve this error.
Thanks in Advance.
SELECT
cfs.roll_no,
max(cclas.crdm_cd) as crdm_cd,
max(cclas.kjtm_cd) as kjtm_cd
FROM(select cf.* from cust_focus cf
join class_hist ch on cf.roll_no!=ch.roll_no
join msk5_mst_tbl msk5 on cf.master_cd = msk5.msk5_master_cd where
msk5_m_code=9))cfs
LEFT JOIN cust_class cclas
ON (cfs.CF_CLAS_NO = cclas.CLAS_NO
AND cfs.DFS_CD = cclas.DFS_CD
AND cclas.D_AREA = 'US'
AND cclas.active_flag = 'Y')
AND UPPER(TRIM(cfs.D_AREA)) = 'US'
OR cfs.master_cd IS NULL
These many joins would impact the performance though!!
Only multiple join subqueries are supported.
below query works without any issue.
select * from (select id from test where id>10) a
join (select id from test where id>20) b on a.id=b.id;
In your case ,both filters are being used against same table(cust_focus) only otherwise you could have applied filters on different tables like above example.

Teradata 3706, expected something between DISTINCT and LEFT keyword

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)

Hive equivalent of sql update

I am trying to convert below query equivalent to hive.
updating tier_cd in rpt table but hive 0.13 doesn't support update so using the changing to hive equivalent and output is not correct.
UPDATE test_report A
SET TIER_CD = (SELECT B.TIER_CD FROM lk B
WHERE B.CLASS_CD = A.CLASS_CD AND B.RC_TYPE_CD = 'OATS'
AND A.ACPTD_ROE_CT >= B.BEGIN_QT
AND A.ACPTD_ROE_CT <= B.END_QT
AND EFCTV_DT = (SELECT MAX(C.EFCTV_DT) FROM LK C WHERE
C.CLASS_CD = B.CLASS_CD AND C.RC_TYPE_CD = 'OATS' AND
C.TIER_CD = B.TIER_CD AND
C.EFCTV_DT <= ?
AND B.TIER_CD <> 'I')
WHERE A.YEAR_CD = ?
AND A.MONTH_CD = ?;
HIve equivalent is below
select z.*,x.tier_Cd from
(
select * from test_report
where year_Cd=?
and month_Cd=?')z
left outer join
(SELECT B.TIER_CD TIER_cD, A.MKT_CLASS_CD,A.year_cd FROM LK B, test_report A
WHERE B.CLASS_CD = A.CLASS_CD AND B.RC_TYPE_CD = 'OATS'
AND A.ACPTD_ROE_CT >= B.BEGIN_QT
AND A.ACPTD_ROE_CT <= B.END_QT
AND EFCTV_DT = (SELECT MAX(C.EFCTV_DT) FROM LK C WHERE
C.CLASS_CD = B.CLASS_CD AND C.RC_TYPE_CD = 'OATS' AND
C.TIER_CD = B.TIER_CD AND
C.EFCTV_DT <= ?)
AND B.TIER_CD <> 'I')x
on(z.class_Cd=x.Class_Cd)
please assist what's wrong in it.
Hive doesn't support multiple subqueries instead you can connect three tables with only joins. My guess is you're getting an error near select (max

Merge Source Table Into Target Table using subjoins

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;

Update error column parameter does not exist

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;