Group by query Rising ORA-00934 exception - oracle

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

Related

How to convert CLOB to VARCHAR2 in Oracle

I am trying to transform a data that has been saved in a table as CLOB to varchar2. When the query is executed the value that is returned is still a CLOB. What can I be doing wrong when it comes to building the query?
I tried
select l.user_id, l.login, l.is_active, l.is_locked,
(SELECT rtrim(xmlagg(xmlelement(e,lav.ATTR_VALUE,',').extract('//text()') order by TO_CHAR(lav.ATTR_VALUE)).getClobVal,',')
FROM LOGIN l1,LOGIN_ATTRIBUTES la,LOGIN_ATTRIBUTE_VALUES lav
WHERE l1.LOGIN_ID = la.LOGIN_ID AND la.ID = lav.LOGIN_ATTRIBUTE_ID AND la.NAME = 'email') as EMAIL
from IAMUSER.LOGIN l
left join IAMUSER.MANAGED_SYS ms on ms.managed_sys_id = l.managed_sys_id
where l.managed_sys_id = 'MOODLE' and login = 'ex392310'
Results:
USER_ID |LOGIN |IS_ACTIVE|IS_LOCKED|EMAIL |
--------------------------------+--------+---------+---------+------+
8a8c9321799d403d01799e401527011c|ex392310|Y |N |[CLOB]|
Change getClobVal() to getStringVal():
select l.user_id,
l.login,
l.is_active,
l.is_locked,
( SELECT rtrim(
xmlagg(
xmlelement(e,lav.ATTR_VALUE,',').extract('//text()')
order by TO_CHAR(lav.ATTR_VALUE)
).getStringVal(),
','
)
FROM LOGIN l1
INNER JOIN LOGIN_ATTRIBUTES la
ON (l1.LOGIN_ID = la.LOGIN_ID)
INNER JOIN LOGIN_ATTRIBUTE_VALUES lav
ON (la.ID = lav.LOGIN_ATTRIBUTE_ID)
WHERE la.NAME = 'email'
) as EMAIL
from IAMUSER.LOGIN l
left join IAMUSER.MANAGED_SYS ms
on ms.managed_sys_id = l.managed_sys_id
where l.managed_sys_id = 'MOODLE'
and login = 'ex392310'
Or, use LISTAGG:
select l.user_id,
l.login,
l.is_active,
l.is_locked,
( SELECT LISTAGG(lav.ATTR_VALUE,',')
WITHIN GROUP (ORDER BY TO_CHAR(lav.ATTR_VALUE))
FROM LOGIN l1
INNER JOIN LOGIN_ATTRIBUTES la
ON (l1.LOGIN_ID = la.LOGIN_ID)
INNER JOIN LOGIN_ATTRIBUTE_VALUES lav
ON (la.ID = lav.LOGIN_ATTRIBUTE_ID)
WHERE la.NAME = 'email'
) as EMAIL
from IAMUSER.LOGIN l
left join IAMUSER.MANAGED_SYS ms
on ms.managed_sys_id = l.managed_sys_id
where l.managed_sys_id = 'MOODLE'
and login = 'ex392310'

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

Column ambiguously defined Oracle SQL

