Left join problems in oracle - oracle

I hope anyone can help me :) My problem is when i run my code i am seeing this error " SQL command not properly ended" and "example" is red.
Thank you for your interested
(select mat.oid matoid
from lsn.material mat,
lsn.plan_def def,
lsn.unit meu
where math.plan_def_oid = def.oid
and math.unit_oid = meu.oid
)example left join lsn.plan_det det on det.def_oid = example .matoid;

I think you can just join all four tables together in one go:
select math.oid matoid
from lsn.material mat
inner join lsn.plan_def def
on math.plan_def_oid = def.oid
inner join lsn.unit meu
on math.unit_oid = meu.oid
left join lsn.plan_det det
on plan_def_oid = math.oid
Changes I made here include converting your implicit joins to explicit inner joins. As a general rule, you should avoid putting commas into the from clause. Also I moved your original incomplete subquery out to the main query.

In addition, it looks like there is a reference to a RESULTS table that is missing

Related

ORACLE - DECODE view predicate and apply on base table

I have a view defined as below
CREATE VIEW DQ_DB.DQM_RESULT_VIEW
AS SELECT
res.ACTIVE_FL AS ACTIVE_FL,
res.VERSION as VERSION,
res.rule_constituents_tx,
nvl(ruletable.rule_desc,'N/A') AS rule_ds,
nvl(res.effective_dt, TO_DATE('31-dec-9999','dd-mon-yyyy')) AS effective_dt,
nvl(res.rule_id,'N/A') AS rule_id,
res.audit_update_ts AS rule_processed_at,
res.load_dt,
res.vendor_group_key,
nvl(res.vendor_entity_key,'N/A') AS vendor_entity_key,
res.vendor_entity_producer_nm,
(SELECT category_value_tx FROM dq_db.category_lookup_view WHERE category_nm = 'RESULT_STATUS_NB' AND category_value_cd = res.result_status_nb ) AS result,
--catlkp.category_value_tx as result,
res.entity_type,
nvl(rgrp.grp_nm,'N/A') AS rule_category,
nvl(ruletable.rule_nm,'N/A') AS rule_nm,
feedsumm.feed_run_nm AS file_nm,
res.application_id AS application,
res.data_source_id AS datasource,
res.entity_nm,
res.rule_entity_effective_dt,
res.result_id,
dim.dimension_nm,
dim.sub_dimension_nm,
ruletable.execution_env AS execution_env,
ruletable.ops_action AS ops_action,
rulefunctiontable.func_nm AS rule_func_nm,
-- nvl2(res.primary_dco_sid,dq_db.get_dco_name(res.primary_dco_sid),null) AS dco_primary,
-- nvl2(res.delegate_dco_sid,dq_db.get_dco_name(res.delegate_dco_sid),null) AS dco_delegate,
res.primary_dco_sid AS dco_primary,
res.delegate_dco_sid AS dco_delegate,
ruletable.data_concept_id AS data_concept_id,
res.latest_result_fl as latest_result_fl,
res.batch_execution_ts as batch_execution_ts
FROM
dq_db.dqm_result res
--LEFT OUTER JOIN dq_db.category_lookup_view catlkp on (catlkp.category_nm = 'RESULT_STATUS_NB' AND catlkp.category_value_cd = res.result_status_nb)
LEFT OUTER JOIN dq_db.feed_run_summary feedsumm ON res.vendor_group_key = feedsumm.batch_id
LEFT OUTER JOIN dq_db.dqm_rule ruletable ON res.rule_id = ruletable.rule_id
LEFT OUTER JOIN dq_db.dqm_rule_grp rgrp ON ruletable.rule_grp_id = rgrp.rule_grp_id
LEFT OUTER JOIN dq_db.dqm_rule_function rulefunctiontable ON ruletable.func_id = rulefunctiontable.func_id
LEFT OUTER JOIN dq_db.dq_dimension_view dim ON dim.dimension_id = ruletable.dimension_id
result column of view is a lookup value generated from subquery condition which translates code as
below.
0|PASS 1|ALERT 2|ERROR
My web application adds few predicates to this view which are pushed to base tables. But one particular predicate on view shown below is not pushed due to inline nature of the predicate.
select * from dqm_result_view where result IN ('ALERT','ERROR')
Right now this query applies filter after view JOINs are executed as there is no way to push predicate to DQM_RESULT table.
What is need is.. if we get that result predicate then apply code 0,1,2 instead of applying result predicate at end so that data is filtered ahead of time for JOINs from DQM_RESULT base table and thus improve performance. Any idea on how to achieve this?

Bug in Toad when converting join from Oracle to ANSI notation?

