Error when joining CTEs - oracle

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.

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.

OR in Select Oracle

select * from table1 t1 where t1.column1 = 'someValue' and ((t1.column2 =1) OR (sysdate < select t1.DateColumn2 + t2.DateColumn2/1440
from
table2 t2 where t1.column3 = t2.column3));
if t1.column2 =1 evaluates to false, I want to check another condition if time t1.DateColumn2 + t2.DateColumn2 is < sysdate . Oracle is throwing syntax error near or condition. Cant sysdate be used directly like that? Not sure where I am going wrong. Thanks
If I am guessing your intention correctly, you want an exists clause
select *
from table1 t1
where t1.column1 = 'someValue'
and ( (t1.column2 =1)
OR exists( select 1
from table2 t2
where t2.column3 = t1.column3
and sysdate < t1.DateColumn2 + t2.DateColumn2/1440 ));
Or just join the two tables in the outer query assuming there is at most 1 row in t2 per t1 row (if there is exactly 1 row you should do an inner join rather than a left outer join)
select t1.*
from table1 t1
left outer join table2 t2
on( t1.column3 = t2.column3 )
where t1.column2 = 1
or sysdate < t1.DateColumn2 + t2.DateColumn2/1440;

ORA-00907 error at order by in Oracle 11g

select Customer_Name from Customer where Customer_Id IN
(select distinct Customer.Customer_Id from Customer join Toy_Rental on Toy_Rental.Customer_ID=Customer.Customer_Id
order by Toy_Rental.Total_Amount desc
fetch next 2 rows only);
There's no FETCH clause in 11g; it is available in 12c.
See if this helps:
select a.Customer_Name
from Customer a
where a.Customer_Id IN (select x.customer_id
from (select c.Customer_Id
from Customer c join Toy_Rental t on t.customer_id = c.customer_id
order by t.total_amount desc
) x
where rownum <= 2
)
Alternatively:
with cust as
(select c.customer_id
row_number() over (order by t.total_amount desc) rn
from customer c join toy_rental t on t.customer_id = c.customer_id
)
select a.customer_name
from customer a join cust x on x.customer_id = a.customer_id
where x.rn <= 2

I am trying to combine 3 tables for to get a distinct combination as below

SELECT TYPE_DETAILS(a.column1,c.column2,c.column3) BULK COLLECT INTO OUT_DETAILS
FROM TABLE1 a
INNER JOIN TABLE2 b ON a.column2 = b.column2
INNER JOIN TABLE3 c ON a.column3 = c.column3;
I only want combinations for distinct values of a.column1 . If I apply distinct as below i am getting error
SELECT TYPE_DETAILS(DISTINCT a.column1,c.column2,c.column3) BULK COLLECT INTO OUT_DETAILS
FROM TABLE1 a
INNER JOIN TABLE2 b ON a.column2 = b.column2
INNER JOIN TABLE3 c ON a.column3 = c.column3;
Why don't you use sub-query:
SELECT TYPE_DETAILS(column1,column2,column3)
BULK COLLECT INTO OUT_DETAILS FROM
(SELECT DISTINCT a.column1,c.column2,c.column3
FROM TABLE1 a
INNER JOIN TABLE2 b ON a.column2 = b.column2
INNER JOIN TABLE3 c ON a.column3 = c.column3);

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;

Resources