I'm facing this ambiguously defined error. I understand this is due to alias not defined properly but I am not able to understand in the below query why this error is coming. I'm using Oracle.
SELECT
papf.person_number as personnumber,
paam.position_id as position,
fabu.bu_name
FROM
(SELECT
papf.person_number,
paam.person_id,
fabu.bu_name,
fabu.bu_id,
paam.assignment_id,
paam.assignment_number,
paam.action_code,
paam.effective_start_date,
wft.outcome,
htd.state,
htd.status,
Count(paam.assignment_id)
over (
PARTITION BY paam.assignment_id) rowcnt
FROM
per_all_assignments_m paam,
per_all_people_f papf,
hrc_txn_header hth,
hrc_arm_process_vl hapv,
hrc_txn_data htd,
fa_fusion_soainfra.wftask wft,
fun_all_business_units_v fabu
WHERE
paam.primary_assignment_flag = 'Y'
AND paam.effective_latest_change = 'Y'
AND paam.assignment_status_type = 'ACTIVE'
AND paam.action_code IN ( 'GLB_TRANSFER', 'TRANSFER' )
AND hth.subject = 'PER_ALL_PEOPLE_F'
AND hth.subject_id = paam.person_id
AND hth.object IN ( 'PER_ALL_ASSIGNMENTS_M', 'PER_ALL_PEOPLE_F' )
AND hth.object_id = Decode(hth.object, 'PER_ALL_ASSIGNMENTS_M', paam.assignment_id,
'PER_ALL_PEOPLE_F', paam.person_id)
AND hapv.process_id = hth.process_id
AND hapv.txn_module_identifier IN ( 'Transfers', 'ManageEmployment', 'AddWorkRelationship' )
AND htd.transaction_id = hth.transaction_id
AND wft.identificationkey = To_char(hth.transaction_id)
AND SYSDATE BETWEEN papf.effective_start_date AND papf.effective_end_date
AND paam.person_id = papf.person_id
and sysdate between fabu.date_from and fabu.date_to
and fabu.status='A'
and paam.business_unit_id=fabu.bu_id
GROUP BY papf.person_number,
paam.person_id,
paam.assignment_id,
fabu.bu_name,
fabu.bu_id,
paam.assignment_number,
paam.action_code,
paam.effective_start_date,
wft.outcome,
htd.state,
htd.status) t1,
per_all_people_f papf,
per_all_assignments_m paam,
fun_all_business_units_v fabu
WHERE rowcnt = 1
AND outcome = 'APPROVE'
AND state = 'COMPLETE'
AND status = 'APPROVED'
AND paam.person_id = t1.person_id
AND paam.action_code = t1.action_code
AND paam.assignment_status_type = 'INACTIVE'
AND paam.assignment_sequence = 1
AND paam.effective_start_date = t1.effective_start_date
AND paam.assignment_type = 'E'
and papf.person_id = t1.person_id
AND papf.effective_start_date = t1.effective_start_date
AND fabu.bu_id = t1.bu_id
I'm defining the paam table and papf table the same way and not facing issue in them.
Why I am facing this error when I try to add fabu table in end ?
You forgot to add t1. when refer to some columns that can be found in other tables:
WHERE t1.rowcnt = 1
AND t1.outcome = 'APPROVE'
AND t1.state = 'COMPLETE'
AND t1.status = 'APPROVED'

SQL ERROR When running query

