Unpivot Multiple columns in Oracle is not working - oracle

I have a table like below
Process_1
Tab1_Count
Tab1_Process_Count
Tab1_Status
Process_2
Tab2_Count
Tab2_Process_Count
Tab2_Status
Tab1_Process
123
123
Equal
Tab2_Process
123
126
Not Equal
I am trying to Unpivot the above like the below
TABLEPROCESS
Tab_Count
Process_Count
Status
Tab1_Process
123
123
Equal
Tab2_Process
123
123
Not Equal
With the below query
SELECT *
FROM
(
SELECT 'Tab1' AS tab1_Process,
count(tb1.ID) AS tab1_count,
count(tbp1.ID) AS tab1_Process_count,
CASE WHEN count(tb1.ID)=count(tbp1.ID) THEN 'Equal' ELSE 'Not Equal' END AS Tab1_Status,
'Tab2' AS tab2_Process,
count(tb2.ID) AS tab2_count,
count(tbp2.ID) AS tab2_Process_count,
CASE WHEN count(tb2.ID)=count(tbp2.ID) THEN 'Equal' ELSE 'Not Equal' END AS Tab2_Status
FROM table1 tb1
LEFT JOIN table1_process tbp1 ON tbp1.ID = tb1.ID
LEFT JOIN table2 tb2 ON tb2.ID = tb1.ID
LEFT JOIN table1_process tbp2 ON tbp2.ID = tb2.ID
)
UNPIVOT
(
(TABLEPROCESS,STATUS)
FOR COUNT IN
(
(Tab1_Count,Tab2_Count) AS 'Tab_Count',
(Tab1_Process_Count,Tab2_Process_Count) AS 'Process_Count',
(Tab1_Status,Tab2_Status) AS 'STATUS'
)
)
but it is giving me error -- ORA-00907: missing right parenthesis
As per comment by Alex Poole tried CROSS APPLY method
SELECT *
FROM
(
SELECT 'Tab1' AS tab1_Process,
count(tb1.ID) AS tab1_count,
count(tbp1.ID) AS tab1_Process_count,
CASE WHEN count(tb1.ID)=count(tbp1.ID) THEN 'Equal' ELSE 'Not Equal' END AS Tab1_Status,
'Tab2' AS tab2_Process,
count(tb2.ID) AS tab2_count,
count(tbp2.ID) AS tab2_Process_count,
CASE WHEN count(tb2.ID)=count(tbp2.ID) THEN 'Equal' ELSE 'Not Equal' END AS Tab2_Status
FROM table1 tb1
LEFT JOIN table1_process tbp1 ON tbp1.ID = tb1.ID
LEFT JOIN table2 tb2 ON tb2.ID = tb1.ID
LEFT JOIN table1_process tbp2 ON tbp2.ID = tb2.ID
)
CROSS APPLY
VALUES (
(tab1_Process,Tab1_Count,Tab1_Process_Count,Tab1_Status),
(tab2_Process,Tab2_Count,Tab2_Process_Count,Tab2_Status),
) (Process,Tab_Count,Process_Count,Status);
It is working in SQL Server but it is giving ORA-00903: invalid table name in oracle
I am new to oracle, Please help

Achieved the expected result using the below UNPIVOT Query
SELECT TABLEPROCESS,TAB_COUNT,PROCESS_COUNT,STATUS
FROM
(
SELECT 'Tab1' AS tab1_Process,
count(tb1.ID) AS tab1_count,
count(tbp1.ID) AS tab1_Process_count,
CASE WHEN count(tb1.ID)=count(tbp1.ID) THEN 'Equal' ELSE 'Not Equal' END AS Tab1_Status,
'Tab2' AS tab2_Process,
count(tb2.ID) AS tab2_count,
count(tbp2.ID) AS tab2_Process_count,
CASE WHEN count(tb2.ID)=count(tbp2.ID) THEN 'Equal' ELSE 'Not Equal' END AS Tab2_Status
FROM table1 tb1
LEFT JOIN table1_process tbp1 ON tbp1.ID = tb1.ID
LEFT JOIN table2 tb2 ON tb2.ID = tb1.ID
LEFT JOIN table1_process tbp2 ON tbp2.ID = tb2.ID
)
UNPIVOT
(
(TABLEPROCESS,TAB_COUNT,PROCESS_COUNT,STATUS)
FOR (t,c,p,s)
IN
(
(tab1_Process,Tab1_Count,Tab1_Process_Count,Tab1_Status),
(tab2_Process,Tab2_Count,Tab2_Process_Count,Tab2_Status)
)
);

Related

Oracle Left join give duplicate

