ROWID Error on oracle view with left join - oracle

So, i obtained this select from a view which has been causing errors when used, and i got this select.
This was the original query:
CREATE OR REPLACE VIEW RESUMEN_VENTA_PRODUCTOS
(
PRODUCTO,
M_PRODUCT_ID,
C_INVOICE_ID,
C_PROJECTOFB_ID,
C_DOCTYPE_ID,
TIPO_DOCUMENTO,
NETO,
C_CURRENCY_ID,
MONEDA,
FECHA_DOC,
NUMERO_DOC,
FECHA_CREACION_PRO,
RUT_NUMERO,
RUT_DV,
EJECUTIVO,
EVALUACION_PRE,
FALLA_TIPO,
FALLA_CAT,
CATEGORIA,
INGENIERO,
ORIGEN,
PRECIO_MINIMO
)
AS
select
p.name as producto, il.m_product_id, i.c_invoice_id, i.c_projectofb_id, i.c_doctype_id, dt.name as tipo_documento,
CASE WHEN charat (dt.docbasetype, 3) = 'C' AND dt.docbasetype <> 'CDC' then
il.linenetamt *-1
else
il.linenetamt
end as neto
, curr.c_currency_id,
curr.iso_code as moneda, i.dateinvoiced as fecha_doc, i.documentno as numero_doc,
pro.created as fecha_creacion_pro,
bp.value as rut_numero, bp.digito as rut_dv,
u.name as ejecutivo,
rltspe.name as evaluacion_pre,
rltftspr.name as falla_tipo,
rltfcspr.name as falla_cat,
pc.name as categoria,
ing.name as ingeniero
, rlt_o.name as ORIGEN
, pro.precio_minimo
from c_invoiceline il
left join c_invoice i
on (il.c_invoice_id = i.c_invoice_id)
left join m_product p
on (il.m_product_id = p.m_product_id)
left join m_product_category pc
on (p.m_product_category_id = pc.m_product_category_id)
left join c_projectofb pro
on (i.c_projectofb_id = pro.c_projectofb_id)
left join ad_user ing
on (pro.salesrep_id = ing.ad_user_id)
left join c_doctype dt
on (i.c_doctypetarget_id = dt.c_doctype_id)
left join c_currency curr
on (i.c_currency_id = curr.c_currency_id)
left join c_bpartner bp
on (i.c_bpartner_id = bp.c_bpartner_id)
left join ad_user u
on (i.salesrep_id = u.ad_user_id)
left join c_subprojectofb spe
on (pro.c_projectofb_id = spe.c_projectofb_id and spe.typeprojectkepler = '00')
left join c_subprojectofb spr
on (pro.c_projectofb_id = spr.c_projectofb_id and spr.typeprojectkepler = '01')
left join ad_ref_list rlspe
on (spe.evaluationpre_kp = rlspe.value and rlspe.ad_reference_id = 1000021)
left join AD_Ref_List_Trl rltspe
on (rlspe.ad_ref_list_id = rltspe.ad_ref_list_id)
left join ad_ref_list rlftspr
on (spr.falla_tipo = rlftspr.value and rlftspr.ad_reference_id = 1000053)
left join AD_Ref_List_Trl rltftspr
on (rlftspr.ad_ref_list_id = rltftspr.ad_ref_list_id)
left join ad_ref_list rlfcspr
on (spr.falla_cat = rlfcspr.value and rlfcspr.ad_reference_id = 1000052)
left join AD_Ref_List_Trl rltfcspr
on (rlfcspr.ad_ref_list_id = rltfcspr.ad_ref_list_id)
left join c_preproyecto pp
on (i.c_projectofb_id = pp.c_projectofb_id)
left join ad_ref_list rl_o
on (pp.origen = rl_o.value and rl_o.ad_reference_id = 1000059)
left join AD_Ref_List_Trl rlt_o
on (rl_o.ad_ref_list_id = rlt_o.ad_ref_list_id)
/*
left join ad_ref_list rl_est
on (pro.estado = rl_est.value and rl_est.ad_reference_id = 1000017)
left join AD_Ref_List_Trl rlt_est
on (rl_est.ad_ref_list_id = rlt_est.ad_ref_list_id)
*/
where i.docstatus = 'CO' and i.issotrx = 'Y'
and pro.typeprojectkepler = '01'
order by i.dateinvoiced desc;
GRANT SELECT ON RESUMEN_VENTA_PRODUCTOS TO KEPLER;
so i modified it to:
select p.name as producto,
il.m_product_id,
i.c_invoice_id,
i.c_projectofb_id,
i.c_doctype_id,
dt.name as tipo_documento,
CASE WHEN DT.C_DOCTYPE_ID IN (1000004,1000006,1000069,1000071,50013,1000120,1000121,1000135,1000144,1000086,1000099,1000105,1000107,1000113,1000021,1000048,1000049,141,124,118,1000037,1000039)
AND dt.docbasetype <> 'CDC'
then il.linenetamt *-1
else il.linenetamt
end as neto,
curr.c_currency_id,
curr.iso_code as moneda,
i.dateinvoiced as fecha_doc,
i.documentno as numero_doc,
pro.created as fecha_creacion_pro,
bp.value as rut_numero,
bp.digito as rut_dv,
u.name as ejecutivo,
rltspe.name as evaluacion_pre,
rltftspr.name as falla_tipo,
rltfcspr.name as falla_cat,
pc.name as categoria,
ing.name as ingeniero
,rlt_o.name as ORIGEN
,pro.precio_minimo
from c_invoiceline il
left join c_invoice i
on (il.c_invoice_id = i.c_invoice_id)
left join m_product p
on (il.m_product_id = p.m_product_id)
left join m_product_category pc
on (p.m_product_category_id = pc.m_product_category_id)
left join c_projectofb pro
on (i.c_projectofb_id = pro.c_projectofb_id)
left join ad_user ing
on (pro.salesrep_id = ing.ad_user_id)
left join c_doctype dt
on (i.c_doctypetarget_id = dt.c_doctype_id)
left join c_currency curr
on (i.c_currency_id = curr.c_currency_id)
left join c_bpartner bp
on (i.c_bpartner_id = bp.c_bpartner_id)
left join ad_user u
on (i.salesrep_id = u.ad_user_id)
left join c_subprojectofb spe
on (pro.c_projectofb_id = spe.c_projectofb_id and spe.typeprojectkepler = '00')
left join c_subprojectofb spr
on (pro.c_projectofb_id = spr.c_projectofb_id and spr.typeprojectkepler = '01')
left join ad_ref_list rlspe
on (spe.evaluationpre_kp = rlspe.value and rlspe.ad_reference_id = 1000021)
left join ad_ref_list rlftspr
on (spr.falla_tipo = rlftspr.value and rlftspr.ad_reference_id = 1000053)
full join AD_Ref_List_Trl rltspe
on (rlspe.ad_ref_list_id = rltspe.ad_ref_list_id)
left join AD_Ref_List_Trl rltftspr
on (rlftspr.ad_ref_list_id = rltftspr.ad_ref_list_id)
left join ad_ref_list rlfcspr
on (spr.falla_cat = rlfcspr.value and rlfcspr.ad_reference_id = 1000052)
left join AD_Ref_List_Trl rltfcspr
on (rlfcspr.ad_ref_list_id = rltfcspr.ad_ref_list_id)
left join c_preproyecto pp
on (i.c_projectofb_id = pp.c_projectofb_id)
left join ad_ref_list rl_o
on (pp.origen = rl_o.value and rl_o.ad_reference_id = 1000059)
left join AD_Ref_List_Trl rlt_o
on (rl_o.ad_ref_list_id = rlt_o.ad_ref_list_id)
where i.docstatus = 'CO' and i.issotrx = 'Y'
and pro.typeprojectkepler = '01';
order by i.dateinvoiced desc;
because the first CASE WHEN gave me an ORA-01455 error.
After modifying it, and when i ran the second select i obtained this message
An error occurred when executing the SQL command:
select p.name as producto,
il.m_product_id,
i.c_invoice_id,
i.c_projectofb_id,
i.c_doctype_id,
dt.name as tipo_documento,
CASE WHEN DT.C_DOCTYPE...
Error at line 54:
ORA-01445: cannot select ROWID from, or sample, a join view without a key-preserved table
full join AD_Ref_List_Trl rltspe
^
Execution time: 1.3s
Statement 1 of 2 finished
Script execution finished
Total script execution time: 1.3s
i have tried using full join, right join, inner join, outer join, and nothing seems to work, more than removin the said section of the query, i'm getting out of ideas here, so i decided to ask here for any help on the case.
Everything will be really appreciated

