How to convert CLOB to VARCHAR2 in Oracle - 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'

Related

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

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.

left join with ActiveRecord (yii2)

I tried to send SQL request with LEFT JOIN but it doesn't display data from table2 table.
public static function top($limit)
{
return self::findBySql("
SELECT * FROM table 1 g1
LEFT JOIN table2 s1
ON (g1.id = s1.g_id AND s1.id = (
SELECT MAX(id)
FROM table2 s2 WHERE s2.g_id = g1.id
))
LIMIT :limit",
[':limit' => $limit]
)->all();
}
It seems you are adding this function to the model and self represents the model itself.
Yii will not return results from another table and will be limited to the model only if you are calling the find on a model, instead you need to use a db query as below:
$query = new \yii\db\Query;
$query->select('*')
->from('table 1 g1')
->leftJoin('table2 s1', 's1.g_id AND s1.id = (SELECT MAX(id) FROM table2 s2 WHERE s2.g_id = g1.id')
->limit($Limit);
$command = $query->createCommand();
$resp = $command->queryAll();
The correct SQL query is
SELECT * FROM table 1 g1
LEFT JOIN table2 s1
ON g1.some_field = s1.some_field
where g1.some_field = s1.some_field are the fields that define the join.
I have working code something like...:)
with user and user_friend_list
$query = new Query;
$query ->select(['user.id AS user_id', 'user_friend_list.id'])
->from('user')
->innerJoin('user_friend_list', 'user.email = user_friend_list.email');
$command = $query->createCommand();
$data = $command->queryAll();
foreach($data as $datakey)
{
//echo $datakey['id'];
$friendfind = UserFriendList::findOne($datakey['id']);
$friendfind->is_app_using_user_id=$datakey['user_id'];
$friendfind->save();
}

Sql Query in Linq

i need to write sql query in string and pass it to linq like executenonquery() in ado.net
but i need to return data from 4 tables
Example Query :
SELECT *
FROM dbo.GeneralAd INNER JOIN
dbo.Category ON dbo.GeneralAd.FkCategoryID = dbo.Category.CategoryID INNER JOIN
dbo.District ON dbo.GeneralAd.FkDistrictID = dbo.District.DistrictID LEFT OUTER JOIN
dbo.Users ON dbo.GeneralAd.FkApprovedByID = dbo.Users.UserID AND dbo.GeneralAd.FKAddedByID = dbo.Users.UserID where .........
how to return data from 4 tables with executenonquery method in linq ?
I'm not sure why you why you want to return all columns from all tables. That seems unnecessary. You can get to the District and Category fields from the GeneralAd object.
What I would probably do is define a property on the General Ad object that executes a select to get the associated User.
If you did want to do it in Linq, you could use something like this:
var results = (from ga in DataContext.GeneralAds
select new
{
Col1 = ga.Col1,
Col2 = ga.Category.Col2,
Col3 = ga.District.Col3,
Col4 = (from u in DataContext.Users
where u.UserID = ga.FkApprovedByID AND u.UserID = ga.FKAddedByID
select u.Col4)
});
You might have to add an Any() method on Col4:
var results = (from ga in DataContext.GeneralAds
select new
{
Col1 = ga.Col1,
Col2 = ga.Category.Col2,
Col3 = ga.District.Col3,
Col4 = ((from u in DataContext.Users
where u.UserID = ga.FkApprovedByID AND u.UserID = ga.FKAddedByID
select u.Col4).Any() ?
(from u in DataContext.Users
where u.UserID = ga.FkApprovedByID AND u.UserID = ga.FKAddedByID
select u.Col4).First() : -1)
});

Resources