Strange condition while trying to get the contact information (EBS) - oracle

I dont know how to get all the contact information that is stored in HZ_PARTIES.
We have HZ_PARTIES AND HZ_PARTY_SITES, both have contact information, right?.
I want to obtain the information from HZ_PARTIES.
The problem is that my query is not working properly, it does not retrieve the information that i want. If i use this query:
SELECT
hp.party_name
, hca.account_number
, hcp.phone_number
, hcp.email_address
FROM apps.hz_cust_accounts hca
INNER JOIN apps.hz_cust_acct_sites_all hcas ON hca.cust_account_id = hcas.cust_account_id
INNER JOIN apps.hz_party_site hps ON hcas.party_site_id = hps.party_site_id
INNER JOIN apps.hz_locations hl ON hps.location_id = hl.location_id
INNER JOIN apps.hz_parties hp ON hps.party_id = hp.party_id
LEFT JOIN (
SELECT
owner_table_id
, max(case when contact_point_type = 'PHONE' then phone_number end) phone_number
, max(case when contact_point_type = 'EMAIL' then email_address end) email_address
FROM hz_contact_points
WHERE status = 'A'
AND primary_flag = 'Y'
AND owner_table_name = 'HZ_PARTIES'
AND contact_point_type IN ('EMAIL','PHONE')
GROUP BY
owner_table_id
) hcp ON hcas.party_site_id = hcp.owner_table_id
WHERE hcas.status = 'A'
AND hps.status = 'A'
AND hca.status = 'A'
--AND hca.account_number = 'account_number'
;
DO NOTE that i am using this condition:
hcp ON hcas.party_site_id = hcp.owner_table_id
But this query IS NOT working properly because if i want to find all the contact information for a specific account number, it should return an email but instead it returns NULL
The fact is if i put this another condition in exchange for the previous one:
hcp ON hcas.party_site_id = hps.party_site_id
It returns several results for a single account_number, everything is wrong BUT it DOES show the email that i am looking for.
What is going on here?
Could you please help me to solve this?
PS: Everytime that a customers is created, this PROCEDURE is executed:
https://www.codepile.net/pile/9nzMKy4D

As I know HZ_PARTIES = HZ_PARTY_SITES this is one to many relation. So HZ_PARTY_SITES can contain e.g. address "bill to", "ship to" etc.
First of all, check in EBS how many sites the customer has.
I also recommend you page http://etrm.oracle.com/pls/etrm/etrm_search.search. You can check the relation between the EBS's tables there.
enter image description here
As you can see, the relation between hcp ON hcas.party_site_id = hcp.owner_table_id
not exists.

Related

Syntax to escape a reserved word in Oracle while using abbreviated table names

