query xmltable oracle 10g - oracle

I have a query that used XML input to generate a XML table, I give that table an alias "XMLalias". How can I query this table in some other select statement, which is part of same batch.
I want to do something like " select * from XMLalias ".
I am new to oracle so please excuse if this is something really simple.
thanks.

im not sure what you need exactly as i figure what you want is one of this two:
select * from
(select * from XMLalias ) insider
where insider.col1 /*.....*/
Or you wanted someting like that
select *
from XMLalias a,
XMLalias b
where a.key_col=b.other_key_col
and a.col1 = /*...... */
and b.col2 = /*...... */

Related

Unexpected behavior with nested Oracle SELECT query

Would love to get some help here understanding an unexpected behavior with an Oracle SELECT query. And maybe there is a nicer way to construct my query.
Basically, this is the query that gets me the distinct set of IDs that I'm after:
select distinct(c.LOCATIONID) from COURSESESSION c where c.MARKETID=280 and c.STATUSID in(1, 2)
and c.COURSESESSIONID not in (SELECT distinct(REGISTRATION.COURSESESSIONID) FROM REGISTRATION)
But I also want all the data for these distinct IDs form COURSESESSION table, so I'm wrapping the above query in yet another select statement, so it looks like this:
select * from COURSESESSION cs where cs.COURSESESSIONID in
(select distinct(c.LOCATIONID) from COURSESESSION c where c.MARKETID=280 and c.STATUSID in(1, 2)
and c.COURSESESSIONID not in (SELECT distinct(REGISTRATION.COURSESESSIONID) FROM REGISTRATION));
The first query returns a set of 21 records, but the second set returns 19, so I'm pretty sure I'm not doing this right. The triple-select query is basically for these 2 steps:
Set idsSet = select distinct(c.LOCATIONID) from COURSESESSION c where c.MARKETID=280 and c.STATUSID in(1, 2)
and c.COURSESESSIONID not in (SELECT distinct(REGISTRATION.COURSESESSIONID) FROM REGISTRATION)
select * from COURSESESSION cs where cs.COURSESESSIONID in(idsSet);
Anyone knows how to fix my triple-select query?

Access union all into new table

