ansi join on oracle 8i - oracle

I've got this query in Oracle 8i:
select
decode(seqnum,
1 , '1',
cnt, '0'
) as value1,
decode(seqnum,
1 , t.BEGIN_DT,
cnt, t.END_DT
) as TIME1,
t4.UNIT1 || '.SUBBATCH_TRIGGER' TAG
from (select t.*,
row_number() over(partition by t.BATCH_ID, t.plant_unit, t3.ID2 order by t.BEGIN_DT) as seqnum,
count(*) over(partition by t.BATCH_ID, t.plant_unit, t3.ID2) as cnt
from SCH2.tb_pg_unit_stap t
join (select ID1,batch_id from SCH2.VW_BATCH) t2 on t.BATCH_ID = t2.BATCH_ID
join (select ID2,ID1 from SCH1.STEP) t3 on t3.ID1 = t2.ID1) t
join SCH2.TB_W_MACHINE t4 on t4.plant_unit = t.plant_unit
where (seqnum = 1
or seqnum = cnt) AND (t.BEGIN_DT > '01-jan-2013' AND t.BEGIN_DT < '01-feb-2013');
I've recently asked the Stackoverflow community and they told me Oracle 8i doesn't support ansi joins.
how can I rewrite this query for Oracle 8i?
Thanks in advance!

select decode(seqnum, 1 , '1',
cnt, '0') as value1,
decode(seqnum, 1, t.BEGIN_DT,
cnt, t.END_DT) as TIME1,
t4.UNIT1 || '.SUBBATCH_TRIGGER' TAG
from (select t.*,
row_number() over(partition by t.BATCH_ID, t.plant_unit, t3.ID2 order by t.BEGIN_DT) as seqnum,
count(*) over(partition by t.BATCH_ID, t.plant_unit, t3.ID2) as cnt
from SCH2.tb_pg_unit_stap t,
(select ID1,batch_id from SCH2.VW_BATCH) t2,
(select ID2,ID1 from SCH1.STEP) t3
where t.BATCH_ID = t2.BATCH_ID
and t3.ID1 = t2.ID1) t,
SCH2.TB_W_MACHINE t4
where t4.plant_unit = t.plant_unit
and (seqnum = 1 or seqnum = cnt)
AND (t.BEGIN_DT > '01-jan-2013' AND t.BEGIN_DT < '01-feb-2013');

Related

Converting Oracle Query to Hive

How can I convert the below query in Oracle to Hive?
SELECT A.EMP_NO, A.LOGIN_TIMESTAMP FROM TABLE1 A, TABLE2 B
WHERE A.EMP_NO = 1234 AND B.EMP_CURR =
(SELECT MIN(EMP_CURR) FROM TABLE2 WHERE EMP_NO = A.EMP_NO AND
LOGIN_TIMESTAMP = A.LOGIN_TIMESTAMP AND EMP_STATUS_CODE <> 'P')
Use dense_rank() to get rows with minimum EMP_CURR:
SELECT A.EMP_NO, A.LOGIN_TIMESTAMP
FROM TABLE1 A
INNER JOIN (select B.*,
dense_rank() over(partition by B.EMP_NO, B.LOGIN_TIMESTAMP order by B.EMP_CURR) rn
from TABLE2 B where EMP_STATUS_CODE <> 'P'
) B
on B.EMP_NO = A.EMP_NO and B.LOGIN_TIMESTAMP = A.LOGIN_TIMESTAMP and B.rn=1
where B.rn=1 and A.EMP_NO = 1234;

Oracle how to do outer join in line select