I have an Oracle 11g database with table named "time_recs". I need to run a query in SQL Developer, including a column named comment.
As comment is a reserved word, I need to escape it, that´s ok. The problem is I can´t find the correct syntax when you are using abbreviated table names.
E.g.: If I run select "comments" from "time_recs"; it works well.
But the report I need to run is pasted below. If you see line 17 ("tr"."comment"), I´ve tried a lot of different things: quotes, double quotes, etc. Still I can´t make it work as I keep getting the "invalid identifier" error
Any help will be really appreciated.
select
tr.record_date as "Date"
, ua4.string_value as "Employee Number"
, case when p.id_parent = 'root' then p.pname else pp.pname end as "Parent Project Name"
, p.pname as "Project"
, ct.pname as "Work Package"
, tra1.string_value as "Cost Center"
, ua1.string_value as "Discipline"
, case when ua3.string_value = 'Research' then 'R' else 'D' end as "R or D"
, 'N/A' as "Sub Discipline"
, tra2.string_value as "Primary Indication"
, tra3.string_value as "Project Phase"
, pa1.string_value as "Project Template Type"
, pa2.string_value as "Secondary Indication"
, pa3.string_value as "Therapeutic Area"
, sum(tr.time_amount) as "Hours"
, "tr"."comment"
from time_recs tr
left join users us on
tr.id_user = us.id_user
left join users_attribs ua1 on
tr.id_user = ua1.id_user
left join projects p on
tr.id_project = p.id_project
join projects pp on
pp.id_project = p.id_parent
join codes_tasks ct on
ct.id_code = tr.id_code_task
left join time_recs_attribs tra1 on
tr.id_time_rec = tra1.id_time_rec
join attribute_types attr1 on
attr1.id_attr_type = tra1.id_attr_type and attr1.pname = 'Cost Center'
join attribute_types atua2 on
atua2.id_attr_type = ua1.id_attr_type and atua2.pname = 'Discipline'
left join users_attribs ua3 on
tr.id_user = ua3.id_user
join attribute_types atua3 on
atua3.id_attr_type = ua3.id_attr_type and atua3.pname = 'Organization'
left join users_attribs ua4 on
tr.id_user = ua4.id_user and ua4.id_attr_type='D00C686107154F3FB2B97F47B172CB7F' --Employe Number
left join time_recs_attribs tra2 on
tr.id_time_rec = tra2.id_time_rec
join attribute_types attr2 on
attr2.id_attr_type = tra2.id_attr_type and attr2.pname = 'Primary Indication'
left join time_recs_attribs tra3 on
tr.ID_TIME_REC = tra3.ID_TIME_REC
join attribute_types attr3 on
attr3.id_attr_type = tra3.id_attr_type and attr3.pname = 'Project Phase'
right join projects_attribs pa1 on
tr.id_project = pa1.id_project
join attribute_types atpa1 on
atpa1.id_attr_type = pa1.id_attr_type and atpa1.pname = 'Project Template Type'
left join projects_attribs pa2 on
tr.id_project = pa2.id_project
join attribute_types atpa2 on
atpa2.id_attr_type = pa2.id_attr_type and atpa2.pname = 'Secondary Indication'
left join projects_attribs pa3 on
tr.id_project = pa3.id_project
join attribute_types atpa3 on
atpa3.id_attr_type = pa3.id_attr_type and atpa3.pname = 'Therapeutic Area'
where
tr.record_date >= 20190101
and tr.record_date <= 20190131
having sum(tr.time_amount) >0
group by
tr.record_date
, ua4.string_value
, case when p.id_parent = 'root' then p.pname else pp.pname end
, p.pname
, ct.pname
, tra1.string_value
, ua1.string_value
, case when ua3.string_value = 'Research' then 'R' else 'D' end
, 'N/A'
, tra2.string_value
, tra3.string_value
, pa1.string_value
, pa2.string_value
, pa3.string_value
;
You don't need anything; use alias as is:
SQL> create table time_recs ("comment" varchar2(10));
Table created.
SQL> insert into time_recs ("comment") values ('Littlefoot');
1 row created.
SQL> select tr."comment" --> here
2 from time_recs tr;
comment
----------
Littlefoot
SQL>
thank you for your comments. Well, now I see that I have to be more careful with typing. The column is named comment (lowercase without quotes).
It works if I run the following simple query:
select "comment" from "time_recs";
However, I can´t make it work with the abbreviated table names. – I´m also providing a list of the table fields screenshot of table fields

SQL: Recursion With a Join Confusion

I'll start by laying out what I'm trying to do and then I'll list the code I've made so far.
I'm coding in Oracle PL/SQL on the Application Express Platform
I have two tables: USERS and LEADS.
Leads Columns: LEADID, COMPANYNAME, CONTACTNAME, OWNER
Users Columns: EMAIL, SUPER, ROLE
Foreign Keys:
OWNER is a foreign key that refers to EMAIL in USERS
SUPER is a foreign key that refers to EMAIL in USERS
SUPER is the supervisor of a given person. ROLE is their position in the company
There are about 5 levels. 'PEON','MGR','DIR','SRDIR','VP'
Peons are the only people with leads assigned to them.
I'm trying to generate a report that returns rows containing the following
SUBORDINATE, NUMLEADS
Subordinate is anyone directly under the user using the application. I have code for that
select U.EMAIL as Subordinate
from USERS U
WHERE lower(v('APP_USER')) = U.SUPER
Numleads is all the leads created by peons under the subordinate's organization. I currently have code to list the number of peons under the current user
select count(*)
from USERS U2
where U2.ROLE = 'PEON'
start with lower(v('APP_USER')) = U2.EMAIL
connect by NOCYCLE prior U2.email = U2.super
I'm part of the way there, but I'm confused how to reference the result of a query in a recursive sequence. I know I need to query all PEONS under the subordinates of the current user, JOIN them with all leads they're associated with, and then count the number of leads. But i'm not sure how to order that in SQL.
Your help is much appreciated
EDIT: Answer figured out thanks to JBrooks
select U.EMAIL as Sub, count(*) as CreatedAllTime
from USERS U
left join USERS UPEON
on UPEON.EMAIL in
(
select UPEON2.EMAIL
from USERS UPEON2
where UPEON2.ROLE = 'PEON'
start with U.EMAIL = UPEON2.EMAIL
connect by NOCYCLE prior UPEON2.email = UPEON2.super
)
left join LEADS L
on UPEON.EMAIL = L.OWNER
where U.EMAIL in
(
select U2.EMAIL as Sub
from USERS U2
WHERE lower(v('APP_USER')) = U2.SUPER
)
group by U.EMAIL
select U2.Email as Subordinate,
count(*) as NumLeads
from USERS U2
left join LEADS l
on U2.Email = l.Owner
where U2.ROLE = 'PEON'
and lower(v('APP_USER')) in
(select EMAIL
from USERS S
START WITH lower(v('APP_USER')) = lower(S.SUPPER)
CONNECT BY PRIOR EMAIL = SUPPER)
group by U2.Email
order by U2.Email