I have a saved query (MyUnion) for a union, appending monthly files (linked views):
select * from RawTrade1801
union all
select * from RawTrade1802
Trying to write this to a new table is proving problematic:
SELECT MyUnion.* into RawTrade2
FROM MyUnion
WHERE Field8 = 'ZAR';
I get an error: Cannot open database "
My aim is to once a month create a master table by appending every monthly file.
The following steps should generate a table from your UNION query:
Create a new query. In the SQL View, now type:
SELECT * FROM (SELECT * FROM RawTrade1801
UNION ALL
SELECT * FROM RawTrade1802);
Save the query. In its Design View click the Make Table button. Type in the table name (e.g. NewTable) that you want the output of this query to be.
Save and close the query.
The query icon will have changed. Double click on it and it should generate your table.
Access actually changes the SQL to
SELECT * INTO NewTable FROM
(SELECT * FROM RawTrade1801
UNION ALL
SELECT * FROM RawTrade1802) AS [%$###_Alias];
when you view the SQL after step 4.
Try using INSERT INTO ... SELECT:
INSERT INTO RawTrade2
SELECT * FROM RawTrade1801 WHERE Field8 = 'ZAR'
UNION ALL
SELECT * FROM RawTrade1802 WHERE Field8 = 'ZAR';
Of course, even if this works it still does not explain why your original query does not work. I expect it is a slight technical problem, though I don't know enough Access to see it immediately.
Create the Union query, save it with the name like 'qUnion'.
From menu open:
Create-->Query--> Make Table.
In Add Table Menu chose "Queries" and
Add your union query ('qUnion').
Select the fields that you need.
Open query.
Anew table will be created.

Is there a Hive equivalent of SQL “LIKE ANY ( SUBQUERY )”

While Hive doesn't supports multi-value LIKE queries which are supported in SQL : ex.
SELECT * FROM user_table WHERE first_name LIKE ANY ( 'root~%' , 'user~%' );
We can convert it into equivalent HIVE queries as :
SELECT * FROM user_table WHERE first_name LIKE 'root~%' OR first_name LIKE 'user~%'
Does anyone know an equivalent solution that Hive does support in case sub-query is used with LIKE ? Have a look at below example :
SELECT * FROM user_table WHERE first_name LIKE ANY ( SELECT expr FROM exprTable);
As It doesn't have values in expression, I can't use same approach for generating multiple LIKE expression separated with OR / AND operator. Initially I thought to write HIVE UDF for it ? Can you please help me supporting such expression and finding HIVE equivalent ?
You can use Hive's RLIKE relational operator as shown below,
SELECT * FROM user_table WHERE first_name RLIKE 'root~|user~|admin~';
Hope this helps!
This is a case involving theta joins in Hive. There is a wiki page for this and a jira request. Please go through the details here on this page: https://cwiki.apache.org/confluence/display/Hive/Theta+Join
Your case is similar to the Side-Table Similarity case given on the page.
You need to convert the expr values into a map and then use regular expression to find the like. Alternatively you can also use union all with all the like expressions in separate SQL - the query might become tedious so you can programatically generate it.
What about this using EXISTS
SELECT * FROM user_table WHERE EXISTS ( SELECT * FROM exprTable WHERE first_name LIKE expr );

PostgreSQL - migrate a query with 'start with' and 'connect by' in oracle

I have the following query in oracle. I want to convert it to PostgreSQL form. Could someone help me out in this,
SELECT user_id, user_name, reports_to, position
FROM pr_operators
START WITH reports_to = 'dpercival'
CONNECT BY PRIOR user_id = reports_to;
A something like this should work for you (SQL Fiddle):
WITH RECURSIVE q AS (
SELECT po.user_id,po.user_name,po.reports_to,po.position
FROM pr_operators po
WHERE po.reports_to = 'dpercival'
UNION ALL
SELECT po.user_id,po.user_name,po.reports_to,po.position
FROM pr_operators po
JOIN q ON q.user_id=po.reports_to
)
SELECT * FROM q;
You can read more on recursive CTE's in the docs.
Note: your design looks strange -- reports_to contains string literals, yet it is being comapred with user_id which typicaly is of type integer.

Oracle: Order by Union returning ORA-00933: SQL command not properly ended

I have an issue with using Oracle's union and order by clauses together.
I have two complex queries (with sub queries in them) having an order by clause for each of them. I need to union the output of both and return the result. When I run it, I am getting the error ORA-00933: SQL command not properly ended.
But it works when I comment out the order by clauses in both of them.
To test this, I created a simple query as simple as shown below
select * from employee where employee_id=2 order by name
union
select * from employee where employee_id=3 order by name;
Even this gave the same error when ran with order by clauses but runs well when I commentout the order by clauses.
I tried searching forums, but I could not get solution for the exact problem. I found one at ORACLE Query with ORDER BY and UNION but As my queries are already too complecated because of subqueries and joins between too many tables, I dont want to implement this.
Can someone help me on fixing the root cause of the issue.
try this code:
select e1.name name /* e1.* */
from employee e1
where employee_id = 2
union
select
e2.name name /* e2.* */
from employee e2
where employee_id = 3
order by name;
if you want to order the result of first query then to order the result the second query so you can do like this:
select 1 query, e1.name name /* e1.* */
from employee e1
where employee_id = 2
union
select
2 query, e2.name name /* e2.* */
from employee e2
where employee_id = 3
order by query, name;
You can have only one ORDER BY when combining multiple queries, on the last statement. The ORDER BY clause acts on the entire set.
See the Oracle Documentation:
You cannot specify the order_by_clause in the subquery of these operators.
If you want order by in each query you must wrap it in other select as a subquery:
select * from (select * from employee where employee_id=2 order by name)
union
select * from (select * from employee where employee_id=3 order by name);

Resources