I need to cross join two tables but the joining conditions are:
Select FutureInventory.Item,
To_Number(Concat(Location, Channel_Id)) Location,
From V_CUST_FUTURE_INV_POSITION FutureInventory,
xx_Item_Loc_Info_V ItemLoc
Where FutureInventory.Item(+) = ItemLoc.Item
And To_Number(Concat(Location, Channel_Id))(+) = ItemLoc.Loc;
I want to outer join the column To_Number(Concat(Location, Channel_Id)) with ItemLoc.Loc
where to put the (+) sign
First, use explicit join syntax - the old (+) is hard to read and easy to get wrong. Second, use the || operator instead of the CONCAT function - again, easier to read. So we end up with
Select FutureInventory.Item,
To_Number(Location || Channel_Id) Location,
From xx_Item_Loc_Info_V ItemLoc
LEFT OUTER JOIN V_CUST_FUTURE_INV_POSITION FutureInventory,
ON FutureInventory.Item = ItemLoc.Item And
To_Number(Location || Channel_Id) = ItemLoc.Loc
To do an outer join in Oracle, you use the appropriate JOIN type -- just as in all other databases. I think the logic you want is:
select FutureInventory.Item,
To_Number(Concat(Location, Channel_Id)) as Location,
from xx_Item_Loc_Info_V ItemLoc LEFT JOIN
V_CUST_FUTURE_INV_POSITION FutureInventory,
on FutureInventory.Item = ItemLoc.Item AND
To_Number(Concat(Location, Channel_Id)) = ItemLoc.Loc;
Related
I would like to know how I can do operations between a column and a subquery, what I want to do is add to the field Subtotal what was obtained in the subquery Impuestos, the following is the query that I am using for this case.
Select
RC.PURCHID;
LRC.VALUEMST as 'Subtotal',
isnull((
select sum((CONVERT(float, TD1.taxvalue)/100)*LRC1.VALUEMST ) as a
FROM TAXONITEM TOI1
inner join TAXDATA TD1 ON (TD1.TAXCODE = TOI1.TAXCODE and RC.DATAAREAID = TD1.DATAAREAID)
inner join TRANS LRC1 on (LRC1.VEND = RC.RECID)
WHERE TOI1.TAXITEMGROUP = PL.TAXITEMGROUP and RC.DATAAREAID = TOI1.DATAAREAID
), 0) Impuestos
from VEND RC
inner join VENDTABLE VTB on VTB.ACCOUNTNUM = RC.INVOICEACCOUNT
inner join TRANS LRC on (LRC.VEND = RC.RECID)
inner join PURCHLINE PL on (PL.LINENUMBER =LRC.LINENUM and PL.PURCHID =RC.PURCHID)
where year (RC.DELIVERYDATE) =2021 and RC.PURCHASETYPE =3 order by RC.PURCHID;
Hope someone can give me some guidance when doing operations with subqueries.
A few disjointed facts that may help:
When a SELECT statement returns only one row with one column, you can enclose that statement in parenthesis and use it as a plain value. In your case, let's say that select sum(......= TOI1.DATAAREAID returns 500. Then, your outer select's second column is equivalent to isnull(500,0)
You mention in your question "subquery Impuestos". Keep in mind that, although you indeed used a subquery as we mentioned earlier, by the time it was enclosed in parentheses it is not treated as a subquery (more accurately: derived table), but as a value. Thus, the "Impuestos" is only a column alias at this point
I dislike and avoid subqueries before the from, makes things much harder to read. Here is a solution with apply which will keep your code mostly intact:
Select
RC.PURCHID,
LRC.VALUEMST as 'Subtotal',
isnull(subquery1.a, 0) as Impuestos
from VEND RC
inner join VENDTABLE VTB on VTB.ACCOUNTNUM = RC.INVOICEACCOUNT
inner join TRANS LRC on (LRC.VEND = RC.RECID)
inner join PURCHLINE PL on (PL.LINENUMBER =LRC.LINENUM and PL.PURCHID =RC.PURCHID)
outer apply
(
select sum((CONVERT(float, TD1.taxvalue)/100)*LRC1.VALUEMST ) as a
FROM TAXONITEM TOI1
inner join TAXDATA TD1 ON (TD1.TAXCODE = TOI1.TAXCODE and RC.DATAAREAID = TD1.DATAAREAID)
inner join TRANS LRC1 on (LRC1.VEND = RC.RECID)
WHERE TOI1.TAXITEMGROUP = PL.TAXITEMGROUP and RC.DATAAREAID = TOI1.DATAAREAID
) as subquery1
where year (RC.DELIVERYDATE) =2021 and RC.PURCHASETYPE =3 order by RC.PURCHID;
Can't figure out why I can't perform this join, I've done many like it in the past. Am I missing something?
SELECT *
FROM THB_View.PCM_PCM_BASE base
LEFT OUTER JOIN THB_View.PCM_PCM_BASE_REOCCUR rec
LEFT OUTER JOIN THB_View.ATV atv ON base.PCM_TAG = atv.ATV_OCCURRING_PCM_TAG
ON base.RECORD_KEY = rec.RECORD_KEY
WHERE
base.PCM_STATUS = 'ENROUTE'
AND rec.PCM_HRC_TAG IS NULL OR rec.PCM_HRC_TAG = ''
AND rec.PCM_EQP_TAG IS NULL OR rec.PCM_EQP_TAG = ''
The error occurs on base.PCM_TAG the 5th line in the SQL statement
Your from clause is not the typical way to write the joins. It is allowed, but this affects the scoping of the identifiers. Essentially, this is interpreted as:
from THB_View.PCM_PCM_BASE base left join
(THB_View.PCM_PCM_BASE_REOCCUR rec left join
THB_View.ATV atv
on base.PCM_TAG = atv.ATV_OCCURRING_PCM_TAG
)
on base.RECORD_KEY = rec.RECORD_KEY
Which explains why base is not recognized.
Just write the joins the standard way with the on condition right after the tables being joined:
from THB_View.PCM_PCM_BASE base left join
THB_View.PCM_PCM_BASE_REOCCUR rec
on base.RECORD_KEY = rec.RECORD_KEY left join
THB_View.ATV atv
on base.PCM_TAG = atv.ATV_OCCURRING_PCM_TAG
im trying to rewrite the following code:
W_WHERE := ' PD.NIF(+) = p.NIF and pd.num_colegiado(+) = p.num_colegiado AND PD.FECHA_INICIO(+) <= SYSDATE
AND NVL(PD.FECHA_FIN(+), SYSDATE) >= SYSDATE AND D.ID_DIRECCION(+) = PD.ID_DIRECCION AND p.num_colegiado = coleg.num_colegiado';
into normal JOIN notation, could anybody help me ?
PS. PD is for PERSONA_DIRECCION table and P is for PERSONA table
Explicit joins get their name from specifying explicitly what kind of join you use on the table (CROSS JOIN, INNER JOIN, LEFT OUTER JOIN etc.)
So you will have to re-write the query such that you replace the comma-separated tables in your FROM clause with explicit joins (INNER JOIN and LEFT JOIN here). Then move your join criteria to the ON clause in question:
select ...
from colleg
inner join p on p.num_colegiado = coleg.num_colegiado
left join pd on pd.nif = p.nif and
pd.num_colegiado = p.num_colegiado and
pd.fecha_inicio <= sysdate and
nvl(pd.fecha_fin, sysdate) >= sysdate
left join d on d.id_direccion = pd.id_direccion;
There is nothing implicit here. In Oracle, "(+) =" is a "normal JOIN notation" (as you said) for outer join. If you don't want outer join, just remove the (+).
See this SO answer for the explanation.
I want to add the below join to the existing query. I am not aware whether the logic is correct.
Any help would be appreciated.
LEFT OUTER JOIN
dbo.note not
CASE
WHEN not.main_ref_type='M'
THEN pem.membership_id=not.main_ref_id
WHEN not.main_ref_type=P'
THEN per.person_id=not.main_ref_id
END
You need an on clause. It should look more like this:
LEFT OUTER JOIN
dbo.note not
on (not.main_ref_type='M' and not.main_ref_id = pem.membership_id) or
(not.main_ref_type='P' and per.person_id=not.main_ref_id)
You should know that joins with or conditions often perform badly. In many cases, it is better to do two separate joins (to the note table) and then use logic in the select (typically coalesce()) to get the right values.
Try this
LEFT OUTER JOIN dbo.note nt ON (nt.main_ref_type='M'
AND pem.membership_id=nt.main_ref_id)
OR
(nt.main_ref_type='P'
AND per.person_id=nt.main_ref_id)
END
CROSS APPLY (
VALUES
('M' , pem.membership_id),
('P' , per.person_id )
) map(ref_type, ref_id )
LEFT OUTER JOIN dbo.note not ON (
not.main_ref_type = map.ref_type AND
not.main_ref_id = map.ref_id
)
I can't understand the usage of the using word. Can you explain me?
SELECT 1
FROM CONF_RAGGR_OPZTAR ropt
JOIN TAR_OPZIONI_TARIFFARIE OPT using (OPT_OPZIONE_TARIFFARIA_ID)
JOIN CONF_RAGGRUPPAMENTI_FORN rgf using (RGF_RAGGRUPPAMENTO_FORN_ID)
JOIN CONF_FORNITURE_REL_RAGG forg using (RGF_RAGGRUPPAMENTO_FORN_ID)
JOIN CONF_FORNITURE forn using (FORN_FORNITURA_ID)
LEFT JOIN (
select *
from CONF_ELEMENTI_FATTURABILI
where ELF_FLAG_ANN = 'N'
AND ELF_DATA_VER_FIN = TO_DATE('31/12/9999','DD/MM/YYYY')
) elf **using** (ROPT_RAGGR_OPZTAR_ID,COID_CONTRATTUARIO_ID,ROPT_DATA_INI,EDW_PARTITION)
-- LEFT OUTER JOIN TAR_VOCI_FATTURABILI vof
-- ON (elf.VOF_VOCE_FATTURABILE_ID = vof.VOF_VOCE_FATTURABILE_ID)
-- LEFT OUTER JOIN BASE_FASCE_ORARIE fas
-- ON (fas.FAS_FASCIA_ORARIA_ID = elf.FAS_FASCIA_ORARIA_ID)
WHERE FORN_FORNITURA_ID = 'QJlXmOFZPF3eAlAG'
ORDER BY elf.ELF_VERSIONE DESC;
The using keyword indicates that this is a natural join. This means that the column names on both side of the join are identical.
In your case this means that you will join both sides on ROPT_RAGGR_OPZTAR_ID, COID_CONTRATTUARIO_ID, ROPT_DATA_INI and EDW_PARTITION.