Related

How to combine 2 teradata queries into one

can someone please help me in combining these 2 Teradata queries into a single query? The tables - cdb.dim_party_doc_id, cdb.dim_doc_issuer, mdb.fp_account_entity_map do not have customer_account_number in them, so I not able to directly join all these tables in a single query directly.
Thanks a lot!!
SELECT
det.cust_id AS customer_account_number,
c.encrypt_val AS ssn_encrypted,
det.cust_first_name AS name_1,
bal.BALANCE_AMT AS principal
FROM
cdb.DIM_CUSTOMER det
INNER JOIN
cdb.fact_stored_val_acct_dly bal
ON det.cust_id = bal.customer_id AND bal.curr_cd='USD' and bal.acct_type_code='SBA'
INNER JOIN
cdb.dim_party_acct_map b
ON bal.customer_id = b.cust_id
INNER JOIN
cdb.dim_party_doc_id c
ON b.party_key = c.party_key
AND c.status = 'A'
INNER JOIN
cdb.dim_doc_issuer d
ON c.doc_issuer_id = d.doc_issuer_id
AND d.doc_type = 'TAX_ID'
AND d.doc_subtype = 'SSN'
and
SELECT
own.owner_id AS customer_account_number,
entity.entity_id AS dd_number
FROM
mdb.fp_account_owner_map own
LEFT JOIN
mdb.fp_account_entity_map entity
ON own.fp_account_id = entity.fp_account_id
WHERE
entity.entity_type in (12)
AND
own.product_id in (5501)
Below query solves my problem
SELECT
det.cust_id AS customer_account_number,
temp.direct_deposit_account_number AS account_number,
c.encrypt_val AS ssn_encrypted,
det.cust_first_name AS name_1,
bal.BALANCE_AMT AS principal
FROM
cdb.DIM_CUSTOMER det
LEFT JOIN
cdb.fact_stored_val_acct_dly bal
ON det.cust_id = bal.customer_id AND bal.curr_cd='USD' and bal.acct_type_code='SBA'
INNER JOIN
cdb.dim_party_acct_map b
ON bal.customer_id = b.cust_id
INNER JOIN
cdb.dim_party_doc_id c
ON b.party_key = c.party_key
AND c.status = 'A'
INNER JOIN
cdb.dim_doc_issuer d
ON c.doc_issuer_id = d.doc_issuer_id
AND d.doc_type = 'TAX_ID'
AND d.doc_subtype = 'SSN'
INNER JOIN
(SELECT
own.owner_id AS customer_id,
entity.entity_id AS direct_deposit_account_number
FROM mdb.fp_account_owner_map own
LEFT JOIN
mdb.fp_account_entity_map entity
ON own.fp_account_id = entity.fp_account_id
WHERE entity.entity_type in (12)
AND own.product_id in (5501)) AS temp
ON customer_account_number=temp.customer_id