Oracle Inner Table Query

I am trying to write a query in Oracle to give totals of active legal entities and inactive legal entities.
The query so far I have is:
select le.Name, b.LE_ID, count(*) As TOTAL, dead.LE_ID as DEAD
from BOOK b
left join Legal_Entity le on le.LE_ID = b.LE_ID
left join
(
select count(LE_ID) as LE_ID
from BOOK
where (Name like '%DUMMY%' or name like '%TEST%' or name like '%DEAD%' or name like '%DO NOT%' or status <> 'Active')
) dead on dead.LE_ID = b.LE_ID
where b.LE_ID = 1234
group by le.Name, b.LE_ID, dead.LE_ID
order by b.LE_ID;
The results I am expecting are:
Name EntityID Total Dead
Entity A 1234 500 200
i.e. for Book.LE_ID = 1234 I would like one row in the result set and a column with the Total number i.e. select * from Book where LE_ID = 1234 and a column with the number of dead books i.e. the inner query
But at the moment, my query is returning NULL for the number of dead rows.
The inner query is working without issue, but I'm clearly missing something here.
I am not sure, but shouldn't it be something like this?
select le.Name, b.LE_ID, count(*) As TOTAL, dead.DEAD as DEAD
from BOOK b
left join Legal_Entity le on le.LE_ID = b.LE_ID
left join
(
select LE_ID, count(LE_ID) as DEAD
from BOOK
where (Name like '%DUMMY%' or name like '%TEST%' or name like '%DEAD%' or name like '%DO NOT%' or status <> 'Active')
group by LE_ID
) dead on dead.LE_ID = b.LE_ID
where b.LE_ID = 1234
group by le.Name, b.LE_ID, dead.LE_ID
order by b.LE_ID;
It is a bit hard to figure out exactly what you are trying to do. After all, you are trying to join between a count and an id, something that is not likely to lead to success.
Your example has a name 'Entity A'. By the rules specified in the query for "dead" entities, there should be none. The name doesn't match this rule.
In all likelihood, you want conditional aggregation. Here is a query that I think gets the totals over all entities:
select count(*) As TOTAL,
sum(case when Name like '%DUMMY%' or name like '%TEST%' or name like '%DEAD%' or name like '%DO NOT%' or status <> 'Active'
then 1 else 0
end) as dead
from BOOK b left join
Legal_Entity le
on le.LE_ID = b.LE_ID;

XML query having arguments from different table

I need to frame a query where the argument to be passed against 'city' here will come form another table by join operation.
Actual query:-
SELECT c.cid FROM customer c
WHERE XMLEXISTS('$d/*:customerinfo/*:addr[ *:city = "Aurora" ]'
PASSING info AS "d")
I want the city value to be come from two different tables by join.
Like:-
SELECT c.cid FROM customer c
WHERE XMLEXISTS('$d/*:customerinfo/*:addr[ //*:city = $city ]'
PASSING info AS "d" , (select city from customer C , Office D where C.city = D.city ) as city )
This is not working , please suggest an alternate way for it.

Using group by subquery to deduplicate record set

I would like to know how I can return the activity Id to a parent query using a query like the following to de-duplicate the parent record set. The issue here is that using this group by or distinct I cannot find a way.
I will use all of the group by fields to determine a unique record. But, I need to use only the record with the select min(status.effective_date)
The query returns the correct date values, but I cannot link it back to the parents activity records with just that date value.
select min(status.effective_date)
from accounts
, address
, activity
, status
where accounts.par_row_id = activity.account_id
and address.row_id = activity.address_id
and status.par_row_id = activity.status_id
and account.name = 'xyz'
group by account.name, address.addr, address.ADDR_LINE_2, address.ADDR_LINE_3
, address.ADDR_LINE_4, address.CITY, address.COUNTRY, address.X_STATE
, address.ZIPCODE
You should try this query:
select min(activity.row_id) keep(dense_rank first order by status.effective_date) as activity_id
from accounts
, address
, activity
, status
where accounts.par_row_id = activity.account_id
and address.row_id = activity.address_id
and status.par_row_id = activity.status_id
and accounts.name = 'xyz'
group by accounts.name, address.addr, address.ADDR_LINE_2, address.ADDR_LINE_3
, address.addr_line_4, address.city, address.country, address.x_state
, address.ZIPCODE;

Resources