ClickHouse Unsupported JOIN ON conditions - clickhouse

I've a SQL query that works in PostgreSQL, but doesn't work with Clickhouse. But I want to migrate it from Postgres. Query:
select
u.counter_id,
r.date_of_visit,
sum(r.sessions) as sessions,
sum(r.pageviews) as pageviews,
u.utm_campaign,
u.utm_source,
u.utm_medium,
u.utm_content,
u.utm_term
from
metrika.utm_for_collect u
inner join metrika.utm_sessions_report r on u.counter_id = r.counter_id
and (
u.utm_campaign = r.utm_campaign
or u.utm_campaign is null
)
and (
u.utm_source = r.utm_source
or u.utm_source is null
)
and (
u.utm_medium = r.utm_medium
or u.utm_medium is null
)
and (
u.utm_content = r.utm_content
or u.utm_content is null
)
and (
u.utm_term = r.utm_term
or u.utm_term is null
)
group by
u.counter_id,
r.date_of_visit,
u.utm_campaign,
u.utm_source,
u.utm_medium,
u.utm_content,
u.utm_term
It throws an error:
Unsupported JOIN ON conditions.
Unexpected '(utm_campaign = r.utm_campaign) OR (utm_campaign IS NULL)': While processing (utm_campaign = r.utm_campaign) OR (utm_campaign IS NULL). (INVALID_JOIN_ON_EXPRESSION) (version 22.7.2.15 (official build))
Any ways to fix it?

Related

ora-00933 : oracle update query using join sql command not properly ended

I have a query , when I run the below query in Oracle
update cust_logs el
set batch_id = '3344'
from
client c
join port p on p.pid = c.pid
and
p.flag = 0
where
c.cid = el.cid
and
c.flag = 0
and
p.export = 1
and
el.trans_id is nul
I am getting error as
ORA-00933: sql command not properly ended.
I have tried
merge into cust_logs el
using (
select *
from client c
join port p
on (
p.pid = c.pid
and p.flag = 0
)
) "s"
on ((
c.cid = el.cid
and c.flag = 0
and p.export = 1
and decode(el.trans_id, nul, 1, 0) = 1
))
when matched then update set
batch_id = '3344'
Can you please let me know how to resolve
This is my sql fiddle
http://sqlfiddle.com/#!4/c9166/1
You can't join in an update.
You haven't said what the issue is with your merge attempt. But your on clause referring to the base table instead of the "s" alias. And the select * will cause an ambiguous identifier error as pid appears in both tables. And it's null not nul.
So this might be closer, at least:
merge into cust_logs el
using (
select c.cid, c.flag, p.export
from client c
join port p
on p.pid = c.pid
where p.flag = 0
) s
on (
s.cid = el.cid
and s.flag = 0
and s.export = 1
and decode(el.trans_id, null, 1, 0) = 1
)
when matched then update set batch_id = '3344'
But we don't have your tables or data to verify it.
If batch_id is a numeric column then the last line should be:
when matched then update set batch_id = 3344

Oracle: Value from main query is not available in subquery