I am trying to get all the customers whether they have install service or not from TRANS_TABLE.
NOA- query to get only the MAX product and join again with TRANS_TABLE by email id to get the all the MAX customers details (wwhther they have install service by adding Y OR N, but this query return duplicate with REP Product as well
Below is my Oracel Query which give duplicated
with CTE as (SELECT NOA.*,
CASE
WHEN TRANS_TABLE.product_name LIKE '%Installation%' THEN 'Y'
ELSE 'N'
END AS Installaion ,
ROW_NUMBER() OVER (PARTITION BY TRANS_TABLE.email_address ORDER BY TRANS_TABLE.email_address) AS rn
FROM (SELECT DISTINCT email_address
FROM TRANS_TABLE
WHERE email_address IS NOT NULL
and pdct_name like '%MAX%'
) NOA
LEFT JOIN TRANS_TABLE
ON NOA.email_address = TRANS_TABLE.email_address
select * from cte where rn='1'
The following code will help:
CTE AS (
SELECT
NOA.*,
CASE
WHEN TRANS_TABLE.PDCT_NAME LIKE '%INSTALLATION%' THEN 'Y' -- case sensitive name is used
ELSE 'N'
END AS INSTALLAION,
ROW_NUMBER() OVER(
PARTITION BY TRANS_TABLE.EMAIL_ADDRESS
ORDER BY
TRANS_TABLE.EMAIL_ADDRESS
) AS RN
FROM
(
SELECT DISTINCT
PDCT_NAME,
EMAIL_ADDRESS
FROM
TRANS_TABLE
WHERE
EMAIL_ADDRESS IS NOT NULL
AND PDCT_NAME LIKE '%MAX%'
) NOA
LEFT JOIN TRANS_TABLE ON NOA.EMAIL_ADDRESS = TRANS_TABLE.EMAIL_ADDRESS
WHERE TRANS_TABLE.PDCT_NAME NOT LIKE '%REP%') -- added this WHERE condition
SELECT
PDCT_NAME, EMAIL_ADDRESS, INSTALLAION
FROM
CTE
WHERE
RN = '1'
db<>fiddle demo
Cheers!!

Subquery with Select statement works in 12C but not on 11g

Select
c.id AS case_id,
c.id AS case_number,
tt.description as Target_Type,
coalesce(
v_vendors.vendor_number,
vma.org_code,
TO_CHAR(sm.staff_member_id),
la.org_code,
oe.other_fns_number,
TO_CHAR(oe.other_id),
TO_CHAR(c_clients.client_id)
) as Target_Identifier,
coalesce(
v_vendors.vendor_name,
vma.name,
s_wic_users.username,
la.name,
oe.other_name,
UPPER(
c_clients.last_name || ', ' || c_clients.first_name
)
) as Target_Name,
c.date_opened AS Open_Date,
c.date_closed AS Close_Date,
t.target_type_id,
t.target_id,
s.description AS Status,
case when aIosm.last_name is null then '' else UPPER(
aIosm.last_name || ', ' || aIosm.first_name
) end Investigator,
case when aSosm.last_name is null then '' else UPPER(
aSosm.last_name || ', ' || aSosm.first_name
) end Supervisor,
c.Notes AS Supervisor_Comments,
aI.Date_Assigned as Assign_Date,
----subquery starts
(
select
lat.description
from
investigation_actions_taken iat
join investig_lu_action_type lat on iat.action_type_id = lat.id
where
iat.investigation_case_id = c.id
and iat.is_deleted <> 'T'
order by
iat.date_assessed desc,
iat.date_created desc OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY
) AS Last_Action
from
v_investigation_cases c
join investigation_target t on c.target_id = t.target_id
join v_lu_inv_status s on (
case when date_closed <= TRUNC(sysdate) then 2 else case when date_opened > TRUNC(sysdate) then 2 else 1 end end
) = s.status_id
join investigation_target_type tt on t.target_type_id = tt.target_type_id
---left join starts
left join investigation_case_assignment aI on c.id = aI.investigation_case_id
and aI.date_roll_off is null
and aI.assignment_type = 'I'
left join o_staff_members aIosm on aI.staff_member_id = aIosm.staff_member_id
left join investigation_case_assignment Super on c.id = Super.investigation_case_id
and Super.date_roll_off is null
and Super.assignment_type = 'S'
left join o_staff_members aSosm on Super.staff_member_id = aSosm.staff_member_id
-----left join starts
left join v_vendors on t.vendor_id = v_vendors.id
left join o_organizational_units vma on t.vendor_management_area_id = vma.seq_id
left join c_clients on t.client_id = c_clients.client_id
left join o_staff_members sm on t.user_id = sm.staff_member_id
left join s_wic_users on sm.staff_member_id = s_wic_users.sm_staff_member_id
left join investigation_other_entity oe on t.non_wic_vendor_id = oe.other_id
left join o_organizational_units la on t.local_agency_id = la.seq_id
---end of the query
Here's an article about what you can achieve in Oracle 12c with the OFFEST FETCH based on Kaushik Nayak.
http://www.dba-oracle.com/t_offset_fet_first_rows_only.htm
I hope it can help you to convert your original query to 11g. Good luck.

Alternate approach for Case statement in oracle

I have a query insert statement like shown below
insert into my_table
select col1,col2,CASE
WHEN EXISTS
(SELECT 1 FROM Table_A m WHERE m.COLA='XXX' AND m.id=b.id
)
THEN
(SELECT m.COLB FROM Table_A m WHERE m.COLA='XXX' AND m.id=b.id
)
ELSE 'Not Open'
END XXX,
CASE
WHEN EXISTS
(SELECT 1 FROM Table_A m WHERE m.COLA='YYY' AND m.id=b.id
)
THEN
(SELECT m.COLB FROM Table_A m WHERE m.COLA='YYY' AND m.id=b.id
)
ELSE 'Not Open'
END YYY,
.
.
.
.
. -- ~150 case statement
from Test b,table_main c
where b.id=c.id
Here we have case statement which is referring same table (TABLE_A) but checking for different values(XXX,YYY,etc..,) in each statement. This is taking so much time. We are trying to optimize this insert query. Could someone help here?
Try a variation of a pivot query:
SELECT col1, col2 ,
coalesce ( max( CASE WHEN m.COLA='XXX' THEN m.COLB END ), 'Not open' ) As XXX,
coalesce ( max( CASE WHEN m.COLA='YYY' THEN m.COLB END ), 'Not open' ) As YYY,
.....
.....
coalesce ( max( CASE WHEN m.COLA='ZZZ' THEN m.COLB END ), 'Not open' ) As ZZZ
FROM Test b
JOIN table_main c ON b.id=c.id
LEFT JOIN Table_A m ON m.id=b.id
GROUP BY col1, col2
Rewrite using decode and LEFT JOIN as follows.
SELECT col1, col2 ,
decode ( m.COLA , 'XXX' , m.COLB , 'Not Open' ) XXX ,
decode ( m.COLA , 'YYY', m.COLB , 'Not Open' ) YYY ,
...
...
FROM Test b LEFT JOIN Table_A m ON m.id=b.id JOIN table_main c
ON b.id=c.id;
It is taking time because you are making several calls to a table to get your values. Simply reduce the number of calls being made to Table_A.
If the condition Table_A.id = m.id and m.COLA= 'Any Value' will always return one row then you can join the tables Table_A, Test and Table_main in a single join like below
Insert into my_table(col1, col2) values
select
decode(a.COLA,'XXX' , a.COLB , 'Not Open'),
decode(a.COLA,'YYY' , a.COLB , 'Not Open'),
...
...
from Table_A a, Test b, Table_main c
where a.id = b.id
and b.id = c.id
In case the condition may return multiple records then you can use With clause like below
Insert into my_table(col1, col2) values
with table_a as (select col1, col2 , a.COLA, a.COLB
from Table_A a, Test b, Table_main c
where a.id = b.id
and b.id = c.id
group by col1, col2,a.COLA, a.COLB)
select col1,col2,
decode(x.COLA,'XXX' , x.COLB , 'Not Open'),
decode(x.COLA,'YYY' , x.COLB , 'Not Open'),
....
....
from table_a x

Populate data from one column to another column on condition in hive

WE have :-
TABLE_1 having column (A_COL,NUM ) and TABLE_2 having column ( B_COL ,C_COL , NUM)
Q condition
If A_COL is NULL in the TABLE_1 then, populate the C_COL into B_COL column from TABLE_2
TRIED :-
Select
CASE WHEN A_COL IS NULL THEN B_COL = C_COL end as ABC_COL
from line
FROM TABLE_2 A
LEFT OUTER JOIN
TABLE_1 B
ON
A.NUM = B.NUM
I tried this but B_COL = C_COL not able to populate column C_COL into B_COL ,Do we have any other way to do this?
Try the below :
tab_1 as A and tab_2 as B
Selecting complete table 2 with condition :
Select
B.colB,
if ( A.colA = null , B.colB , B.colC) as B.colC,
B.num
from Tab_2 B
LEFT OUTER JOIN
TABLE_1 A
ON
A.NUM = B.NUM
Let me know if it dint work out.

Sql recursive error invalid column name CTE

I have done following CTE it works fine,but I need to get sum of the last node so the problem is when I add T1.Debit Column to be calculated its give me an invalid column name 'Debit' !!! on line ***
ALTER Function [dbo].[SubTopics_GetSum]
-- Add the parameters for the stored procedure here
(
#TopicID int
)
RETURNS TABLE
AS
RETURN
(
-- Add the SELECT statement with parameter references here
WITH cte
AS (
SELECT
T1.TopicID,
T1.Code,
T1.Description,
T1.ParentID,
T1.ParentID AS NewParentID,
CAST(T1.Code AS nvarchar(MAX)) AS TopicCode,
CAST(T1.Description AS nvarchar(MAX)) AS TopicDescription,
isnull((Accounting.DocumentDetail.Debit),0) AS Debit,
isnull((Accounting.DocumentDetail.Credit),0) AS Credit
FROM Accounting.Topics AS T1
LEFT OUTER JOIN Accounting.DocumentDetail
ON T1.TopicID = Accounting.DocumentDetail.TopicFK
where NOT EXISTS(
SELECT
T2.TopicID,
T2.Code,
T2.Description,
T2.ParentID,
isnull((Accounting.DocumentDetail.Debit),0) AS Debit,
isnull((Accounting.DocumentDetail.Credit),0) AS Credit
FROM Accounting.Topics AS T2
LEFT OUTER JOIN Accounting.DocumentDetail
ON T2.TopicID = Accounting.DocumentDetail.TopicFK
WHERE (ParentID = T1.TopicID)
)
UNION ALL
SELECT
c.TopicID,
c.Code,
c.Description,
c.ParentID,
T1.ParentID AS NewParentID,
CAST(T1.Code AS nvarchar(MAX)) + c.TopicCode AS TopicCode,
CAST(T1.Description AS nvarchar(MAX)) + ' - ' + c.TopicDescription AS TopicDescription,
*** isnull((T1.Debit),0)+isnull(c.Debit,0) AS Debit,--IN THIS LINE error 'Invalid Column Name 'Debit''
isnull(c.Credit,0) AS Credit
FROM cte AS c
INNER JOIN Accounting.Topics AS T1
ON T1.TopicID = c.NewParentID
)
SELECT isnull(sum(Debit),0)AS Debit,
isnull(sum(Credit),0)AS Credit
FROM cte AS c
WHERE (NewParentID = #TopicID)
)
let me know whats wrong with my code confused !!!
actually it doesn't return me a last node sum of debit,credit ... !!!
check following pic
I think below code will help,
ALTER FUNCTION [dbo].[Subtopics_getsum]
-- Add the parameters for the stored procedure here
(#TopicID INT)
returns TABLE
AS
RETURN (
-- Add the SELECT statement with parameter references here
WITH cte
AS (SELECT T1.topicid,
T1.code,
T1.description,
T1.parentid,
T1.parentid AS
NewParentID
,
Cast(T1.code AS NVARCHAR(max))
AS TopicCode,
Cast(T1.description AS NVARCHAR(max)) AS
TopicDescription,
Isnull(( accounting.documentdetail.debit ), 0) AS Debit,
Isnull(( accounting.documentdetail.credit ), 0) AS Credit
FROM accounting.topics AS T1
LEFT OUTER JOIN accounting.documentdetail
ON T1.topicid =
accounting.documentdetail.topicfk
WHERE NOT EXISTS(SELECT T2.topicid,
T2.code,
T2.description,
T2.parentid,
Isnull(( accounting.documentdetail.debit ), 0)
AS
Debit,
Isnull(( accounting.documentdetail.credit ), 0) AS
Credit
FROM accounting.topics AS T2
LEFT OUTER JOIN accounting.documentdetail
ON T2.topicid =
accounting.documentdetail.topicfk
WHERE ( parentid = T1.topicid )))
SELECT Isnull(Sum(debit), 0) AS Debit,
Isnull(Sum(credit), 0)AS Credit
FROM cte AS c
WHERE ( newparentid = #TopicID )
)
I think you Accounting.Topics hasn't column Debit.
UPDTAE
isnull((T1.Debit),0)+isnull(c.Debit,0) AS Debit,--IN THIS LINE error 'Invalid Column Name 'Debit''
isnull(c.Credit,0) AS Credit
FROM cte AS c
INNER JOIN Accounting.Topics AS T1
ON T1.TopicID = c.NewParentID
In the code above your T1.Debit refers to Accounting.Topics.

Resources