When using Toad to convert a query featuring outer joins written in the old Oracle notation into ANSI notation, I've run into a problem:
If I write it like this:
select AGENTS.AG_AGCLE, AGENTS.AG_NOM, S01_SERVICES.SE_CODE,
S01_DEPART.DP_CODE, S01_DEPART.DP_NOM, CONGES.CG_CPANT,
S01_PERSO.PE_TEMPSW, MEDWORK.MK_VILLE, MEDWORK.MK_DOCTORNAME
from S01_PERSO,S01_SERVICES,S01_DEPART,AGENTS,CONGES,MEDWORK
where SE_DPCLE=DP_DPCLE
and SE_SECLE=PE_SECLE
and PE_AGCLE=AG_AGCLE
and AG_AGCLE=CG_AGCLE(+)
and AG_MKCLE=MK_MKCLE(+)
and PE_PECLE=25;
Toad converts it to this, which is correct:
SELECT AG_AGCLE, AG_NOM, SE_CODE, DP_CODE, DP_NOM, CG_CPANT, PE_TEMPSW,
MK_VILLE, MK_DOCTORNAME
FROM S01_PERSO
INNER JOIN AGENTS ON (PE_AGCLE = AG_AGCLE)
INNER JOIN S01_SERVICES ON (SE_SECLE = PE_SECLE)
INNER JOIN S01_DEPART ON (SE_DPCLE = DP_DPCLE)
LEFT OUTER JOIN CONGES ON (AG_AGCLE = CG_AGCLE)
LEFT OUTER JOIN MEDWORK ON (AG_MKCLE = MK_MKCLE)
WHERE PE_PECLE = 25;
Whereas if I write it like this, which is the same query (I just wrote one of the join conditions the other way round):
select AGENTS.AG_AGCLE, AGENTS.AG_NOM, S01_SERVICES.SE_CODE,
S01_DEPART.DP_CODE, S01_DEPART.DP_NOM, CONGES.CG_CPANT,
S01_PERSO.PE_TEMPSW, MEDWORK.MK_VILLE, MEDWORK.MK_DOCTORNAME
from S01_PERSO,S01_SERVICES,S01_DEPART,AGENTS,CONGES,MEDWORK
where SE_DPCLE=DP_DPCLE
and SE_SECLE=PE_SECLE
and PE_AGCLE=AG_AGCLE
and CG_AGCLE(+)=AG_AGCLE
and AG_MKCLE=MK_MKCLE(+)
and PE_PECLE=25;
Toad converts it to this:
SELECT AG_AGCLE, AG_NOM, SE_CODE, DP_CODE, DP_NOM, CG_CPANT, PE_TEMPSW,
MK_VILLE, MK_DOCTORNAME
FROM S01_PERSO
INNER JOIN AGENTS ON (PE_AGCLE = AG_AGCLE)
INNER JOIN S01_SERVICES ON (SE_SECLE = PE_SECLE)
INNER JOIN S01_DEPART ON (SE_DPCLE = DP_DPCLE)
LEFT OUTER JOIN MEDWORK ON (AG_MKCLE = MK_MKCLE)
RIGHT OUTER JOIN CONGES ON (CG_AGCLE = AG_AGCLE)
WHERE PE_PECLE = 25;
I have searched online for this problem and found nothing, Has anybody else come across this? Is this a bug, or am I missing something?
I'm using Toad for Oracle Freeware 12.10.0.30, 64 bit on Windows 7 64 bit.

Raw SQL query or Doctrine_Query? What is the best in performance?

I made this query in raw SQL and need some help moving to Doctrine:
SELECT
re . *, t.tipo, m.patente, o.nombre, de.revisado
FROM
sdriving_registros_emisores re
LEFT JOIN
sdriving_turno t ON re.idturno = t.idturno
LEFT JOIN
sdriving_maquina_emisor me ON me.idmaquinaemisor = re.maquinaemisorid
LEFT JOIN
sdriving_maquina m ON me.idmaquina = m.idmaquina
LEFT JOIN
sdriving_operador o ON re.idoperador = o.idoperador
LEFT JOIN
sdriving_detalle_emisores de ON de.idregistros = re.idregistros
WHERE
o.idempresa = 1
Also I wish to know what's the best method in perfomance: executing raw query or using Doctrine Query or something else

converting sql to linq woes

