Stuck on CONNECT_BY ... PRIOR statement / sub-select Query
I am trying to generate a list of customisations made to our eBusiness system.
I'm using this example as a starting point:
SELECT *
FROM applsys.jdr_paths jp1
WHERE UPPER(jp1.path_name) = 'CUSTOMIZATIONS'
AND jp1.path_docid BETWEEN 18000 AND 18700;
_NAME PATH_DOCID PATH_OWNER_DOCID PATH_TYPE PATH_SEQ PATH_XML_V PATH_XML_E CREATED_BY CREATION_ LAST_UPDATED_BY LAST_UPDA LAST_UPDATE_LOGIN
customizations 18479 1650 PACKAGE -1 1 24-SEP-05 1 24-SEP-05 1
customizations 18665 18663 PACKAGE -1 1 24-SEP-05 1 24-SEP-05 1
customizations 18010 18009 PACKAGE -1 1 24-SEP-05 1 24-SEP-05 1I can then use this to extract the full path - for example, using the example above, the "PATH_OWNER_DOCID" = 18663:
SELECT LEVEL
, SYS_CONNECT_BY_PATH(p.path_name, '/') PATH
, p.path_docid
FROM (SELECT jp.path_name
, jp.path_seq
, jp.path_docid
, jp.path_owner_docid
FROM applsys.jdr_paths jp) p
CONNECT BY path_docid = PRIOR path_owner_docid
START WITH p.path_docid = 18665;
LEVEL PATH PATH_DOCID
1 /customizations 18665
2 /customizations/webui 18663
3 /customizations/webui/pagesetup 18661
4 /customizations/webui/pagesetup/printmgmt 18660
5 /customizations/webui/pagesetup/printmgmt/bpa 10174
6 /customizations/webui/pagesetup/printmgmt/bpa/ar 10173
7 /customizations/webui/pagesetup/printmgmt/bpa/ar/apps 2
8 /customizations/webui/pagesetup/printmgmt/bpa/ar/apps/oracle 1What I'd really like to do is to include the bottom most path (i.e. the most detailed, full path (path_docid = 1 in the e.g. above) in the first SQL statement.
That way, against each customisation, listed via this:
SELECT *
FROM applsys.jdr_paths jp1
WHERE UPPER(jp1.path_name) = 'CUSTOMIZATIONS'
AND jp1.path_docid BETWEEN 18000 AND 18700;I would like to include, possibly via a sub-select, the full path of the customisation - not all of the other lines leading up to the full path, just the full path itself.
But I can't work out how to combine the 2 SQLs, assuming it can be done at all.
Any advice much appreciated.
Thanks!
If understand your question right... Just a MAX would work.
select (SYS_CONNECT_BY_PATH(object_name, '/'))
from
select object_name, rownum lv
from all_objects
where rownum <= 5
) t
START WITH lv = 1
CONNECT BY PRIOR lv = lv - 1
(SYS_CONNECT_BY_PATH(OBJECT_NAME,'/'))
/WRH$_SERVICE_WAIT_CLASS_PK
/WRH$_SERVICE_WAIT_CLASS_PK/WRH$_SERVICE_WAIT_CLASS
/WRH$_SERVICE_WAIT_CLASS_PK/WRH$_SERVICE_WAIT_CLASS/WRH$_SYS_TIME_MODEL_PK
/WRH$_SERVICE_WAIT_CLASS_PK/WRH$_SERVICE_WAIT_CLASS/WRH$_SYS_TIME_MODEL_PK/WRH$_SYS_TIME_MODEL
/WRH$_SERVICE_WAIT_CLASS_PK/WRH$_SERVICE_WAIT_CLASS/WRH$_SYS_TIME_MODEL_PK/WRH$_SYS_TIME_MODEL/WRH$_OSSTAT_PK
5 rows selected.
select max(SYS_CONNECT_BY_PATH(object_name, '/'))
from
select object_name, rownum lv
from all_objects
where rownum <= 5
) t
START WITH lv = 1
CONNECT BY PRIOR lv = lv - 1
MAX(SYS_CONNECT_BY_PATH(OBJECT_NAME,'/'))
/WRH$_SERVICE_WAIT_CLASS_PK/WRH$_SERVICE_WAIT_CLASS/WRH$_SYS_TIME_MODEL_PK/WRH$_SYS_TIME_MODEL/WRH$_OSSTAT_PK
1 row selected.vr
Sudhakar B.
Similar Messages
-
I have to do a task of finding records which matches the following logic.
My table has PID varchar(15), Amount varchar (50),Status varchar(20) columns.
Each PID can go thru changes in a period of time, in my case i dont have a data range I just have a flatfile which consists of three types of PID`s
123456 = Original_PID, Reversal =123456R1,Adjustment= 123456A1.
Basically What I am trying to find out if there are any original PID`s which has a Reversal or an Adjustment.
here`s the code I have started on... Can someone help me get it finished?
select R1. pid,a1.pid,org.pid from (
select SUBSTRING(Pid,1,11)as Original_PID, status, paid_amount as Amount from dbo.Q2STATUS)org
inner join (select SUBSTRING(Pid,1,13)as Original_Icn,status,paid_amount from dbo.Q2STATUS
where SUBSTRING(Pid,12,2) ='R1')r1 on r1.pid=org.pid
inner join (select SUBSTRING(claim_id,1,13)as Original_Icn,status,paid_amount from dbo.Q2STATUS
where SUBSTRING(Pid,12,2) ='A1')A1 on a1.Pid=org.PID
FMHi,
Please check following item
It uses a
SQL string function, please create it first
create table Q2STATUS (Pid nvarchar(200), Status varchar(20), paid_amount int)
insert into Q2STATUS select '123456 = Original_PID, Reversal =123456R1, Adjustment= 123456A1',NULL,250
with cte2 as (
select rn = row_number() over (order by pid), * from Q2STATUS
), cte as (
select cte2.*, s.id typeid, v.*
from cte2
cross apply dbo.split(cte2.Pid, ',') s
cross apply dbo.split(s.val, '=') v
), cte3 as (
select
rn, pid, status, paid_amount, max(pidtype) pidtype, max(pidval) pidval
from (
select
rn, Pid, status, paid_amount, typeid,
case when (typeid = 1 and id = 2) or (typeid <> 1 and id = 1) then ltrim(rtrim(val)) else null end as pidtype,
case when (typeid = 1 and id = 1) or (typeid <> 1 and id = 2) then ltrim(rtrim(val)) else null end as pidval
from cte
) g
group by rn, pid, status, paid_amount, typeid
select
cte2.rn, cte2.pid,
cte3.pidval as 'Original_PID',
cte4.pidval as 'Reversal',
cte5.pidval as 'Adjustment'
from cte2
left join cte3 on cte3.rn = cte2.rn and pidtype = 'Original_PID'
left join cte3 as cte4 on cte4.rn = cte2.rn and cte4.pidtype = 'Reversal'
left join cte3 as cte5 on cte5.rn = cte2.rn and cte5.pidtype = 'Adjustment'
Here is the result,
Please note that I have used
SQL CTE expressions a lot in the SQL query to prevent usage of sub-select and temp tables.
There is also a
SQL ROW_NUMBER() function used in the first SELECT statement to distinquish all items from each other instead of using the PID column value. Perhaps you have already an ID key column, you can use it too
SQL Server, SQL Server 2012 Denali and T-SQL Tutorials -
Dynamic From statement in select query and/or outer join not working
Dear Experts, I have a select query where the select columns are dynamic, the where condition is also dynamic. It is of the below format:
Select (dynamic columns) INTO <wa>
FROM a inner join b on af1 = bf1
inner join c on af2 = cf2......
WHERE (dynamic conditios)
ORDER BY ( dynamic sort condition).
Now I have to include some tables (dynamically depending on the user input) in the inner join statement which will give description for the selected fields. And these database tables may or may no be empty. So in this case, my select query will not return any data if these tables are empty. And I dont want that.
I tried using outer join for the extra tables but it gave me a runtime error. I also tried forming the inner join statement dynamically but it was not supporting.
Kindly give me pointers.
ThanksHey thanks for the reply, but the problem is not solved.
I am already using ( fileds, value) like table in my where condition and the select statement was working properly.
the problem is that now I have to include some tables in the join statement which can be empty and so i want to use Outer join.
But I am getting a runtime error as below:
Error analysis
An exception occurred that is explained in detail below.
The exception, which is assigned to class 'CX_SY_DYNAMIC_OSQL_SYNTAX', was not
caught in
procedure "ZATSCSNG_RFC_READ_TABLE" "(FUNCTION)", nor was it propagated by a
RAISING clause.
Since the caller of the procedure could not have anticipated that the
exception would occur, the current program is terminated.
The reason for the exception is:
The running ABAP program wanted to execute a SELECT statement whose
WHERE condition was (partly) specified dynamically. The part that is
specified in an internal table at runtime is compared to a field of the
right table of an LEFT OUTER JOIN. Such comparisons are not supported by
all database systems and are therefore not allowed. -
Strange results with Insert statement having select query
Hi all,
I am facing a strange issue with Insert statement based on a select query having multiple joins.
DB- Oracle 10g
Following is the layout of my query -
Insert into Table X
Select distinct Col1, Col2, Col3, Col4, Function(Col 5) from Table A, B
where trunc(updated_date) > = trunc(sysdate-3)
and join conditions for A, B
Union
Select Col1, Col2, Col3, Col4, Function(Col 5) from Table C, D
trunc(updated_date) > = trunc(sysdate-3)
and join conditions for C, D
Union
.... till 4 unions. all tables are residing in the local Database and not having records more than 50,000.
If I execute above insert in a DBMS job, it results into suppose 50 records where as if I execute the select query it gives 56 records.
We observed following things-
a) no issue with size of tablespace
b) no error while inserting
c) since query takes lot of time so we have not used Cursor and PLSQL block for inserting.
d) this discrepancy in number of records happens frequently but not everytime.
e) examined the records left out from the insert, there we couldn't find any specific pattern.
f) there is no constraint on the table X in which we are trying to insert. Also tables A, B, C....
I went through this thread -SQL insert with select statement having strange results but mainly users are having either DB Links or comparison of literal dates, in my case there is none.
Can somebody explain why is the discrepancy and what is the solution for it.
Or atleast some pointers how to proceed with the analysis.
Edited by: Pramod Verma on Mar 5, 2013 4:59 AM
Updated query and added more details>
Since I am using Trunc() in the where clause so timing should not matter much. Also I manually ruled out records which were updated after the job run.
>
The first rule of troubleshooting is to not let your personal opinion get in the way of finding out what is wrong.
Actually this code, and the process it represents, is the most likely CAUSE of the problem.
>
where trunc(updated_date) > = trunc(sysdate-3)
>
You CANNOT reliably use columns like UPDATED_DATE to select records for processing. Your process is flawed.
The value of that column is NOT the date/time that the data was actually committed; it is the date/time that the row was populated.
If you insert a row into a table right now, using SYSDATE (8am on 3/5/2013) and don't commit that row until April your process will NEVER see that 3/5/2013 date until April.
Here is the more typical scenario that I see all the time.
1. Data is inserted/updated all day long on 3/4/2013.
2. A column, for example UPDATED_DATE is given a value of SYSDATE (3/4/2013) in a query or by a trigger on the table.
3. The insert/update query takes place at 11:55 PM - so the SYSDATE values are for THE DAY THE QUERY BEGAN
4. The data pull begins at 12:05 am (on 3/5/2013 - just after midnight)
5. The transaction is COMMITTED at 12:10 AM (on 3/5/2013); 5 minutes after the data pull began.
That data extract in step 4 will NEVER see those records! They DO NOT EXIST when the data pull query is executed since they haven't been committed.
Even worse, the next nights data pull will not see them either! That is because the next pull will pull data for 3/5/2013 but those records have a date of 3/4/2013. They will never get processed.
>
Job timing is 4am and 10pm EST
>
Another wrinkle is when data is inserted/updated from different timezones and the UPDATED_DATE value is from the CLIENT pc or server. Then you can get even more data missed since the client dates may be hours different than the server date used for the data pull process.
DO NOT try to use UPDATED_DATE type columns to do delta extraction or you can have this issue. -
Hello:
I need to write a query which identifies the patients who exist in the master table for a given organization and trait and do not exist in the final table.
For example, for organization 499 and trait 43926, I have a total of 119 patients. After some processing, out of these 119 patients, only 106 patients get inserted into the final_traits table.
This leaves us with a difference of 13 patients. I want a query that will give me this list of 13 patients.
In order to achieve this, I need to use three tables:
1. Master_traits which contains the entire patient population. The field needed from this table is dwkey_patient.
2. Patient which contains the demographic info. The three fields needed from this table are dwkey_patient, patient_key, and name.
3. Final_traits which contains the detail trait info. The field needed from here is patient_key.
To retrieve the 119 patients along with their names, the first join needs to be between “master_traits” and “patient” using the dwkey_patient field.
From these 119 patients, to determine which ones did NOT get inserted into the final_traits table, the second join will be between “patient” and “final_traits” using the patient_key field. This should give me 13 patients.
So far, I have the portion of the query which gives me the total population of 119.
select a.dwkey_patient, b.patient_key, b.name
from master_traits a, patient b
where a.dwkey_patient = b.dwkey_patient
and a.dwkey_org = 499
and a.dwkey_approval > 0
and a.dwkey_traittype = 43926
and a.out_of_range is null
order by dwkey_patient asc;
[/code]
I am having a tough time getting the second portion of the query to show me the 13 patients who were not inserted.
Can someone help?
Thanks.Well,
It works for me...
MHO%xe> with master_traits as
2 ( select 1234 dwkey_patient from dual union all
3 select 2345 from dual union all
4 select 3456 from dual union all
5 select 4567 from dual union all
6 select 5678 from dual
7 )
8 , patient as
9 ( select 1234 dwkey_patient, 900 patient_key, 'smith' name from dual union all
10 select 2345, 901, 'jones' name from dual union all
11 select 3456, 902, 'murphy' name from dual union all
12 select 4567, 903, 'brown' name from dual union all
13 select 5678, 904, 'miller' name from dual
14 )
15 , final_traits as
16 ( select 900 patient_key from dual union all
17 select 901 from dual union all
18 select 903 from dual
19 ) -- actual query starts here:
20 select a.dwkey_patient, b.patient_key, b.name
21 from master_traits a
22 , patient b
23 , final_traits c
24 where a.dwkey_patient = b.dwkey_patient
25 -- and a.dwkey_org = 499
26 -- and a.dwkey_approval > 0
27 -- and a.dwkey_traittype = 43926
28 -- and a.out_of_range is null
29 and b.patient_key = c.patient_key(+)
30 and c.patient_key is null
31 ;
DWKEY_PATIENT PATIENT_KEY NAME
5678 904 miller
3456 902 murphySo, you might need to adjust your WHERE clause I commented out in above example...
MHO%xe> with master_traits as
2 ( select 1234 dwkey_patient from dual union all
3 select 2345 from dual union all
4 select 3456 from dual union all
5 select 4567 from dual union all
6 select 5678 from dual
7 )
8 , patient as
9 ( select 1234 dwkey_patient, 900 patient_key, 'smith' name from dual union all
10 select 2345, 901, 'jones' name from dual union all
11 select 3456, 902, 'murphy' name from dual union all
12 select 4567, 903, 'brown' name from dual union all
13 select 5678, 904, 'miller' name from dual
14 )
15 , final_traits as
16 ( select 900 patient_key from dual union all
17 select 901 from dual union all
18 select 903 from dual
19 )
20 select a.dwkey_patient, b.patient_key, b.name
21 from master_traits a, patient b
22 where a.dwkey_patient = b.dwkey_patient
23 -- and a.dwkey_org = 499
24 -- and a.dwkey_approval > 0
25 -- and a.dwkey_traittype = 43926
26 -- and a.out_of_range is null
27 and not exists ( select null
28 from final_traits c
29 where c.patient_key = b.patient_key )
30 ;
DWKEY_PATIENT PATIENT_KEY NAME
5678 904 miller
3456 902 murphyWorks too (ofcourse)
Edited by: hoek on Jun 4, 2009 8:37 PM -
Oracle Version 9.2.0.7.0
Sorry to ask what is probably a stupid question that only serves to highlight how useless my SQL is!
I have this SQL which counts total requisition lines, and then counts how many of those lines have converted to Sales Orders:
SELECT prha.segment1
, (SELECT COUNT(*)
FROM po.po_requisition_lines_all prla
WHERE prla.requisition_header_id = prha.requisition_header_id)
req_line_ct
, (SELECT COUNT(*)
FROM po.po_requisition_lines_all prla
, ont.oe_order_lines_all sol
WHERE prla.requisition_line_id = sol.source_document_line_id
AND prla.requisition_header_id = prha.requisition_header_id)
so_line_ct
FROM po.po_requisition_headers_all prha
WHERE prha.creation_date >= '03-JUL-2008';The SQL works, but it is really, really slow. Is that just because it is running through lots and lots of data?
If I comment out the 2nd Count SELECT, it runs immediately. Perhaps it is so slow because the join between the po_requisition_lines_all and the oe_order_lines_all table is not via primary keys. Also, there is not an index on the oe_order_lines_all table related to the source_document_line_id field.
I'm sorry I haven't provided the full table designs for the tables involved - but the primary keys joining the requisition tables = via the requisition_headers_all table, and the join via the po_requisition_lines_all and the oe_order_lines_all table is not via a primary key or an index.
Sorry once again, for probably talking nonsense.Please note this is a untested code as you didn't provide any of the required information like
a) Test Data
b) Relationship between the tables
You could try something like this. But as already @someoneelse suggested it is definitely worth reading the link.
select prha.segment1, count(case when prla.requisition_header_id is not null then 1 end) req_line_ct,
count(case when sol.source_document_line_id is not null then 1 end) so_line_ct
from
po.po_requisition_header_all prha,
po.po_requisition_lines_all prla,
ont.oe_order_lines_all sol
where
prha.creation_date >= to_date('03.07.2008','dd.mm.yyyy')
and
prha.requisition_header_id = prla.requisition_header_id (+)
and
prla.requsition_line_id = sol.source_document_line_id (+)
group by prha.segment1;Regards
Raj -
Sub-Select SQL query in Oracle BI Answers
Hi
What's the proper usage of Sub-Select SQL query in Oracle BI Answers Version Oracle Business Intelligence 10.1.3.2.1?
I get [SQL_STATE: HY000] [nQSError: 10058] A general error has occured when trying to Sub Select query like:
itemno = (SELECT MIN(orders.itemno) FROM mydatabase where rownum < 2 order by orders.itemno)Maybe the best is to create a new physical and logical object for your sub-select and join this with your current objects.
-
Can we write an alter statement in Select Query?
Dynamic sql
SQL> create table t_al (al number(10));
Table created.
SQL>
DECLARE
v_al VARCHAR2 (200);
BEGIN
select 'alter table t_al modify al varchar2(20)' into v_al from dual;
execute immediate v_al;
end;
PL/SQL procedure successfully completed.
SQL> desc t_al;
AL VARCHAR2(20 CHAR) -
I can actually pass multiple values in the IN statement of select query using multiple dictionary fields in the data retrieval rule of the AFC in Cisco cloud portal like
#dictionary.field1# = 1 and
#dictionary.field2#=2
select col1,col2 from table1 where col3 in (#dictionary.field1#,dictionary.field2#).
but I want to pass mutiple values in a single field as
#dictionary.field1#=1,2
select col1,col2 from table1 where col3 in (#dictionary.field1#) and the query gives no data because it is taking as '1,2' instead of '1','2'.
Please give the solution for passing multiple values in a single variable to use in IN operator of WHERE clauseOk, I now understand what you are trying to do. Unfortunately, you cannot inject parts of a SQL statement into a DDR through a dictionary field, which always represents a specific value (the comma in your case is attempting injection of a SQL construct to refer to multiple values). One possible solution is to arbitrarily consolidate your list of values using a delimiter that you know will not be in the values themselves such as a colon (:). Let's use 3 values as it serves as a better example.
Set your dictionary field to a single reference to all 3 values of interest, say 'a', 'b', 'c' as:
:a:b:c: (you can use javascript to create this consolidated dictionary field)
Now your query would look something like the following:
select col1,col2 from table1 where #dictionary.field1t# like '%:'+col3+':%'
This should achieve the desired result. -
Comparing dates in select query.
Hi Experts,
I need to select data from one table such that the date field of the table should be either today's date or earlier date.
How can I write such comparison statement in select query.hi,
select * from <your_table>
where <date_field> <= sy-datum.
endselect .
regards,
burak -
Hi,
I have 2 reports. The first report contains YYYY, Q, avg(val1).
2007 Q1 5
2007 Q2 6
2007 Q2 7
Cumul 6
Besides I want to create a report (gauge) containing the cumul value.
Therefore it seems that I need to create a SUB select query like this
select year, avg(avg(val1))
from (select YYYY, avg(val1) over (partition by YYYY, Q) from table)
group by YYYY
Any one an idea how I can fix this in answers??
Txs for your helpdid you try ∑ option in answers?
-
Using case when statement in the select query to create physical table
Hello,
I have a requirement where in I have to execute a case when statement with a session variable while creating a physical table using a select query. let me explain with an example.
I have a physical table based on a select table with one column.
SELECT 'VALUEOF(NQ_SESSION.NAME_PARAMETER)' AS NAME_PARAMETER FROM DUAL. Let me call this table as the NAME_PARAMETER table.
I also have a customer table.
In my dashboard that has two pages, Page 1 contains a table with the customer table with column navigation to my second dashboard page.
In my second dashboard page I created a dashboard report based on NAME_PARAMETER table and a prompt based on customer table that sets the NAME_ PARAMETER request variable.
EXECUTION
When i click on a particular customer, the prompt sets the variable NAME_PARAMETER and the NAME_PARAMETER table shows the appropriate customer.
everything works as expected. YE!!
Now i created another table called NAME_PARAMETER1 with a little modification to the earlier table. the query is as follows.
SELECT CASE WHEN 'VALUEOF(NQ_SESSION.NAME_PARAMETER)'='Customer 1' THEN 'TEST_MART1' ELSE TEST_MART2' END AS NAME_PARAMETER
FROM DUAL
Now I pull in this table into the second dashboard page along with the NAME_PARAMETER table report.
surprisingly, NAME_PARAMETER table report executes as is, but the other report based on the NAME_PARAMETER1 table fails with the following error.
Error Codes: OPR4ONWY:U9IM8TAC:OI2DL65P
State: HY000. Code: 10058. [NQODBC] [SQL_STATE: HY000] [nQSError: 10058] A general error has occurred. [nQSError: 16001] ODBC error state: S1000 code: 1756 message: [Oracle][ODBC][Ora]ORA-01756: quoted string not properly terminated. [nQSError: 16014] SQL statement preparation failed. (HY000)
SQL Issued: SET VARIABLE NAME_PARAMETER='Novartis';SELECT NAME_PARAMETER.NAME_PARAMETER saw_0 FROM POC_ONE_DOT_TWO ORDER BY saw_0
If anyone has any explanation to this error and how we can achieve the same, please help.
Thanks.Hello,
Updates :) sorry.. the error was a stupid one.. I resolved and I got stuck at my next step.
I am creating a physical table using a select query. But I am trying to obtain the name of the table dynamically.
Here is what I am trying to do. the select query of the physical table is as follows.
SELECT CUSTOMER_ID AS CUSTOMER_ID, CUSTOMER_NAME AS CUSTOMER_NAME FROM 'VALUEOF(NQ_SESSION.SCHEMA_NAME)'.CUSTOMER.
The idea behind this is to obtain the data from the same table from different schemas dynamically based on what a session variable. Please let me know if there is a way to achieve this, if not please let me know if this can be achieved in any other method in OBIEE.
Thanks. -
Sub selects in a SQL statement
Hi, I have a query with an obsene amount of sub selects,
for example :
SELECT p.ID,
(select something from data where id = p.id) data1,
(select somelse from data2 where id = p.id) data2,
(select someelse from data3 where id = p.id) data3,
(select someelse from data4 where id = p.id) data4,
(select someelse from data5 where id = p.id) data5,
FROM property P
WHERE ..........
this query is taking a long time to process. Is there a more efficient way of doing such a statement?
thanks in advanceGabe:
Since the original poster said "this query is taking a long time to process", I assumed that there was a one-to-one relationship between properties and each of the dataX tables, otherwise, as you pointed out, he would be complaining about ORA-01427: single-row subquery returns more than one row
In my union, I am returning a single column from each of the data tables (plus the id), and a NULL for each of the other columns in the in-line view. The MAX function just gets the single row for each column that actually has a value.
As a test case, I did:
CREATE TABLE properties AS
SELECT rownum id, object_name
FROM all_objects
WHERE rownum < 11;
CREATE TABLE data1 (id NUMBER, descr VARCHAR2(10));
INSERT INTO data1 VALUES(1, 'ONE');
INSERT INTO data1 VALUES(2, 'TWO');
INSERT INTO data1 VALUES(3, 'THREE');
CREATE TABLE data2 (id NUMBER, descr VARCHAR2(10));
INSERT INTO data2 VALUES(4, 'FOUR');
INSERT INTO data2 VALUES(5, 'FIVE');
INSERT INTO data2 VALUES(6, 'SIX');
CREATE TABLE data3 (id NUMBER, descr VARCHAR2(10));
INSERT INTO data3 VALUES(7, 'SEVEN');
INSERT INTO data3 VALUES(8, 'EIGHT');
INSERT INTO data3 VALUES(9, 'NINE');The original version as posted retrieves:
SQL> SELECT p.id, p.object_name,
2 (SELECT descr FROM data1 WHERE id = p.id) data1,
3 (SELECT descr FROM data2 WHERE id = p.id) data2,
4 (SELECT descr FROM data3 WHERE id = p.id) data3
5 FROM properties p
6 ORDER BY p.id;
ID OBJECT_NAME DATA1 DATA2 DATA3
1 TAB$ ONE
2 I_IND1 TWO
3 I_COBJ# THREE
4 USER$ FOUR
5 I_OBJ1 FIVE
6 I_PROXY_ROLE_DATA$_2 SIX
7 BOOTSTRAP$ SEVEN
8 I_ICOL1 EIGHT
9 UNDO$ NINE
10 PROXY_ROLE_DATA$
10 rows selected.
Statistics
0 recursive calls
0 db block gets
93 consistent gets
0 physical reads
0 redo size
1000 bytes sent via SQL*Net to client
655 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
1 sorts (memory)
0 sorts (disk)
10 rows processedBut, it does a SELECT (hopefully indexed) of data1, data2, and data3 for each row in properties. Minimally, 9 gets per row of properties (index root, index leaf, table row for each of data1, data2, data3) without even getting any data from properties.
My query returns:
SQL> SELECT p.id, p.object_name, a.data1, a.data2, a.data3
2 FROM properties p,
3 (SELECT id, MAX(data1) data1, MAX(data2) data2, MAX(data3) data3
4 FROM (SELECT id, descr data1, TO_CHAR(NULL) data2, TO_CHAR(NULL) data3
5 FROM data1
6 UNION ALL
7 SELECT id, TO_CHAR(NULL), descr, TO_CHAR(NULL)
8 FROM data2
9 UNION ALL
10 SELECT id, TO_CHAR(NULL), TO_CHAR(NULL), descr
11 FROM data3)
12 GROUP BY id) a
13 WHERE p.id = a.id(+)
14 ORDER BY p.id;
ID OBJECT_NAME DATA1 DATA2 DATA3
1 TAB$ ONE
2 I_IND1 TWO
3 I_COBJ# THREE
4 USER$ FOUR
5 I_OBJ1 FIVE
6 I_PROXY_ROLE_DATA$_2 SIX
7 BOOTSTRAP$ SEVEN
8 I_ICOL1 EIGHT
9 UNDO$ NINE
10 PROXY_ROLE_DATA$
10 rows selected.
Statistics
0 recursive calls
0 db block gets
12 consistent gets
0 physical reads
0 redo size
1000 bytes sent via SQL*Net to client
655 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
2 sorts (memory)
0 sorts (disk)
10 rows processedThe same result, but 1/8 of the gets. an outer join is also possible:
SQL> SELECT p.id, p.object_name, d1.descr data1, d2.descr data2, d3.descr data3
2 FROM properties p, data1 d1, data2 d2, data3 d3
3 WHERE p.id = d1.id(+) AND
4 p.id = d2.id(+) AND
5 p.id = d3.id(+)
6 ORDER BY p.id;
ID OBJECT_NAME DATA1 DATA2 DATA3
1 TAB$ ONE
2 I_IND1 TWO
3 I_COBJ# THREE
4 USER$ FOUR
5 I_OBJ1 FIVE
6 I_PROXY_ROLE_DATA$_2 SIX
7 BOOTSTRAP$ SEVEN
8 I_ICOL1 EIGHT
9 UNDO$ NINE
10 PROXY_ROLE_DATA$
10 rows selected.
Statistics
0 recursive calls
0 db block gets
12 consistent gets
0 physical reads
0 redo size
1000 bytes sent via SQL*Net to client
655 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
1 sorts (memory)
0 sorts (disk)
10 rows processedSo, the same number of gets, and it saves one sort. But if the tables are large, multiple outer joins may not be as efficient as one outer join to the union all query.
HTH
John -
Please suggest a select query / sub query with out using any subprograms or
source table: Three columns ORIGIN, DESTINATION,MILES
Origin Destination Miles
Sydney Melbourne 1000
Perth Adelaide 3000
Canberra Melbounre 700
Melbourne Sydney 1000
Brisbane Sydney 1000
Perth Darwin 4000
Sydney Brisbane 1000
out put :Three columns ORIGIN, DESTINATION,MILES
Duplicate routes are to be ignored so the output is
Origin Destination Miles
Sydney Melbourne 1000
Perth Adelaide 3000
Canberra Melbounre 700
Brisbane Sydney 1000
Perth Darwin 4000
Please suggest a select query / sub query with out using any subprograms or functions/pkgs to get the out put table.Hi,
user9368047 wrote:
... Please suggest a select query / sub query with out using any subprograms or functions/pkgs to get the out put table.Why? If the most efficient way to get the results you want involves using a function, why wouldn't you use it?
Here's one way, without any functions:
SELECT a.*
FROM source_table a
LEFT OUTER JOIN source_table b ON a.origin = b.destination
AND a.destination = b.origin
AND a.miles = b.miles
WHERE b.origin > a.origin -- Not b.origin > b.origin
OR b.origin IS NULL
;If you'd care to post CREATE TABLE and INSERT statements for your sample data, then I could test this.
Edited by: Frank Kulash on Nov 6, 2012 7:39 PM
Corrected WHERE clause after MLVrown (below) -
Sub-Select Count query breaking TOAD
Oracle 10.2.0.4.0
Running TOAD 9.1
I am running some SQL on our eBusiness Suite:
SELECT pha.segment1
, pha.type_lookup_code
, (SELECT COUNT(DISTINCT pha2.po_header_id)
FROM po.po_headers_all pha2
, po.po_lines_all pla
WHERE pha2.po_header_id = pla.po_header_id
AND pla.contract_id = pha.po_header_id) po_count
, (SELECT MAX(pha2.creation_date)
FROM po.po_headers_all pha2
, po.po_lines_all pla
WHERE pha2.po_header_id = pla.po_header_id
AND pla.contract_id = pha.po_header_id) latest_cpa_po
FROM po.po_headers_all pha
, po.po_vendors pv
, po.po_vendor_sites_all pvsa
WHERE pha.vendor_id = pv.vendor_id
AND pha.vendor_site_id = pvsa.vendor_site_id
-- AND pv.VENDOR_NAME LIKE 'H%'
AND pha.vendor_id = 98
AND pha.type_lookup_code = 'CONTRACT'
AND pha.org_id IN(7041, 7042);The above query runs quicky (approx. 1 second). If I take out the AND pha.vendor_id = 98 then the query takes a few minutes to run.
When I try to export it, or scroll down to view > 500 rows, TOAD crashes.
I know this isn't a TOAD forum, but I think that this is probably an issue with my no doubt rubbish SQL.
If I take out this sub-select, then the problem doesn't happen:
, (SELECT COUNT(DISTINCT pha2.po_header_id)
FROM po.po_headers_all pha2
, po.po_lines_all pla
WHERE pha2.po_header_id = pla.po_header_id
AND pla.contract_id = pha.po_header_id) po_countHowever, I can't work out a better way of getting the data I need.
The sub-select counts POs which have been raised where the contractID on the PO line is the same as the PO Header ID from the main query.
Any advice please, on what I could do to sort this out would be much appreciated.
Thanks!Hi,
It looks like you can replace both scalar sub-queries with a join, like this:
WITH header_lines_summary AS
SELECT pla.contract_id
, COUNT (DISTINCT pha2.po_header_id) AS po_count
, MAX (pha2.creation_date) AS latest_cpa_po
FROM po.po_headers_all pha2
, po.po_lines_all pla
WHERE pha2.po_header_id = pla.po_header_id
GROUP BY pla.contract_id
) -- Everything up to this line is new
SELECT pha.segment1
, pha.type_lookup_code
, hls.po_count -- Changed
, hls.latest_cpa_po -- Changed
FROM po.po_headers_all pha
, po.po_vendors pv
, po.po_vendor_sites_all pvsa
, header_lines_summary hls -- New
WHERE pha.vendor_id = pv.vendor_id
AND pha.vendor_site_id = pvsa.vendor_site_id
AND pha.po_header_id = hls.contract_id (+) -- New
-- AND pv.VENDOR_NAME LIKE 'H%'
AND pha.vendor_id = 98
AND pha.type_lookup_code = 'CONTRACT'
AND pha.org_id IN (7041, 7042);Aside from the sub-query (which is entirely new), the query above is just what you posted, with 2 lines changed and 2 lines added, as marked.
This should be more efficient, but I don't know for certain that it will solve the Toad problem.
I hope this answers your question.
If not, post a little sample data (CREATE TABLE and INSERT statements, relevant columns only) for all tables, and also post the results you want from that data.
It never hurts to say what version of Oracle you're using.
Maybe you are looking for
-
Last week, bought mid-2014 base model rMPB 13.3. Display was warm yellowish with greenish tint. Compared with store models, which all had cool blue. Replacement had same issue, so I asked for refund. Display looked worse than my late 2010 base model
-
Hello PP Experts, We hv defined the routings in the old plants, Now we hv changed the plant . Can we do the mass conversion of these routings for new plant? Any transaction code? Thanks in advance. Subhash
-
I have donwloaded a smartform to my system and how to proceed further...
Hi friends, I have copied a smartform to my system [ BC470_FLOWD_FINAL ]. And then if i execute it, it is not showing proper output. And also it is not taking any input from the user. In the sense we have to mannually write a program and p
-
What is the north american phone number for retu
what do i do to return my mp3 player for a fix for the head phone jack (cheap soddering!). i have the reciept within a year so what do i do whats an adress i looked at the site and it just seems to circle around everything this site is not very user
-
Wo finde ich den support auf deutsch? englisch nervt
wo finde ich den support auf deutsch? englisch nervt ich kann keine fotos mehr machen, da angeblich der speicher voll ist. was mach ich da?