Missing Operator in Query Expression

I have this code
SELECT Sales.InvoiceNo, Sales.SaleDate, Clients.Name, Clients.Address AS
ClientAdrs, Stock.Itemname, Stock.Tax, Stock.Price, Transactions.Qty,
Transactions.NetValue,Transactions.TaxAmount, Transactions.TotalAmount,
Sales.GrossNet, Sales.GrossTax, Sales.GrossTotal, Sales.PrintingCharge,
Sales.LabourCharge, Sales.AdjustableAmount,Sales.GrandTotal, Sales.InWords,
ShopeDetails.Address, ShopeDetails.Email, ShopeDetails.Mobile1,
ShopeDetails.Mobile2, ShopeDetails.TIN, ShopeDetails.AcN,ShopeDetails.IFC
FROM Sales
INNER JOIN Clients
ON Sales.Cid = Clients.Cid
INNER JOIN Transactions
ON Sales.InvoiceNo = Transactions.InvoiceNo
INNER JOIN Stock
ON Transactions.Sid = Stock.Sid
INNER JOIN ShopeDetails
ON Sales.Id = ShopeDetails.Id
and i get this error
Syntax error (missing operator) in query expression
Sales.Cid = Clients.Cid INNER JOIN Transactions ON Sales.InvoiceNo = Transactions.InvoiceNo INNER JOIN Stock ON Transactions.Sid = Stock.Sid INNER JOIN ShopeDetails ON Sales.Id = ShopeDetails.I
Please Help anyone !!
SELECT Sales.InvoiceNo, Sales.SaleDate, Clients.Name, Clients.Address AS
ClientAdrs,Stock.Itemname, Stock.Tax, Stock.Price, Transactions.Qty,
Transactions.NetValue,Transactions.TaxAmount, Transactions.TotalAmount,
Sales.GrossNet, Sales.GrossTax, Sales.GrossTotal, Sales.PrintingCharge,
Sales.LabourCharge, Sales.AdjustableAmount,Sales.GrandTotal, Sales.InWords,
ShopeDetails.Address, ShopeDetails.Email, ShopeDetails.Mobile1,
ShopeDetails.Mobile2, ShopeDetails.TIN, ShopeDetails.AcN,ShopeDetails.IFC
FROM (((Sales INNER JOIN Clients ON Sales.Cid = Clients.Cid)
INNER JOIN Transactions ON Sales.InvoiceNo = Transactions.InvoiceNo)
INNER JOIN Stock ON Transactions.Sid = Stock.Sid )
INNER JOIN ShopeDetails ON Sales.Id = ShopeDetails.Id
this solved the problem