At my job our main application was written long ago before n-tier was really a thing, ergo - it has tons and tons of business logic begin handled in stored procs and such.
So we have finally decided to bite the bullet and make it not suck so bad. I have been tasked with converting a 900+ line sql script to a .NET exe, which I am doing in C#/Linq. Problem is...for the last 5-6 years at another job, I had been doing Linq exclusively, so my SQL has gotten somewhat rusty, and some of thing I am converting I have never tried to do before in Linq, so I'm hitting some roadblocks.
Anyway, enough whining.
I'm having trouble with the following sql statement, I think due to the fact that he is joining on a temp table and a derived table. Here's the SQL:
insert into #processedBatchesPurgeList
select d.pricebatchdetailid
from pricebatchheader h (nolock)
join pricebatchstatus pbs (nolock) on h.pricebatchstatusid = pbs.pricebatchstatusid
join pricebatchdetail d (nolock) on h.pricebatchheaderid = d.pricebatchheaderid
join
( -- Grab most recent REG.
select
item_key
,store_no
,pricebatchdetailid = max(pricebatchdetailid)
from pricebatchdetail _pbd (nolock)
join pricechgtype pct (nolock) on _pbd.pricechgtypeid = pct.pricechgtypeid
where
lower(rtrim(ltrim(pct.pricechgtypedesc))) = 'reg'
and expired = 0
group by item_key, store_no
) dreg
on d.item_key = dreg.item_key
and d.store_no = dreg.store_no
where
d.pricebatchdetailid < dreg.pricebatchdetailid -- Make sure PBD is not most recent REG.
and h.processeddate < #processedBatchesPurgeDateLimit
and lower(rtrim(ltrim(pbs.pricebatchstatusdesc))) = 'processed' -- Pushed/processed batches only.
So that's raising an overall question first: how to handle temp tables in Linq? This script uses about 10 of them. I currently have them as List. The problem is, if I try to .Join() on one in a query, I get the "Local sequence cannot be used in LINQ to SQL implementations of query operators except the Contains operator." error.
I was able to get the join to the derived table to work using 2 queries, just so a single one wouldn't get nightmarishly long:
var dreg = (from _pbd in db.PriceBatchDetails.Where(pbd => pbd.Expired == false && pbd.PriceChgType.PriceChgTypeDesc.ToLower().Trim() == "reg")
group _pbd by new { _pbd.Item_Key, _pbd.Store_No } into _pbds
select new
{
Item_Key = _pbds.Key.Item_Key,
Store_No = _pbds.Key.Store_No,
PriceBatchDetailID = _pbds.Max(pbdet => pbdet.PriceBatchDetailID)
});
var query = (from h in db.PriceBatchHeaders.Where(pbh => pbh.ProcessedDate < processedBatchesPurgeDateLimit)
join pbs in db.PriceBatchStatus on h.PriceBatchStatusID equals pbs.PriceBatchStatusID
join d in db.PriceBatchDetails on h.PriceBatchHeaderID equals d.PriceBatchHeaderID
join dr in dreg on new { d.Item_Key, d.Store_No } equals new { dr.Item_Key, dr.Store_No }
where d.PriceBatchDetailID < dr.PriceBatchDetailID
&& pbs.PriceBatchStatusDesc.ToLower().Trim() == "processed"
select d.PriceBatchDetailID);
So that query gives the expected results, which I am holding in a List, but then I need to join the results of that query to another one selected from the database, which is leading me back to the aforementioned "Local sequence cannot be used..." error.
That query is this:
insert into #pbhArchiveFullListSaved
select h.pricebatchheaderid
from pricebatchheader h (nolock)
join pricebatchdetail d (nolock)
on h.pricebatchheaderid = d.pricebatchheaderid
join #processedBatchesPurgeList dlist
on d.pricebatchdetailid = dlist.pricebatchdetailid -- PBH list is restricted to PBD purge list rows that have PBH references.
group by h.pricebatchheaderid
The join there on #processedBatchesPurgeList is the problem I am running into.
So uh...help? I have never written SQL like this, and certainly never tried to convert it to Linq.
As pointed out by the comments above, this is no longer being rewritten as Linq.
Was hoping to get a performance improvement along with achieving better SOX compliance, which was the whole reason for the rewrite in the first place.
I'm happy with just satisfying the SOX compliance issues.
Thanks, everyone.

How to implement a left outer join in the Entity Framework

I have the following SQL query:-
select distinct * from dbo.Profiles profiles
left join ProfileSettings pSet on pSet.ProfileKey = profiles.ProfileKey
left join PlatformIdentities pId on pId.ProfileKey = profiles.Profilekey
I need to convert it to a LinqToEntities expression. I have tried the following:-
from profiles in _dbContext.ProfileSet
let leftOuter = (from pSet in _dbContext.ProfileSettingSet
select new
{
pSet.isInternal
}).FirstOrDefault()
select new
{
profiles.ProfileKey,
Internal = leftOuter.isInternal,
profiles.FirstName,
profiles.LastName,
profiles.EmailAddress,
profiles.DateCreated,
profiles.LastLoggedIn,
};
The above query works fine because I haven't considered the third table "PlatformIdentities". Single left outer join works with what I have done above. How do I include PlatformIdentities (the 3rd table) ? I basically want to translate the SQL query I specified at the beginning of this post (which gives me exactly what I need) in to LinqToEntities.
Thanks
Let me know if you want to select something different, but a true join is below
from p in _dbContext.ProfileSet
join ps in _dbContext.ProfileSettings on p.ProfileKey = ps.ProfileKey into a
join pi in _dbContext.PlatformIdentities on p.ProfileKey = pi.ProfileKey into b
select new
{
profiles.ProfileKey,
profiles.FirstName,
profiles.LastName,
profiles.EmailAddress,
profiles.DateCreated,
profiles.LastLoggedIn,
PlatformSettings = a.Select(x=>x),
PlatformIdentities = b.Select(y=>y)
}

Resources