ORACLE BIPUBLISHER SQL Query taking too long to execute - sql

Following is the query
Select
pu.username,
ROLE.ROLE_NAME,
(
CASE
WHEN ((ROLE.ORG_ID IS NOT NULL) AND ROLE.ORG_ID=fabuv.BU_ID) THEN FABUV.BU_NAME
WHEN ((ROLE.LEDGER_ID IS NOT NULL) AND ROLE.LEDGER_ID=gl.LEDGER_ID) THEN GL.NAME
WHEN ((ROLE.BOOK_ID IS NOT NULL) AND ROLE.BOOK_ID=fbc.BOOK_CONTROL_ID) THEN FBC.BOOK_TYPE_NAME
WHEN ((ROLE.SET_ID IS NOT NULL) AND ROLE.SET_ID=fssv.SET_ID) THEN FSSV.SET_NAME
WHEN ((ROLE.INV_ORGANIZATION_ID IS NOT NULL) AND ROLE.INV_ORGANIZATION_ID=iop.ORGANIZATION_ID) THEN IOP.ORGANIZATION_CODE
WHEN ((ROLE.CST_ORGANIZATION_ID IS NOT NULL) AND ROLE.CST_ORGANIZATION_ID=ccov.COST_ORG_ID) THEN CCOV.COST_ORG_CODE
WHEN ((ROLE.ACCESS_SET_ID IS NOT NULL) AND ROLE.ACCESS_SET_ID=gas.ACCESS_SET_ID) THEN GAS.NAME
WHEN ((ROLE.CONTROL_BUDGET_ID IS NOT NULL) AND ROLE.CONTROL_BUDGET_ID=xcb.CONTROL_BUDGET_ID) THEN XCB.NAME
WHEN ((ROLE.INTERCO_ORG_ID IS NOT NULL) AND ROLE.INTERCO_ORG_ID=fio.INTERCO_ORG_ID) THEN FIO.INTERCO_ORG_NAME
ELSE 'NOT_APPLICABLE'
END
) "Security_Context_Value"
from
fusion.per_users pu,
fusion.FA_BOOK_CONTROLS FBC,
fusion.FUN_ALL_BUSINESS_UNITS_V FABUV,
fusion.XCC_CONTROL_BUDGETS XCB,
fusion.CST_COST_ORGS_V CCOV,
fusion.gl_access_sets GAS,
fusion.FUN_INTERCO_ORGANIZATIONS FIO,
fusion.INV_ORG_PARAMETERS iOP,
fusion.GL_LEDGERS GL,
fusion.RCS_MFG_PARAMETERS RMP,
fusion.FUN_USER_ROLE_DATA_ASGNMNTS ROLE,
fusion.FND_SETID_SETS_VL FSSV
where
1=1
and pu.USERNAME=:Username
AND ROLE.ORG_ID != NULL
OR ROLE.LEDGER_ID != NULL
OR ROLE.BOOK_ID != NULL
OR ROLE.SET_ID != NULL
OR ROLE.INV_ORGANIZATION_ID != NULL
OR ROLE.CST_ORGANIZATION_ID != NULL
OR ROLE.ACCESS_SET_ID != NULL
OR ROLE.CONTROL_BUDGET_ID != NULL
OR ROLE.INTERCO_ORG_ID != NULL
And pu.USER_GUID=role.USER_GUID
It is the query required to extract roles assigned to users with Security_Context and Value.
I have tried different combinations of using IS NOT NULL in place of '!=' but it also didnt help, again Username is not taken into consideration when if it run.