I have three selects
SELECT
mam.fecha fecha
, u.COD_USUARIO usuario
, u.NOMBREAPELLIDOS nombre
, ROW_NUMBER() OVER (PARTITION BY u.cod_usuario ORDER BY mam.fecha ASC) AS rn_asc
, ROW_NUMBER() OVER (PARTITION BY u.cod_usuario ORDER BY mam.fecha DESC) AS rn_desc
,mam.accion accion
,u.ID_CENTRO_GESTION centrogestion
FROM r_mod_asignar_material mam
INNER JOIN r_usuarios u ON mam.cod_usuario = u.cod_usuario
where mam.ACCION = 'A'
and trunc(mam.fecha) = to_date('13/09/2018','dd/mm/yyyy')
and mam.cod_usuario = '9717703'
and u.id_centro_gestion = '3'
order by u.cod_usuario
Second one, it is the same changing the where
SELECT
mam.fecha fecha
, u.COD_USUARIO usuario
, u.NOMBREAPELLIDOS nombre
, ROW_NUMBER() OVER (PARTITION BY u.cod_usuario ORDER BY mam.fecha ASC) AS rn_asc
, ROW_NUMBER() OVER (PARTITION BY u.cod_usuario ORDER BY mam.fecha DESC) AS rn_desc
,mam.accion accion
,u.ID_CENTRO_GESTION centrogestion
FROM r_mod_asignar_material mam
INNER JOIN r_usuarios u ON mam.cod_usuario = u.cod_usuario
where mam.ACCION = 'D'
and trunc(mam.fecha) = to_date('13/09/2018','dd/mm/yyyy')
and mam.cod_usuario = '9717703'
and u.id_centro_gestion = '3'
order by u.cod_usuario
Thrid one
SELECT COUNT (*) numeroincidencias, ri.cod_usuario usuario
FROM r_incidencias ri
WHERE TRUNC (ri.fecha_inc) = TO_DATE ('13/09/2018', 'dd/mm/yyyy')
and ri.cod_usuario = '9717703'
GROUP BY ri.cod_usuario
I want to link these select by cod_usuario. It couldn't be rows in the first, second or third select.
How can I do an outer join of these selects?
Make each of your selects a common table expression, then full outer join them together:
WITH cte1 AS (SELECT mam.fecha AS fecha,
u.COD_USUARIO AS usuario,
u.NOMBREAPELLIDOS AS nombre,
ROW_NUMBER() OVER (PARTITION BY u.cod_usuario
ORDER BY mam.fecha ASC) AS rn_asc,
ROW_NUMBER() OVER (PARTITION BY u.cod_usuario
ORDER BY mam.fecha DESC) AS rn_desc,
mam.accion AS accion,
u.ID_CENTRO_GESTION AS centrogestion
FROM r_mod_asignar_material mam
INNER JOIN r_usuarios u
ON mam.cod_usuario = u.cod_usuario
where mam.ACCION = 'A' and
trunc(mam.fecha) = to_date('13/09/2018','dd/mm/yyyy') AND
mam.cod_usuario = '9717703' AND
u.id_centro_gestion = '3'
order by u.cod_usuario),
cte2 AS (SELECT mam.fecha AS fecha,
u.COD_USUARIO AS usuario,
u.NOMBREAPELLIDOS AS nombre,
ROW_NUMBER() OVER (PARTITION BY u.cod_usuario
ORDER BY mam.fecha ASC) AS rn_asc,
ROW_NUMBER() OVER (PARTITION BY u.cod_usuario
ORDER BY mam.fecha DESC) AS rn_desc,
mam.accion AS accion
u.ID_CENTRO_GESTION AS centrogestion
FROM r_mod_asignar_material mam
INNER JOIN r_usuarios u
ON mam.cod_usuario = u.cod_usuario
where mam.ACCION = 'D' and
trunc(mam.fecha) = to_date('13/09/2018','dd/mm/yyyy') AND
mam.cod_usuario = '9717703' AND
u.id_centro_gestion = '3'
order by u.cod_usuario),
cte3 AS (SELECT COUNT (*) numeroincidencias,
ri.cod_usuario usuario
FROM r_incidencias ri
WHERE TRUNC (ri.fecha_inc) = TO_DATE ('13/09/2018', 'dd/mm/yyyy') AND
ri.cod_usuario = '9717703'
GROUP BY ri.cod_usuario)
SELECT *
FROM cte1
FULL OUTER JOIN cte2
ON cte2.cod_usuario = cte1.cod_usuario
FULL OUTER JOIN cte3
ON cte3.cod_usuario = COALESCE(cte1.cod_usuario, cte2.cod_usuario)

Is this query OK? [Oracle 8i]