Join the result with new inner query sql

select A.UNIT, A.LEASE_ID, A.MONTHS_GUARANTEED, A.MONTHLY_PAYMENT_AM,
B.DATE_PAID, C.CHARGE_JOB from leasei A
left outer join eq_capture B on A.LEASE_ID = B.LEASE_ID
left outer join eq_mast C on A.UNIT = C.UNIT
where A.DATE_LEASE_EXPIRE = 0
ORDER BY A.LEASE_ID;
I want to use the result of the above query that is use the value C.CHARGE_JOB and the check with another table (job_infojc D) and get the D.STATE value with a where condition C.CHARGE_JOB = D.JOB
Any help is highly appreciated.
Like this:
select D.STATE, X.*
from job_infojc D
join (select A.UNIT, A.LEASE_ID, A.MONTHS_GUARANTEED, A.MONTHLY_PAYMENT_AM,
B.DATE_PAID, C.CHARGE_JOB from leasei A
left outer join eq_capture B on A.LEASE_ID = B.LEASE_ID
left outer join eq_mast C on A.UNIT = C.UNIT
where A.DATE_LEASE_EXPIRE = 0
) X on X.CHARGE_JOB = D.JOB
ORDER BY X.LEASE_ID;

Linq expression multiple left outer join error

I am unable to execute the below linq.
var items( from p in Patients
join q in MURWorksheets on p.PatientId equals q.PatientId into step1
from s in step1.DefaultIfEmpty()
join t in MURWorksheetAnswers on s.MURWorksheetId equals t.MURWorksheetId into step2
from s2 in step2.DefaultIfEmpty()
select new {p.FirstName , Date = (s.MURDate == null ? DateTime.Now.Date : s.MURDate),
s2.MURQuestionnaireId,s2.MURExpctedAnswersId}).ToList();
Here is the sql for the same for your reference.
select a.FirstName,b.MURDate,c.MURQuestionnaireId,c.MURWorksheetAnswersID from Patients as a
left join MURWorksheet as b on a.PatientId = b.PatientId
left join MURWorksheetAnswers as c on b.MURWorksheetId = c.MURWorksheetId
You are missing an equals sign in your pasted code:
var items = (from p in Patients
join q in MURWorksheets on p.PatientId equals q.PatientId into step1
from s in step1.DefaultIfEmpty()
join t in MURWorksheetAnswers on s.MURWorksheetId equals t.MURWorksheetId into step2
from s2 in step2.DefaultIfEmpty()
select new {p.FirstName , Date = (s.MURDate == null ? DateTime.Now.Date : s.MURDate),
s2.MURQuestionnaireId,s2.MURExpctedAnswersId}).ToList();