I think what you're after is something like:
SELECT pu.username,
role.role_name,
CASE WHEN ROLE.ORG_ID IS NOT NULL THEN FABUV.BU_NAME
WHEN ROLE.LEDGER_ID IS NOT NULL THEN GL.NAME
WHEN ROLE.BOOK_ID IS NOT NULL THEN FBC.BOOK_TYPE_NAME
WHEN ROLE.SET_ID IS NOT NULL THEN FSSV.SET_NAME
WHEN ROLE.INV_ORGANIZATION_ID IS NOT NULL THEN IOP.ORGANIZATION_CODE
WHEN ROLE.CST_ORGANIZATION_ID IS NOT NULL THEN CCOV.COST_ORG_CODE
WHEN ROLE.ACCESS_SET_ID IS NOT NULL THEN GAS.NAME
WHEN ROLE.CONTROL_BUDGET_ID IS NOT NULL THEN XCB.NAME
WHEN ROLE.INTERCO_ORG_ID IS NOT NULL THEN FIO.INTERCO_ORG_NAME
ELSE 'NOT_APPLICABLE'
END "Security_Context_Value"
FROM fusion.per_users pu
INNER JOIN fusion.fun_user_role_data_asgnmnts role ON pu.user_guid = role.user_guid
LEFT OUTER JOIN fusion.fun_all_business_units_v fabuv ON role.org_id = fabuv.bu_id
LEFT OUTER JOIN fusion.gl_ledgers gl ON role.ledger_id = gl.ledger_id
LEFT OUTER JOIN fusion.fa_book_controls fbc ON role.book_id=fbc.book_control_id
LEFT OUTER JOIN fusion.fnd_setid_sets_vl fssv ON role.SET_ID=fssv.SET_ID
LEFT OUTER JOIN fusion.inv_org_parameters iop ON role.inv_organization_id=iop.organization_id
LEFT OUTER JOIN fusion.cst_cost_orgs_v ccov ON role.cst_organization_id=ccov.cost_org_id
LEFT OUTER JOIN fusion.gl_access_sets gas ON role.access_set_id=gas.access_set_id
LEFT OUTER JOIN fusion.xcc_control_budgets xcb ON role.control_budget_id=xcb.control_budget_id
LEFT OUTER JOIN fusion.fun_interco_organizations fio ON role.interco_org_id=fio.interco_org_id
WHERE pu.username = :username
AND COALESCE(role.org_id,
role.ledger_id,
role.book_id,
role.set_id,
role.inv_organization_id,
role.cst_organization_id,
role.access_set_id,
role.control_budget_id,
role.interco_org_id) IS NOT NULL;
Note the use of the ANSI join syntax, which forces you to add in join conditions (unless you explicitly request a cross join).
Your original query was doing a cross join between your fabuv, gl, fbc, etc tables, meaning each row in fabuv was being joined to all rows in gl, and each of the resultant rows was being joined to every row in the fbc table and so on... that's a lot of rows - e.g. if there are 100 rows in each of the fabuv, gl, fbc, etc tables, you're going to end up with 100 x 100 x 100 x ... which ends up being way too many rows.
I've take a guess as to what the join conditions should be, based on your case statement - n.b. the rmp table wasn't being referenced at all, so I've excluded it from the from clause.
I also used the COALESCE function to simplify your where clause - this function returns the first not-null value.

Related

Why is this Hive code returning 0 results?

select *
FROM prd_raw_sf.sf_opportunity_dn A JOIN
prd_raw_sf.sf_opportunity_rw B
ON A.OPPORTUNITYID = B.SFDC_ID LEFT JOIN
prd_raw_sf.sf_si_accounts_mapping C
ON TRIM(UPPER(A.ACCOUNT_NAME)) = TRIM(UPPER(C.sfdc_account_name))
WHERE C.sfdc_account_name IS NULL
and C.Billing_Client in ('CL.00000:')
It returns the necessary results when I query till "WHERE C.sfdc_account_name IS NULL". However, if I add the last line... "and C.Billing_Client in ('CL.00000:')", it just returns 0 result. Please help!
I speculate that you intend:
SELECT *
FROM prd_raw_sf.sf_opportunity_dn od JOIN
prd_raw_sf.sf_opportunity_rw orw
ON od.OPPORTUNITYID = orw.SFDC_ID LEFT JOIN
prd_raw_sf.sf_si_accounts_mapping am
ON TRIM(UPPER(od.ACCOUNT_NAME)) = TRIM(UPPER(ord.sfdc_account_name)) AND
am.Billing_Client in ('CL.00000:')
WHERE am.sfdc_account_name IS NULL;
It is impossible for the account name to be NULL after the LEFT JOIN and for the Billing_Client to be anything other than NULL. That is because the account name is used as a JOIN key, so NULL does not match. The NULL value indicates no match, so all other columns from the table are NULL.
Note that I also fixed the table aliases so they are meaningful rather than arbitrary letters.

LEFT OUTER JOIN with isnull

This is a pretty straightforward LEFT OUTER JOIN, but I get zero returned rows:
SELECT All_varieties.*,variety_join.*
FROM All_varieties LEFT JOIN
variety_join
ON All_varieties.varietyID = variety_join.varietyID
WHERE (variety_join.sourceID = Null);
However, I am using a query All_varieties
SELECT plant.plantName, plant.plantGenus, plant.plantSpecies, variety.varietyLatin, variety.varietyEnglish, variety.varietyID
FROM plant LEFT JOIN
variety
ON plant.plantID = variety.plantID;
Is the problem maybe with the query All_varieties?
TABLES:
It should be variety_join.sourceID is null instead of variety_join.sourceID = Null
SELECT All_varieties.*,variety_join.*
FROM All_varieties LEFT JOIN
variety_join
ON All_varieties.varietyID = variety_join.varietyID
WHERE variety_join.sourceID is Null