I've been trying to execute below code, but it's not running. SQLFiddle gives
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.'EO' AS 'EO','EF'.'AC' AS 'AC','EO'' at line 2
I don't know what I'm doing wrong.
SELECT DISTINCT
'EO'.'EO' AS 'EO',
'EF'.'AC' AS 'AC',
'EO'.'WO' AS 'WO',
'EO'.'WOF' AS 'WOF',
'EF'.'PN_SN' AS 'PN_SN',
'EC'.'CD' AS 'CD',
'EF'.'PN' AS 'PN',
'EO'.'EOC' AS 'EOC',
'EO'.'STAT' AS 'STAT',
'EF'.'OVRD' AS 'OVRD',
'EF'.'SEL' AS 'SEL',
'EF'.'ED' AS 'ED',
'EF'.'SCHED' AS 'SCHED',
'EF'.'SAD' AS 'SAD',
'EF'.'SA' AS 'SA',
'EO'.'EOD' AS 'EOD'
FROM
EO,
EC,
EF
WHERE
(
'EO'.'EO' = 'EC'.'EO'
)
AND
(
'EF'.'PSN' = 'EC'.'PSN'
)
AND
(
'EO'.'WB' = 'Y'
)
AND
(
'EC'.'CONT' = 'W & B'
)
;
SELECT
'EC'.'RSTD' AS 'RSTD'
FROM EC RIGHT JOIN EF
ON ( 'EF'.'EO' = 'EC'.'EO' )
ON ( 'EF'.'AC' = 'EC'.'AC' )
ON ( 'EF'.'PN' = 'EC'.'PN' )
ON ( 'EF'.'PSN' = 'EC'.'PSN' )
Single quotes - ' - are for literals. Use double-quotes - " - for identifiers ...
SELECT
"EC"."RSTD"
FROM EC RIGHT JOIN EF
ON ( "EF"."EO" = "EC"."EO" )
ON ( "EF"."AC" = "EC"."AC" )
ON ( "EF"."PN" = "EC"."PN" )
ON ( "EF"."PSN' = "EC"."PSN" )
... or, even better, omit them altogether:
SELECT
EC.RSTD
FROM EC RIGHT JOIN EF
ON ( EF.EO = EC.EO )
ON ( EF.AC = EC.AC )
ON ( EF.PN = EC.PN )
ON ( EF.PSN = EC.PSN )
Obviously keep the single quotes when appropriate e.g. comparing a column to a literal:
where EO.WB = 'Y'
Also note that it is unnecessary to use a column alias when you're not changing the column name: select ec.rstd as rstd is redundant, and indeed annoying. Reserve their use for renaming columns e.g. select ec.rstd as my_new_col_name

ORA-01427: Subquery returns more than one row

When I execute the query below, I get the message like this: "ORA-01427: Sub-query returns more than one row"
Define REN_RunDate = '20160219'
Define MOP_ADJ_RunDate = '20160219'
Define RID_RunDate = '20160219'
Define Mbr_Err_RunDate = '20160219'
Define Clm_Err_RunDate = '20160219'
Define EECD_RunDate = '20160219'
select t6.Member_ID, (Select 'Y' from MBR_ERR t7 where t7.Member_ID = t6.Member_ID and t7.Rundate = &Mbr_Err_RunDate ) Mbr_Err,
NVL(Claim_Sent_Amt,0) Sent_Claims, Rejected_Claims,Orphan_Claim_Amt,Claims_Accepted, MOP_Adj_Sent Sent_MOP_Adj,Net_Sent,
(Case
When Net_Sent < 45000 then 0
When Net_Sent > 25000 then 20500
Else
Net_Sent - 45000
End
)Net_Sent_RI,
' ' Spacer,
Total_Paid_Claims CMS_Paid_Claims, MOP_Adjustment CM_MOP_Adj, MOP_Adjusted_Paid_claims CM_Net_Claims, Estimated_RI_Payment CM_RI_Payment
from
(
select NVL(t3.Member_ID,t5.Member_ID)Member_ID, t3.Claim_Sent_Amt, NVL(t4.Reject_Claims_Amt,0) Rejected_Claims, NVL( t8.Orphan_Amt,0) Orphan_Claim_Amt,
(t3.Claim_Sent_Amt - NVL(t4.Reject_Claims_Amt,0) - NVL(t8.Orphan_Amt,0)) Claims_Accepted,
NVL(t2.MOP_Adj_Amt,0) MOP_Adj_Sent ,
( (t3.Claim_Sent_Amt - NVL(t4.Reject_Claims_Amt,0)) - NVL(t2.MOP_Adj_Amt,0) - NVL(t8.Orphan_Amt,0) ) Net_Sent,
t5.Member_ID CMS_Mbr_ID,t5.Total_Paid_Claims,t5.MOP_Adjustment, t5.MOP_Adjusted_Paid_Claims, t5.Estimated_RI_Payment
From
(
Select t1.Member_ID, Sum( t1.Paid_Amount) Claim_Sent_Amt
From RENS t1
where t1.rundate = &REN_RunDate
group by t1.Member_ID
) t3
Left Join MOP_ADJ t2
on (t3.Member_ID = t2.Member_ID and t2.rundate = &MOP_ADJ_RunDate)
Left Join
(select Member_ID, sum(Claim_Total_Paid_Amount) Reject_Claims_Amt from CLAIM_ERR
where Rundate = &Claim_Err_RunDate
and Claim_Total_Paid_Amount != 0
Group by member_ID
)t4
on (t4.Member_ID = t3.Member_ID )
Full Outer Join
(
select distinct Member_ID,Total_Paid_Claims,MOP_Adjustment,MOP_Adjusted_Paid_Claims, Estimated_RI_Payment
from RID
where Rundate = &RID_RunDate
and Estimated_RI_Payment != 0
)t5
On(t5.Member_ID = t3.Member_ID)
Left Outer Join
(
select Member_ID, Sum(Claim_Paid_Amount) Orphan_Amt
From EECD
where RunDate = &EECD_RunDate
group by Member_ID
)t8
On(t8.Member_ID = t3.Member_ID)
)t6
order by Member_ID
You have this expression among the select columns (at the top of your code):
(Select 'Y' from MBR_ERR t7 where t7.Member_ID = t6.Member_ID
and t7.Rundate = &Mbr_Err_RunDate ) Mbr_Err
If you want to select the literal 'Y', then just select 'Y' as Mbr_Err. If you want to select either 'Y' or null, depending on whether the the subquery returns exactly one row or zero rows, then write it that way.
I suspect this subquery (or perhaps another one in your code, used in a similar way) returns more than one row - in which case you will get exactly the error you got.

Resources