Converting Oracle Query to Hive - oracle

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;

Related

Oracle Hierarchical queries: Translate START WITH ... CONNECT BY PRIOR into 'Recursive Subquery Factoring'

How would the following START WITH / CONNECT BY hierarchical query look like when translated into a RECURSIVE SUBQUERY FACTORING hierarchical query with WITH clause:
SELECT t1.id
FROM table1 t1, table2 t2
WHERE t1.version_id = t2.id
AND t1.baseline_date = TRIM (TO_DATE ('2015-05-26', 'yyyy-mm-dd'))
AND t2.entry_date = t1.baseline_date
START WITH t1.id IN (SELECT id
FROM table1
WHERE parent_id = 101015)
CONNECT BY PRIOR t1.id = t1.parent_id
ORDER SIBLINGS BY t1.child_index;
I think you want:
WITH rsqfc (id, child_index, baseline_date) AS (
SELECT t1.id,
t1.child_index,
t1.baseline_date
FROM table1 t1
INNER JOIN table2 t2
ON ( t1.version_id = t2.id
AND t2.entry_date = t1.baseline_date )
WHERE t1.parent_id = 101015
UNION ALL
SELECT t1.id,
t1.child_index,
t1.baseline_date
FROM rsqfc r
INNER JOIN table1 t1
ON (r.id = t1.parent_id)
INNER JOIN table2 t2
ON ( t1.version_id = t2.id
AND t2.entry_date = t1.baseline_date )
)
SEARCH DEPTH FIRST BY child_index SET order_id
SELECT id
FROM rsqfc
WHERE baseline_date = DATE '2015-05-26';
However, without sample data it is difficult to be sure.

Update with a minimum value from a union in Oracle

UPDATE table1 t SET t.columnA =
(SELECT MIN(columnB) FROM
(SELECT columnB FROM table2
WHERE table2.fk = t.pk
UNION ALL
SELECT columnB FROM table3
WHERE table3.fk = t.pk))
gives me ORA-00904: "T"."PK": invalid identifier . Any ideas on how to achieve this?
This is a problem of scoping. Oracle does not recognize the outer query alias more than one level of nesting deep.
If we assume that values are in both tables, then you can use LEAST() with subqueries:
UPDATE table1 t
SET t.columnA = LEAST( (SELECT MIN(columnB)
FROM table2
WHERE table2.fk = t.pk
),
(SELECT MIN(columnB)
FROM table3
WHERE table2.fk = t.pk
)
);
If not, you can modify your query by moving the correlation clause out one level:
UPDATE table1 t
SET t.columnA = (SELECT MIN(columnB)
FROM ((SELECT table2.fk, columnB FROM table2
) UNION ALL
(SELECT table3.fk, columnB FROM table3
)
) tt
WHERE tt.fk = t.pk
);

Error when joining CTEs

I have 2 CTE.When i try to join them i get an error message "ORA-01789: ".how can i merge the 2 CTE.Is there any other way to get the desired result?
WITH IMPORT_CTE
AS ((select A.*
FROM IMPORT_REGISTRY_ERROR_LOG_1 A
INNER JOIN (select distinct POD_ID,CONFLICTED_POD_ID,ERROR_CODE
FROM IMPORT_REGISTRY_ERROR_LOG_1
GROUP BY POD_ID,CONFLICTED_POD_ID,ERROR_CODE
HAVING COUNT(*) > 1) B
on A.POD_ID = B.POD_ID AND A.CONFLICTED_POD_ID = B.CONFLICTED_POD_ID AND A.ERROR_CODE = B.ERROR_CODE ) order by a.pod_id desc)
select t1.*
from IMPORT_CTE t1
where t1.insert_date =(select max(t2.insert_date)
from IMPORT_CTE t2
where t2.POD_ID =t1.POD_ID)
WITH IMPORT_CTE1
AS ((select A.*
FROM IMPORT_REGISTRY_ERROR_LOG_1 A
INNER JOIN (select distinct POD_ID,CONFLICTED_POD_ID,ERROR_CODE
FROM IMPORT_REGISTRY_ERROR_LOG_1
GROUP BY POD_ID,CONFLICTED_POD_ID,ERROR_CODE
HAVING COUNT(*) > 1) B
on A.POD_ID = B.POD_ID AND A.CONFLICTED_POD_ID = B.CONFLICTED_POD_ID AND A.ERROR_CODE = B.ERROR_CODE ) order by a.pod_id desc)
select t1.insert_date
from IMPORT_CTE1 t1
where t1.insert_date =(select min(t2.insert_date)
from IMPORT_CTE1 t2
where t2.POD_ID =t1.POD_ID)
You've got an extra set of parentheses in each of your queries. The first one should apparently be:
WITH IMPORT_CTE AS
(select A.*
FROM IMPORT_REGISTRY_ERROR_LOG_1 A
INNER JOIN (select distinct POD_ID,CONFLICTED_POD_ID,ERROR_CODE
FROM IMPORT_REGISTRY_ERROR_LOG_1
GROUP BY POD_ID,CONFLICTED_POD_ID,ERROR_CODE
HAVING COUNT(*) > 1) B
on A.POD_ID = B.POD_ID AND
A.CONFLICTED_POD_ID = B.CONFLICTED_POD_ID AND
A.ERROR_CODE = B.ERROR_CODE
order by a.pod_id desc)
select t1.*
from IMPORT_CTE t1
where t1.insert_date = (select max(t2.insert_date)
from IMPORT_CTE t2
where t2.POD_ID = t1.POD_ID)
The second one has a similar problem.
Best of luck.

Get unmatched records without using oracle minus except not in