Why is this SQL query returning this result?

Can anyone tell me why this sql query is returning this result.
SELECT
M.MatterNumber_Id, M.MatterNumber, M.MatterName,
ISNULL(MP.Role_Cd, 'No Primary') AS PrimaryRole,
ISNULL(E.Name, 'No Primary') AS PrimaryName,
ISNULL(C.CommNumber, 'l.w#abc.ca;m.j#abc.com') AS Email
FROM
Matter M
LEFT OUTER JOIN
MatterPlayer MP ON M.MatterNumber_Id = MP.MatterNumber_Id AND
MP.Role_Cd IN ('Primary Lawyer', 'Primary Staff Member') AND
MP.EndDate IS NULL
LEFT OUTER JOIN
Entity E on MP.Entity_EID = E.Entity_EID
LEFT OUTER JOIN Communication C on MP.Entity_EID = C.Entity_EID AND
C.CommunicationType_Cd = 'Email'
LEFT OUTER JOIN
MatterExposure ME on M.MatterNumber_Id = ME.MatterNumber_Id AND
ME.AssessedDate > '7/10/2014' AND
ME.Currency_CD IS NOT NULL
WHERE
M.MatterStatus_Cd = 'Active' AND
ME.AssessedDate IS NULL AND
M.Matter_Cd in
(SELECT
rpl.Type_CD
FROM
RuleProfile_Tabs rpt
INNER JOIN
RuleProfile_LookupCode rpl ON rpt.RuleProfile_ID = rpl.RuleProfile_ID
WHERE
tab_id = 1034 AND Caption LIKE 'Reportable Matter%')
ORDER BY
Email, M.MatterName
But When i see the results I check one of the records returned and it should not have been returned.
One of the results returned:
Bailey, Richard
In the db i check the values in the tables it should not have been returned because Currency_CD is null and in the sql it states currency_cd is not null. Also the assessed date is AFTER 7/10/2014
Table Values:
MatterStatus_CD:
Active
AssessedDate:
7/24/2014
Currency_CD:
NULL
EndDate:
NULL
Where clauses are used for filtering, not the join clause. Every JOIN should have an ON. Here's a head start.
Select M.MatterNumber_Id, M.MatterNumber, M.MatterName,
IsNull(MP.Role_Cd, 'No Primary') as PrimaryRole,
IsNull(E.Name, 'No Primary') as PrimaryName,
IsNull(C.CommNumber, 'l.w#abc.ca;m.j#abc.com') as Email
From Matter M
Left Outer Join MatterPlayer MP on M.MatterNumber_Id = MP.MatterNumber_Id
Left Outer Join Entity E on MP.Entity_EID = E.Entity_EID
Left Outer Join Communication C on MP.Entity_EID = C.Entity_EID
Left Outer Join MatterExposure ME on M.MatterNumber_Id = ME.MatterNumber_Id
Where M.MatterStatus_Cd = 'Active'
AND ME.AssessedDate is Null
and M.Matter_Cd in (select rpl.Type_CD
from RuleProfile_Tabs rpt
inner join RuleProfile_LookupCode rpl on rpt.RuleProfile_ID = rpl.RuleProfile_ID
where tab_id = 1034 AND Caption like 'Reportable Matter%')
and MP.Role_Cd In ('Primary Lawyer', 'Primary Staff Member')
and MP.EndDate is Null
and ME.AssessedDate > '7/10/2014'
and ME.Currency_CD IS NOT NULL
and C.CommunicationType_Cd = 'Email'
Order by Email, M.MatterName
You are LEFT JOINing MatterExposure onto Matter. Your Me.Currency_CD IS NOT NULL criteria in the LEFT JOIN's ON statement is only saying, "don't join to records where Currency_CD is null..." but because this is a left join, none of your Matter table's records are lost in the join. So when you ask for Currency_CD you will get NULL.
To remove records from your query where Currency_CD is null in the MatterExposure table you will either need to specify Currency_CD IS NOT NULL in your where statement (instead of your join) or will need to use an INNER JOIN to your MatterExposure table.
It is because this join is a LEFT OUTER JOIN. If you want to enforce this join make it an inner join. Or move that check to the WHERE clause.

SQL JOIN two tables with null values