I have this query, and one of its column is a subquery that should be bringing a list of values using a listagg function. This list has its starting point as the S.ID_ORGAO_INTELIGENCIA value. The list is a should be, it always has values.
The listagg function is consuming an inline view that uses a window function to create the list.
select *
from (
SELECT DISTINCT S.ID_SOLICITACAO,
S.NR_PROTOCOLO_SOLICITACAO,
S.DH_INCLUSAO,
S.ID_USUARIO,
U.NR_CPF,
OI.ID_MODULO,
OI.ID_ORGAO_INTELIGENCIA,
OI.NO_ORGAO_INTELIGENCIA,
R.ID_ATRIBUICAO,
P.ID_PERMISSAO,
1 AS TIPO_NOTIFICACAO,
(
select LISTAGG(oc6.ID_ORGAO_INTELIGENCIA || '-' || oc6.ord || '-', '; ') WITHIN GROUP (ORDER BY oc6.ord) eai
from (
SELECT oc1.ID_ORGAO_INTELIGENCIA,
oc1.ID_ORGAO_INTELIGENCIA_PAI,
oc1.SG_ORGAO_INTELIGENCIA,
rownum as ord
FROM TB_ORGAO_INTERNO oc1
WHERE oc1.DH_EXCLUSAO is null
-- THE VALUE FROM S.ID_ORGAO_INTELIGENCIA IS NOT AVAILBLE HERE
START WITH oc1.ID_ORGAO_INTELIGENCIA = S.ID_ORGAO_INTELIGENCIA
CONNECT BY prior oc1.ID_ORGAO_INTELIGENCIA_PAI = oc1.ID_ORGAO_INTELIGENCIA
) oc6) aproPrec
FROM TB_SOLICITACAO S
INNER JOIN TB_ORGAO_INTERNO OI ON S.ID_ORGAO_INTELIGENCIA = OI.ID_ORGAO_INTELIGENCIA
INNER JOIN TB_RELACIONAMENTO_ATRIBUICAO R
ON (R.ID_MODULO = OI.ID_MODULO AND R.ID_ORGAO_INTELIGENCIA IS NULL AND
R.ID_SOLICITACAO IS NULL)
INNER JOIN TB_PERMISSAO P
ON (P.ID_USUARIO = :usuario AND P.ID_ORGAO_INTELIGENCIA = :orgao AND
P.ID_ATRIBUICAO = R.ID_ATRIBUICAO)
INNER JOIN TB_USUARIO U ON (U.ID_USUARIO = S.ID_USUARIO)
WHERE 1 = 1
AND U.DH_EXCLUSAO IS NULL
AND P.DH_EXCLUSAO IS NULL
AND S.DH_EXCLUSAO IS NULL
AND OI.DH_EXCLUSAO IS NULL
AND R.ID_ATRIBUICAO IN :atribuicoes
AND P.ID_STATUS_PERMISSAO = 7
AND OI.ID_MODULO = 1
AND S.ID_STATUS_SOLICITACAO IN (1, 2, 5, 6)
and s.ID_ORGAO_INTELIGENCIA in (SELECT DISTINCT o.ID_ORGAO_INTELIGENCIA
FROM TB_ORGAO_INTERNO o
WHERE o.DH_EXCLUSAO IS NULL
START WITH o.ID_ORGAO_INTELIGENCIA = 3
CONNECT BY PRIOR o.ID_ORGAO_INTELIGENCIA = o.ID_ORGAO_INTELIGENCIA_PAI)
);
The problem is that the aproPrec column is always returning null as its result.
If I force the criteria to have the S.ID_ORGAO_INTELIGENCIA hardcoded, the list returns its true value.
If I chance this:
START WITH oc1.ID_ORGAO_INTELIGENCIA = S.ID_ORGAO_INTELIGENCIA
To this:
START WITH oc1.ID_ORGAO_INTELIGENCIA = 311
where 311 is the value that the S.ID_ORGAO_INTELIGENCIA column really has.
Is there a way to make this query works as 'I think' it should work?
To make it work, I changed the subquery by this another one:
(
select qt_.*
from (
SELECT QRY_NAME.*,
rownum as ord
FROM (
SELECT oc1.ID_ORGAO_INTELIGENCIA,
oc1.ID_ORGAO_INTELIGENCIA_PAI,
connect_by_root (oc1.ID_ORGAO_INTELIGENCIA) as root
FROM TB_ORGAO_INTERNO oc1
CONNECT BY NOCYCLE PRIOR oc1.ID_ORGAO_INTELIGENCIA_PAI = oc1.ID_ORGAO_INTELIGENCIA
) QRY_NAME
WHERE root = s.ID_ORGAO_INTELIGENCIA
) qt_
)

Group by query Rising ORA-00934 exception