Multiple Joins LINQ Query performance

I'm new to LINQ and have very little knowledge.
I have the following complex query. it runs 3 or 4 times slower than the stored procedure which i translated to LINQ.
any tips for me to make it run faster?
var result = from a in db.A
join al in db.AL.Where(q => q.CurrentLocation == 1) on a.AID equals al.AID into tmp_al
from al in tmp_al.DefaultIfEmpty()
join l in db.Lon al.LID equals l.LID into tmp_l
from l in tmp_l.DefaultIfEmpty()
join r in db.R on l.RID equals r.RID into tmp_r
from r in tmp_r.DefaultIfEmpty()
join b in db.B on r.BID equals b.BID into tmp_b
from b in tmp_b.DefaultIfEmpty()
join ap in db.AP.Where(q => q.CurrentProtocol == 1) on a.AID equals ap.AID into tmp_ap
from ap in tmp_ap.DefaultIfEmpty()
join p in db.P on ap.PID equals p.PID into tmp_p
from p in tmp_p.DefaultIfEmpty()
join s in db.S on a.SID equals s.SID into tmp_s
from s in tmp_s.DefaultIfEmpty()
join ans in db.AS on a.ASID equals ans.ASID into tmp_ans
from ans in tmp_ans.DefaultIfEmpty()
join pr in db.P on p.PI equals pr.PID into tmp_pr
from pr in tmp_pr.DefaultIfEmpty()
where a.Active == 1
group a by new { a.Active, pr.LN, pr.FN, b.BN, r.RID, r.R1, p.PN, s.S1, ans.AS1 }
into grp
orderby grp.Key.BN, grp.Key.R1, grp.Key.PN, grp.Key.S1, grp.Key.AS1
select new
{
PIName = grp.Key.LN + " " + grp.Key.FN,
BN = grp.Key.BN,
RID = grp.Key.RID,
R = grp.Key.R1,
PN = grp.Key.PN,
S = grp.Key.S1,
AS = grp.Key.AS1,
NumberOA = grp.Count()
};
Thanks for your answers. #Albin Sunnanbo: i dont know how to check the execution plans. my LINQ runs correctly and produces the required output. it is just slow. I would like to speeden it up. #usr: the original sql is as follows:
sorry about the silly table names. the original code is confidential. so i'm not posting the complete table names.
CREATE PROCEDURE [dbo].[report_CBRP] --
AS
SELECT LN + ' ' + FN As PIN, BN, R.RID, R, PN,
S, AS, COUNT(*) As NOA
FROM A
LEFT JOIN AL
ON A.AID = AL.AID
AND AL.CL = 1
LEFT JOIN L
ON AL.LID = L.LID
LEFT JOIN R
ON L.RID = R.RID
LEFT JOIN B
ON R.BID = B.BID
LEFT JOIN AP
ON A.AID = AP.AID
AND AP.CPl = 1
LEFT JOIN P
ON AP.PID = P.PID
LEFT JOIN S
ON A.SID = S.SID
LEFT JOIN AS
ON A.ASID = AS.ASID
LEFT JOIN P
ON P.PI = P.PID
GROUP BY A.A, LN , FN , B.BN, R.RID, R.R, P.PN,
S.S, AS.AS
HAVING A.A = 1
ORDER BY B.BN, R.R, P.PN, S, AS
GO
It seems you're doing SQL hard life here.
In general, try to avoid so many joins, but rather break them into few small queries.
More than that, you're performing a group by which in itself is an expensive operation, let alone with so many columns
I've noticed that you're joining all the columns in each table. Try to select only the relevant columns.
Also noticed that few of the tables aren't used in the group by like al, ap and l. Do you need them at all??
Use AsNoTracking() for readonly data from EF. In that way you speed up things.
Use SQL Views

Resources