I am having a hard time figuring this query out. I need records from issueMaster based on userId and visible, joined to issueActionDetails that are visible, but there may not be any issueActionDetails. I am only getting issues that have actions.
SELECT
b.AssignedTo,
b.Visible,
c.ONEKEY_ID,
C.PHYS_NAME[Doctor],
B.IssueDescription[Issue to Address],
CASE WHEN A.ActionDescription IS NULL THEN 'To Be Assigned' ELSE A.ActionDescription END as [Action],
CASE WHEN A.AssignedTo IS NULL THEN 'To Be Assigned' ELSE A.AssignedTo END as [Assigned To],
CASE WHEN D.[Description] IS NULL THEN 'To Be Assigned' ELSE D.[Description] END As [Status]
FROM [dbo].[tbl_IssueMaster] B
LEFT JOIN [dbo].[tbl_IssueActionDetails] A ON A.IssueID =B.IssueID
INNER JOIN [dbo].[tbl_DoctorsNew] C ON B.OnekeyID =C.ONEKEY_ID
INNER JOIN [dbo].[Action_Status] D ON A.ActionStatus = D.ID
WHERE B.AssignedTo = #UserId AND B.Visible =1 AND A.Visible =1
ORDER BY c.ONEKEY_ID,B.DisplayOrder ,A.DisplayOrder
The "outerness" of the join to tbl_IssueActionDetails is negated by two things:
The inner join to Action_Status:
INNER JOIN [dbo].[Action_Status] D ON A.ActionStatus = D.ID
And the predicate in the WHERE clause
AND A.Visible =1
For any rows returned where there isn't a matching row in tbl_IssueActionDetails, the values in the columns for that table will all be NULL. (The database is essentially creating a row from tbl_IssueActionDetails that consists of all NULLs, and "matching" that row to the row from the table on the left side.
Any predicate that excludes NULL values from columns in that table will exclude that row. If we specify to only return rows where "A.Visible=1", that will exclude any rows that have a NULL value in that column. Which is what I meant when I said the "outerness" of the LEFT JOIN operation was negated.
The fix is to move the predicate to the ON clause of the join to tbl_IssueActionDetail
and change that INNER JOIN to an outer join LEFT JOIN
LEFT JOIN [dbo].[tbl_IssueActionDetails] A
ON A.IssueID = B.IssueID
AND A.Visible = 1
remove "AND A.Visible = 1" from the where clause, add it as a join condition instead
OR, alter the where clause to permit a NULL condition for the LEFT JOIN
WHERE B.AssignedTo = #UserId
AND B.Visible = 1
AND ( A.Visible = 1 OR A.Visible IS NULL)
If you always insist that A.Visible = 1 in the where clause you suppress the NULLs that a LEFT JOIN can provide. Be on guard for this whenever you use an outer join.

including a condition dynamically based on another condition

I have a query as below
select --------
from table a
left outer join ....c
where
(a.column='123') and (c.column='456')
I would like to
include "(c.column='456')" only when (a.column='123') is not null
how do I do that in a single query ? or do I need to write two separate queries ?
Should be pretty straightforward :
select --------
from table
left outer join....
where (Condition A IS NULL) OR (condition A AND condition B)
UPDATED: For your conditions:
where (a.column is null) or (a.column='123' and c.column='456')
It will include a a row if it's a.column is null or if bot a.column and c.column have valid values.
As I understand your requirement this is the sql you want
select distinct cm.credit_amt,e.credit_lifetime,e.credit_desc,e.icom_code,e.entry_hint,
e.credit_id,e.credit_type_id,e.recontract_period,a.class_desc,a.offer_id,
a.offer_class_id
from rti_retention.package a
left outer join rti_retention.offer_class_credit b on (a.offer_id=b.offer_id
and a.offer_class_id=b.offer_class_id
and a.customer_type_id=b.customer_type_id)
left outer join rti_retention.credit_pre_bundle c on (b.credit_id=c.credit_id)
left outer join rti_retention.credit e on (c.credit_id=e.credit_id)
left outer join rti_retention.credit_mix_amount cm on (cm.credit_id=c.credit_id and cm.prod_mix_id=a.prod_mix_id)
where a.offer_class_id not in (1,2,16)
and a.channel_id=5 and a.customer_type_id=1
and a.offer_id='6055'
and c.prod_mix_id = case when (select count(*)
from rti_retention.credit_pre_bundle c1
where c1.prod_mix_id='1000' ) > 1 then '1000' else c.prod_mix_id end
and e.icom_code is not null
some time there will be some sql syntax errors. due to i havent full data base i wrote sql on mind. cant test it.