I've got this query in Oracle 11g [working fine]:
select (case
when seqnum = 1 then
t.DPR_N
when seqnum = cnt then
'0'
end) as VALUE1,
(case
when seqnum = 1 then
t.BEGIN_DT
when seqnum = cnt then
t.END_DT
end) as timestamp,
t2.APP_NAAM || '.SUBBATCH_TRIGGER' TAGNAME1
from (select t.*,t6.*,
row_number() over(partition by t.BATCH_ID, t.PLANT_UNIT,t6.DPR_ID order by t.BEGIN_DT) as seqnum,
count(*) over(partition by t.BATCH_ID, t.PLANT_UNIT,t6.DPR_ID) as cnt
FROM tb_unit_step t
INNER JOIN tb_equipment t2
ON t2.PLANT_UNIT = t.PLANT_UNIT
INNER JOIN tb_rs3 t3
ON t.BATCH_ID = t3.BATCH_ID
INNER JOIN tb_cpm t9
ON t9.BACPM_ID = t3.BACPM_ID
INNER JOIN tb_step t4
ON (t9.BV_ID = t4.BV_ID
AND t.STAP_NR1 = t4.STAP_NR1
AND t.STAP_NR2 = t4.STAP_NR2)
INNER JOIN tb_bv t5
ON t5.BV_ID = t9.BV_ID
INNER JOIN tb_bv_process t6
ON t9.BV_ID = t6.BV_ID
AND t6.DPR_ID = t4.DPR_ID
INNER JOIN tb_ins t7
ON (t7.INS_ID = t4.INS_ID)
INNER JOIN tb_cpm t8
ON t8.BV_ID = t9.BV_ID
WHERE (t.BEGIN_DT > ? AND t.END_DT < ?)
) t
join tb_equipment t2 on t2.plant_unit = t.plant_unit
where (seqnum = 1
or seqnum = cnt);
I've got to make it work on Oracle 8i [I know it's a REALLY old version, but I have no choice since it's not my DB]. I've built this query in order to get the data from Oracle 8i: [I've changed CASE WHEN for DECODE and removed all the ANSI JOINs]
SELECT DECODE(SEQNUM, 1, T.DPR_N,CNT,'0') VALUE1,
DECODE(SEQNUM, 1, T.BEGIN_DT,CNT,T.END_DT) TIMESTAMP,
'090.' || T2.APP_NAAM
|| '.SUBBATCH_TRIGGER' TAGNAME1
FROM
(SELECT T.*,
T6.*,
ROW_NUMBER() OVER(PARTITION BY T.BATCH_ID, T.PLANT_UNIT,T6.DPR_ID ORDER BY T.BEGIN_DT) SEQNUM,
COUNT(*) OVER(PARTITION BY T.BATCH_ID, T.PLANT_UNIT,T6.DPR_ID) CNT
FROM tb_unit_step T ,
tb_equipment T2 ,
tb_rs3 T3 ,
tb_cpm T9 ,
tb_step T4 ,
tb_bv T5 ,
tb_bv_process T6 ,
tb_ins T7 ,
tb_cpm T8
WHERE T2.PLANT_UNIT = T.PLANT_UNIT
AND T.BATCH_ID = T3.BATCH_ID
AND (T9.BV_ID = T4.BV_ID
AND T.STAP_NR1 = T4.STAP_NR1
AND T.STAP_NR2 = T4.STAP_NR2)
AND T5.BV_ID = T9.BV_ID
AND (T9.BV_ID = T6.BV_ID
AND T6.DPR_ID = T4.DPR_ID)
AND T7.INS_ID = T4.INS_ID
AND T8.BV_ID = T9.BV_ID
AND (T.BEGIN_DT > '15-jul-2013'
AND T.END_DT < '01-aug-2014')
) T
,tb_equipment T2
WHERE T2.PLANT_UNIT = T.PLANT_UNIT
AND (T.SEQNUM = 1
OR SEQNUM = T.CNT)
;
The new query is definately not OK because it's taking forever to run. So what would be the correct form of the first query in order to get data from Oracle 8i?
UPDATE:
Result of the query:
ORA-01652: unable to extend temp segment by 128 in tablespace TEMP
01652. 00000 - "unable to extend temp segment by %s in tablespace %s"
*Cause: Failed to allocate an extent of the required number of blocks for
a temporary segment in the tablespace indicated.
*Action: Use ALTER TABLESPACE ADD DATAFILE statement to add one or more
files to the tablespace indicated.
Thanks in advance!
I don't see this condition in your Oracle 8 version:
t9.BACPM_ID = t3.BACPM_ID
That could explain the performance problem.

Alternatives to CASE in Oracle 8i [duplicate]

This question already has answers here:
Query works on Oracle 11g but fails on Oracle 8i
(2 answers)
Closed 9 years ago.
Thanks to the stackoverflow community I found out that this query is not supported in Oracle 8i because of the CASE WHEN.
select (case
when seqnum = 1 then
'1'
when seqnum = cnt then
'0'
end) as value1,
(case
when seqnum = 1 then
t.BEGIN_DT
when seqnum = cnt then
t.END_DT
end) as TIME1,
t4.UNIT1 || '.SUBBATCH_TRIGGER' TAG
from (select t.*,
row_number() over(partition by t.BATCH_ID, t.plant_unit, t3.ID2 order by t.BEGIN_DT) as seqnum,
count(*) over(partition by t.BATCH_ID, t.plant_unit, t3.ID2) as cnt
from SCH2.tb_pg_unit_stap t
join (select ID1,batch_id from SCH2.VW_BATCH) t2 on t.BATCH_ID = t2.BATCH_ID
join (select ID2,ID1 from SCH1.STEP) t3 on t3.ID1 = t2.ID1) t
join SCH2.TB_W_MACHINE t4 on t4.plant_unit = t.plant_unit
where (seqnum = 1
or seqnum = cnt) AND (t.BEGIN_DT > '01-jan-2013' AND t.BEGIN_DT < '01-feb-2013');
What are the alternatives for this query to run on Oracle 8i?
Thanks in advance!
You should try with decode(..., ..., ...)
select
decode(seqnum,
1 , '1',
cnt, '0'
) as value1,
decode(seqnum,
1 , t.BEGIN_DT,
cnt, t.END_DT
) as TIME1
...
here's the link to the documentation of decode.
However, as has been pointed out in a comment, the join construct (ansi joins) won't work in 8i either.
instead of (case when seqnum = 1 then '1' when seqnum = cnt then '0' end)
you could use decode(seqnum, 1, '1', decode(seqnum, cnt, '0'))
I hope you get the idea

Query works on Oracle 11g but fails on Oracle 8i

I am running this query in Oracle 11g with no problem:
select (case
when seqnum = 1 then
'1'
when seqnum = cnt then
'0'
end) as value1,
(case
when seqnum = 1 then
t.BEGIN_DT
when seqnum = cnt then
t.END_DT
end) as TIME1,
t4.UNIT1 || '.SUBBATCH_TRIGGER' TAG
from (select t.*,
row_number() over(partition by t.BATCH_ID, t.plant_unit, t3.ID2 order by t.BEGIN_DT) as seqnum,
count(*) over(partition by t.BATCH_ID, t.plant_unit, t3.ID2) as cnt
from SCH2.tb_pg_unit_stap t
join (select ID1,batch_id from SCH2.VW_BATCH) t2 on t.BATCH_ID = t2.BATCH_ID
join (select ID2,ID1 from SCH1.STEP) t3 on t3.ID1 = t2.ID1) t
join SCH2.TB_W_MACHINE t4 on t4.plant_unit = t.plant_unit
where (seqnum = 1
or seqnum = cnt) AND (t.BEGIN_DT > '01-jan-2013' AND t.BEGIN_DT < '01-feb-2013');
But when I run it in Oracle 8i (8.7.1) it gives ORA-00933: SQL command not properly ended.
Is there anys special consideration I must know for running sql commands in Oracle 8i?
Thanks in advance
AFAK case command was introduced since 9i.
OK - trip--down-memory-lane time:
select decode(seqnum,
1, '1',
cnt, '0') as value1,
decode(seqnum,
1, t.BEGIN_DT,
cnt, t.END_DT) as TIME1,
t4.UNIT1 || '.SUBBATCH_TRIGGER' TAG
from (select t.*,
row_number() over (partition by t.BATCH_ID, t.plant_unit, t3.ID2
order by t.BEGIN_DT) as seqnum,
count(*) over (partition by t.BATCH_ID, t.plant_unit, t3.ID2) as cnt
from SCH2.tb_pg_unit_stap t
INNER join (select ID1, batch_id
from SCH2.VW_BATCH) t2
on t.BATCH_ID = t2.BATCH_ID
INNER join (select ID2, ID1
from SCH1.STEP) t3
on t3.ID1 = t2.ID1) t
INNER join SCH2.TB_W_MACHINE t4
on t4.plant_unit = t.plant_unit
where (seqnum = 1 or seqnum = cnt) AND
(t.BEGIN_DT > '01-jan-2013' AND t.BEGIN_DT < '01-feb-2013');

Resources