I have the following query
SELECT
t.orgname,
t.phone,
t.phone2,
t.fax,
t.address1,
t.address2,
t.address3,
t.address4,
t.city,
t.postal,
t.glname,
t.acctvalue,
t.acctname,
t.dateacct,
t.fiscalfrom,
t.fiscalto,
t.folio,
t.piece,
regexp_replace(coalesce(
LISTAGG(t.description, ' || ') WITHIN GROUP(
ORDER BY
t.acctvalue
), ''), '[^[:print:]]', '')
INTO
description,
SUM(t.amtacctcr) AS amtacctcr,
SUM(t.amtacctdr) AS amtacctdr
FROM (
SELECT
org.name AS orgname,
oi.phone,
oi.phone2,
oi.fax,
loc.address1,
loc.address2,
loc.address3,
loc.address4,
loc.city,
loc.postal,
glc.name AS glname,
ev.value AS acctvalue,
ev.name AS acctname,
fa.description,
fa.dateacct,
fa.amtacctcr,
fa.amtacctdr,
--getfactdocumentno(ad_table_id, record_id) AS piece,
(
SELECT
gcs.seqno
FROM
gl_category_sequence gcs
WHERE
gcs.c_period_id = fa.c_period_id
AND gcs.ad_table_id = fa.ad_table_id
AND gcs.record_id = fa.record_id
AND gcs.gl_category_id = glc.gl_category_id
) AS piece,
fiscalyearforperiod(fa.c_period_id, '01/01/', 'dd/MM/yy') AS fiscalfrom,
fiscalyearforperiod(fa.c_period_id, '31/12/', 'dd/MM/yy') AS fiscalto,
p.periodno AS folio
FROM
fact_acct fa
INNER JOIN c_period p ON ( fa.c_period_id = p.c_period_id )
INNER JOIN gl_category glc ON ( fa.gl_category_id = glc.gl_category_id )
INNER JOIN c_elementvalue ev ON ( fa.account_id = ev.c_elementvalue_id )
INNER JOIN ad_org org ON ( fa.ad_org_id = org.ad_org_id )
INNER JOIN ad_orginfo oi ON ( org.ad_org_id = oi.ad_org_id )
INNER JOIN c_location loc ON ( oi.c_location_id = loc.c_location_id )
WHERE
fa.ad_table_id = 318
AND fa.record_id = 1454983
AND fa.c_acctschema_id=1000003
ORDER BY
fa.fact_acct_id
) t
GROUP BY
t.orgname,
t.phone,
t.phone2,
t.fax,
t.address1,
t.address2,
t.address3,
t.address4,
t.city,
t.postal,
t.glname,
t.acctvalue,
t.acctname,
t.dateacct,
t.fiscalfrom,
t.fiscalto,
t.folio,
t.piece
order
by t.acctvalue;
I m getting the ora-00934
But as I see here what I m trying is possible. Example 10.15.
so where I m I wrong ?
Only incorrect code that I can see is the INTO clause.
Please remove the following code from your query and try:
INTO
description,
SUM(t.amtacctcr) AS amtacctcr,
SUM(t.amtacctdr) AS amtacctdr

Problems updating query with inner join

Can anyone please assist in getting this one query to work. I am trying to update status of a column in a table having joined to other tables
Here is the query
update
(select I.account_id, I.sts, I.name_id, CI.CRM_TYPE, I.comments
from PPInters I
inner join DW.CUST CT
on I.account_id = CT.account_id
where
I.sts is null
AND I.comments IS NOT NULL
AND CT.CUSTTYPe = 'INTNL') T
SET T.STS = 'D'
WHERE T.account_id IN (2000208927,380166014,190180447,166078041,105029075 )
I am getting "ORA-01779: cannot modify a column which maps to a non key-preserved table" error
What I am trying to do here is to set I.STS = 'D' for some 700 records pulled up using this query
select I.account_id, I.sts, I.name_id, CI.CRM_TYPE, I.comments
from PPInters I
inner join DW.CUST CT
on I.account_id = CT.account_id
where
I.sts is null
AND I.comments IS NOT NULL
AND CT.CUSTTYPe = 'INTNL'
I appreciate it
Assumming that account_id is a primary key kolumn in table PPInters,
that is it's value uniquely identifies records in this table:
UPDATE PPInters
SET STS = 'D'
WHERE account_id IN (
select I.account_id
/*, I.sts, I.name_id, CI.CRM_TYPE, I.comments */
from PPInters I
inner join DW.CUST CT
on I.account_id = CT.account_id
where
I.sts is null
AND I.comments IS NOT NULL
AND CT.CUSTTYPe = 'INTNL'
)
AND account_id IN (2000208927,380166014,190180447,166078041,105029075 )

