XML query having arguments from different table - oracle

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.

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

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

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.

ORA-00918 returns from stored procedure but it works executing a query in SQL Page

I'm trying return a list from db but it gives me Error "ORA-00918: column ambiguously defined".
When I execute this query inside new SQL page, it returns true list. However, when I write it in a package as stored procedure, it returns ORA-00918 and package goes invalid status.
What is the reason for this difference?
select distinct c.customer_no, m.title, c.group_id, g.name, c.pricelist_id, p.name from db.customer c
join db.pricelist p on c.pricelist_id = p.pricelist_id
join db.master m on c.customer_no = m.customer_no
join db.group g on c.group_id = g.id
where (c.customer_no = pn_customer_no or pn_customer_no=-1)
and (c.group_id = pn_group_no or pn_group_no=-1)
and (c.pricelist_id = pn_pricelist_no or pn_pricelist_no=-1)
and (c.kom_type = ps_kom_tip)
order by c.customer_no asc
You are selecting the columns:
select distinct
c.customer_no,
m.title,
c.group_id,
g.name, -- NAME column
c.pricelist_id,
p.name -- NAME column
When you run the query in SQL/Plus or SQL Developer (or another IDE) it will output the columns:
CUSTOMER_NO TITLE GROUP_ID NAME PRICELIST_ID NAME1
and will rename the second NAME column to NAME1.
In the PL/SQL scope, it will not do this and will try to handle the two columns with the names you have given (i.e. the same names), fail and return ORA-00918.
You need to give one (or both) column an alias so they have distinct names.
New SQL page assigns your dublicate columns new temporary column names.
But stored procedures add your values a list matched column names.
Therefore, two columns have same names, it confuses which name should desired name.
Like bundle, your column name will be key to learn value and value will be value.
You should change one of them p.name or g.name or both of them.
select distinct c.customer_no, m.isim_unvan, c.group_id, g.name as groupName, c.pricelist_id, p.name as tarifeName from db.customer c
join db.pricelist p on c.pricelist_id = p.pricelist_id
join db.master m on c.customer_no = m.musteri_no
join db.group g on c.group_id = g.id
where (c.customer_no = pn_customer_no or pn_customer_no=-1)
and (c.group_id = pn_group_no or pn_group_no=-1)
and (c.pricelist_id = pn_pricelist_no or pn_pricelist_no=-1)
and (c.kom_type = ps_kom_tip)
order by c.customer_no asc

Hibernate HQL GroupBy in Oracle

I created this query using HQL with Hibernate and Oracle
select c from Cat c
left join c.kittens k
where (c.location= 1 OR c.location = 2)
and (i.activo = 1)
group
by c.id,
c.name,
c.fulldescription,
c.kittens
order by count(e) desc
The problem comes with the fact that in HQL you need to specify all fields in Cat in order to perform a Group By, but fulldescription is a CLOB, and you cannot group by by a CLOB (I get a "Not a Group By Expression" error. I've seen a few solutions around for a pure SQL sentence but none for HQL.
A serious issue GROUP BY of HQL because if you specify your object in GROUP BY and in your SELECT field list behaviours are differents. In GROUP BY has considered only id field but in SELECT field list all fields are considered.
So you can use a subquery with GROUP BY to return only id from your object, so that result becomes an input for the main query, like the follow I write for you.
Pay attention there are some alias table (i and e) not defined, so this query doesn't work, but you know as fixed.
Try this:
select c2 from Cat c2
where c2.id in (
select c.id from Cat c
left join c.kittens k
where (c.location= 1 OR c.location = 2)
and (i.activo = 1) <-- who is i alias??
group by c.id)
order by count(e) desc <-- who is e alias???

How do I write a LINQ query to combine multiple rows into one row?

I have one table, 'a', with id and timestamp. Another table, 'b', has N multiple rows referring to id, and each row has 'type', and "some other data".
I want a LINQ query to produce a single row with id, timestamp, and "some other data" x N. Like this:
1 | 4671 | 46.5 | 56.5
where 46.5 is from one row of 'b', and 56.5 is from another row; both with the same id.
I have a working query in SQLite, but I am new to LINQ. I dont know where to start - I don't think this is a JOIN at all.
SELECT
a.id as id,
a.seconds,
COALESCE(
(SELECT b.some_data FROM
b WHERE
b.id=a.id AND b.type=1), '') AS 'data_one',
COALESCE(
(SELECT b.some_data FROM
b WHERE
b.id=a.id AND b.type=2), '') AS 'data_two'
FROM a first
WHERE first.id=1
GROUP BY first.ID
you didn't mention if you are using Linq to sql or linq to entities. However following query should get u there
(from x in a
join y in b on x.id equals y.id
select new{x.id, x.seconds, y.some_data, y.type}).GroupBy(x=>new{x.id,x.seconds}).
Select(x=>new{
id = x.key.id,
seconds = x.Key.seconds,
data_one = x.Where(z=>z.type == 1).Select(g=>g.some_data).FirstOrDefault(),
data_two = x.Where(z=>z.type == 2).Select(g=>g.some_data).FirstOrDefault()
});
Obviously, you have to prefix your table names with datacontext or Objectcontext depending upon the underlying provider.
What you want to do is similar to pivoting, see Is it possible to Pivot data using LINQ?. The difference here is that you don't really need to aggregate (like a standard pivot), so you'll need to use Max or some similar method that can simulate selecting a single varchar field.

Resources