Actually I have two table and each having column name, I just want the result which are not there in Table2
Table1
----
Name
---
|A|
|B|
|C|
|D|
Table2
------
|Name|
-----
|A|
|B|
Answer
|C|
|D|
I am able to do it by using minus
select name from table1
minus
select name from table2
select name from table1 where name
not in (
select name from table2)
But my Manager ask me to do it with other alternate solution without using minus,except,not in.
Is there a way to do that, It will be great if someone can help me on it.
I need to do it with oracle pl/sql
The one option left with you is using NOT EXISTS
SELECT t1.name
FROM table1 t1
WHERE NOT EXISTS (SELECT 'X'
FROM table2 t2
WHERE t2.name = t1.name);
Update: Using Join
with table_ as
(
select t1.name t1_name, t2.name t2_name
from table1 t1
left join table2 t2
on t1.name = t2.name)
select t1_name
from table_
where t2_name is null;
Or just
select t1.name
from table1 t1
left join table2 t2
on t1.name = t2.name
where t2.name is null;
Another alternative is to use an outer join and then filter rows that don't have a value in the 2nd table:
with t1 as (select 'A' name from dual union all
select 'B' name from dual union all
select 'C' name from dual union all
select 'D' name from dual),
t2 as (select 'A' name from dual union all
select 'B' name from dual)
select t1.name
from t1
left outer join t2 on (t1.name = t2.name)
where t2.name is null;
NAME
----
D
C

Multiple select for aleady joined table

Below is a part of my select query. In the same query I am selecting COLUMN_1 from a table TABLE2 with condition check. Also I am joining this table at end with one of the condition in the inner select as below. Can we have any other way to handle this situation with out using multiple `SELECT inside.
SELECT
T1.COLUMN_1
, (SELECT COLUMN_1 FROM TABLE2 WHERE COLUMN_22 ='A' AND COLUMN_11=T2.COLUMN_11)
, T1.COLUMN_2
, (SELECT COLUMN_1 FROM TABLE2 WHERE COLUMN_22 ='B' AND COLUMN_11=T2.COLUMN_11)
, T1.COLUMN_3
, (SELECT COLUMN_1 FROM TABLE2 WHERE COLUMN_22 ='C' AND COLUMN_11=T2.COLUMN_11)
, T1.COLUMN_4
, (SELECT COLUMN_1 FROM TABLE2 WHERE COLUMN_22 ='D' AND COLUMN_11=T2.COLUMN_11)
, T1.COLUMN_5
, (SELECT COLUMN_1 FROM TABLE2 WHERE COLUMN_22 ='E' AND COLUMN_11=T2.COLUMN_11)
, T1.COLUMN_6
, (SELECT COLUMN_1 FROM TABLE2 WHERE COLUMN_22 ='F' AND COLUMN_11=T2.COLUMN_11)
FROM TABLE1 T1, TABLE2 T2
-- plus two more tables
--plus some other conditions
WHERE T1.COLUMN_11=T2.COLUMN_11
Use CASE instead:
SELECT T1.COLUMN_1
,CASE
WHEN T2.COLUMN_22 = 'A'
THEN T2.COLUMN_1
END
,T1.COLUMN_2
,CASE
WHEN T2.COLUMN_22 = 'B'
THEN T2.COLUMN_1
END
,T1.COLUMN_3
,CASE
WHEN T2.COLUMN_22 = 'C'
THEN T2.COLUMN_1
END
,T1.COLUMN_4
,CASE
WHEN T2.COLUMN_22 = 'D'
THEN T2.COLUMN_1
END
,T1.COLUMN_5
,CASE
WHEN T2.COLUMN_22 = 'E'
THEN T2.COLUMN_1
END
,T1.COLUMN_6
,CASE
WHEN T2.COLUMN_22 = 'F'
THEN T2.COLUMN_1
END
FROM TABLE1 T1
INNER JOIN TABLE2 T2 ON T1.COLUMN_11 = T2.COLUMN_11;
EDIT
I changed the query to use the ansi join syntax. But that change is irrelevant to what you are asking. You can keep your join syntax if you want. The only relevant change is in the SELECT portion of the query.
You won't have very clean solutions I think. Another possibility is with an inner join by case:
SELECT
T1.COLUMN_1,
T2_1.COLUMN_1,
T1.COLUMN_2,
T2_2.COLUMN_1
T1.COLUMN_3,
T2_3.COLUMN_1
T1.COLUMN_4,
T2_4.COLUMN_1
T1.COLUMN_5,
T2_5.COLUMN_1
T1.COLUMN_6,
T2_6.COLUMN_1
FROM TABLE1 T1
INNER JOIN TABLE2 T2_1 ON T1.COLUMN_11=T2_1.COLUMN_11 AND T2_1.COLUMN_22 = 'A',
INNER JOIN TABLE2 T2_2 ON T1.COLUMN_11=T2_2.COLUMN_11 AND T2_2.COLUMN_22 = 'B',
INNER JOIN TABLE2 T2_3 ON T1.COLUMN_11=T2_3.COLUMN_11 AND T2_3.COLUMN_22 = 'C',
INNER JOIN TABLE2 T2_4 ON T1.COLUMN_11=T2_4.COLUMN_11 AND T2_4.COLUMN_22 = 'D',
INNER JOIN TABLE2 T2_5 ON T1.COLUMN_11=T2_5.COLUMN_11 AND T2_5.COLUMN_22 = 'E',
INNER JOIN TABLE2 T2_6 ON T1.COLUMN_11=T2_6.COLUMN_11 AND T2_6.COLUMN_22 = 'F',
WHERE
etc...

Resources