Hierarchical query when used as sub query gives : ORA-00911: invalid character

I have been trying to execute the following query and its throwing me following error:
ORA-00911: invalid character
select
sum(coalesce(decode(FKTD_DR_CR_TYPE,'1',FKTD_NRS_AMOUNT,0),0)) as DEBIT_AMOUNT,
sum(coalesce(decode(FKTD_DR_CR_TYPE,'0',FKTD_NRS_AMOUNT,0),0)) as CREDIT_AMOUNT
from FMS_KS_TRANS_DTL
inner join FMS_KS_TRANS_MST
ON FMS_KS_TRANS_MST.FKTM_TRANS_MST_ID = FMS_KS_TRANS_DTL.FKTD_TRANS_MST_ID
inner join FMS_FC_VOUCHER_CONFIG
ON FMS_FC_VOUCHER_CONFIG.FFVC_VOUCHER_ID = FMS_KS_TRANS_MST.FKTM_VOUCHER_ID
where FFVC_ACCOUNT_TYPE = 5 and FKTM_FISCAL_YEAR='2066/67'
and FKTD_ACC_ID in
(
select FFAM_ACC_ID from FMS_FC_ACC_MST
where ffam_group_flag = 2 and FFAM_FISCAL_YEAR='2066/67'
start with ffam_acc_id in
(
select distinct FKP_CASH_ACC_ID from FMS_KS_PARAMETER
where FKP_FISCAL_YEAR='2066/67'
)
connect by ffam_group_flag = 2
and prior ffam_acc_id = ffam_upper_acc_id;
/*
This sub query executes fine if executed separately
and this whole query block executes fine if
this hierarchical query is not used.
*/
)
A semicolon before commented query?
A subquery should not end with a ;
select
sum(coalesce(decode(FKTD_DR_CR_TYPE,'1',FKTD_NRS_AMOUNT,0),0)) as DEBIT_AMOUNT,
sum(coalesce(decode(FKTD_DR_CR_TYPE,'0',FKTD_NRS_AMOUNT,0),0)) as CREDIT_AMOUNT
from FMS_KS_TRANS_DTL
inner join FMS_KS_TRANS_MST
ON FMS_KS_TRANS_MST.FKTM_TRANS_MST_ID = FMS_KS_TRANS_DTL.FKTD_TRANS_MST_ID
inner join FMS_FC_VOUCHER_CONFIG
ON FMS_FC_VOUCHER_CONFIG.FFVC_VOUCHER_ID = FMS_KS_TRANS_MST.FKTM_VOUCHER_ID
where FFVC_ACCOUNT_TYPE = 5 and FKTM_FISCAL_YEAR='2066/67'
and FKTD_ACC_ID in
(
select FFAM_ACC_ID from FMS_FC_ACC_MST
where ffam_group_flag = 2 and FFAM_FISCAL_YEAR='2066/67'
start with ffam_acc_id in
(
select distinct FKP_CASH_ACC_ID from FMS_KS_PARAMETER
where FKP_FISCAL_YEAR='2066/67'
)
connect by ffam_group_flag = 2
and prior ffam_acc_id = ffam_upper_acc_id
);
should work I guess (semicolon moved), unless there is something else which is wrong :-)

Resources