Recursive joins / how to define query using connect by clause
Hi,
I have a table A have 1-1 reltionship with table Employee
Structure of table is as follows
Table A
id name employee_id
100 aa 1
200 bb 2
300 cc 3
400 dd 4
500 ee 5
Table Employee
id parent_id
1 null
2 1
3 2
4 3
5 1
6 1
I want to get all records from A table whose employee_id is equal to 2 or (recursive)decendants/child of employee with id 2 (i.e. employee with id 2,3,4 )
i.e I want recursively join where in I get records from table A as
id name employee_id
200 bb 2
300 cc 3 (because it is child of employee with id 2)
400 dd 4 (becaue it is child of employee with id 3 which is child of 2)
I know we can use In clause , but it will be performance wise not good.
Can we do it in Toplink without using IN clause?
Oracle has connect by clause, but other database might not have this caluse. So how can write a toplink query which can run on any database.
Any help is highly appreciated.
Thanks a lot.
You can use TopLink's Hierarchical Query support but this only works with Oracle DB as it relies on the database to perform the query.
--Shaun
Similar Messages
-
SQL query using connect by clause
Hi,
I have table:
SQL> col id for 999
SQL> col code for a30
SQL> select id,code from t1;
ID CODE
704 ,2,3,7,8,
707 ,15,12,17,18,
742 ,23,25,27,28,
5 ,53,65,67,58,
4 rows selected.---
When I run query:
select id,
substr(code,instr(code,chr(44),1,level)+1,instr(code,chr(44),1,level+1)-instr(code,chr(44),1,level)-1) code
from(
select id,
code,
length(code) - length(replace(code,',',''))-1 len
from t1
where id=5)
connect by level <= len;
ID CODE
5 53
5 65
5 67
5 58
4 rows selected.How to modify my query to get this output:
ID CODE
5 53
5 65
5 67
5 58
704 2
704 3
704 7
704 8
707 15
707 12
707 17
707 18
742 23
742 25
742 27
742 28What is the best way to get this output?
My db:
SQL> select * from v$version;
BANNER
Oracle9i Enterprise Edition Release 9.2.0.6.0 - 64bit Production
PL/SQL Release 9.2.0.6.0 - Production
CORE 9.2.0.6.0 Production
TNS for Solaris: Version 9.2.0.6.0 - Production
NLSRTL Version 9.2.0.6.0 - Production
5 rows selected.Thanks for answers!
Regards,
MarkoHi,
Generate a separate "counter table" that has the numbers 1, 2, 3, ... , x, where x is the greatest number you'll ever need.
Join to this table, using a join condition that gets the exact numbers you need for each row.
Use the number from the counter table where you are currently using LEVEL.
That is:
WITH cntr AS
SELECT LEVEL AS n
FROM dual
CONNECT BY LEVEL <= ( SELECT MAX ( LENGTH (code)
- LENGTH (REPLACE (code, ','))
FROM t1
) - 1
select id,
substr(code,instr(code,chr(44),1,n)+1,instr(code,chr(44),1,n+1)-instr(code,chr(44),1,n)-1) code
FROM t1
JOIN cntr ON cntr.n <= LENGTH (code) - LENGTH (REPLACE (code, ','))
;If you're using Oracle 10 (or higher) regular expressions would simplify this a lot. -
How to Define and Use Formatted Search in Sap Business One 2007
Hi
Any Document on How to Define and Use
Formatted Search in Sap Business One 2007.
ThanksHi Manish,
It is Very simple Procedure of defining FMS(Formatted Search)
in SAP 2007B Same as SAP 2005B & SAP8.8 Version
Do this,
->> Open the require Document.
for ex. IF you add the UDF in Marketing Document(Sales/Purchase)
1. Goto the UDF (or) any Default(Original) Field and Click ShiftAltF2.
2. Select the SEARCH BY SAVED QUERY.
3. Assign the FMS Query.
4. Select the AUTO REFRESH WHEN FIELD CHENGES.
5. Select the require Auto refresh field
6. Check the Refresh Regularly (or) Display Saved Values.
Regards,
Madhan. -
How to define and use cookies so that same login is used on all application
Hi
I have 3 apps in a single workspace and all of them SSO enabled
However when I go from one apps to other, it ask for login again
I have read in the forum that we can use cookies so that we use the single login on all apps within the same workspace
But I'm not sure how to define and use cookies.Please assistSee this presentation:
http://www.sumneva.com/apex/f?p=15000:395:0::NO::P395_PRESENTATION_KEY:MANY_TO_ONE -
APEX 'crawling' whilst executing a query using CONNECT BY
Hi Folks.
Anyone seen this behaviour before?
I can run this SQL (with the appropriate page item values substitued) in PL/SQL developer and I get the result in less than 0.094 seconds with a COST of 13
SELECT NULL LINK
,a.data_points
,5000 forecast_value
FROM
(SELECT to_char(trunc(add_months((SELECT iag.agreement_start_date
FROM iot_agreement iag
WHERE iag.iot_agreement_id =
:P317_IOT_REBATE_AGREEMENT_ID)
,LEVEL - 1)
,'mm')
,'MON-RR') data_points
FROM dual
CONNECT BY LEVEL <=
trunc(months_between((SELECT iag.agreement_end_date
FROM iot_agreement iag
WHERE iag.iot_agreement_id =
:P317_IOT_REBATE_AGREEMENT_ID)
,(SELECT iag.agreement_start_date
FROM iot_agreement iag
WHERE iag.iot_agreement_id =
:P317_IOT_REBATE_AGREEMENT_ID)))
MINUS
SELECT pkg_ngc_utilities.get_long_traffic_month(imps.traffic_period_id)
FROM iot_mfs_plus_summary imps
WHERE imps.client_master_entity_id = :P317_CLIENT_MASTER_ENTITY_ID
AND imps.home_master_entity_id IN
(SELECT ipy.master_entity_id
FROM iot_party ipy
WHERE ipy.iot_agreement_id = :P317_IOT_REBATE_AGREEMENT_ID
AND ipy.client_side = 0) -- Roaming Partner
AND imps.visited_master_entity_id = imps.client_master_entity_id
AND pkg_ngc_utilities.get_long_traffic_month(imps.traffic_period_id) IN
(SELECT to_char(trunc(add_months((SELECT iag.agreement_start_date
FROM iot_agreement iag
WHERE iag.iot_agreement_id =
:P317_IOT_REBATE_AGREEMENT_ID)
,LEVEL - 1)
,'mm')
,'MON-RR') data_points
FROM dual
CONNECT BY LEVEL <= trunc(months_between ((SELECT iag.agreement_end_date
FROM iot_agreement iag
WHERE iag.iot_agreement_id =
:P317_IOT_REBATE_AGREEMENT_ID) ,
(SELECT iag.agreement_start_date
FROM iot_agreement iag
WHERE iag.iot_agreement_id =
:P317_IOT_REBATE_AGREEMENT_ID)
)))) a
ORDER BY to_date(a.data_points, 'MON-RR')If I use the same query in a Flash Chart the page still only takes a few seconds to display but the chart takes over a minute to display. APEX ADMIN shows the page being served in 2 - 3 seconds.
If I drop the chart and drop the query into a simple report region, the entire page takes over a minute to display.
Has anyone experienced anything similar?
This is the first time I have used CONNECT BY LEVEL in my SQL so that is my main suspect. But please remember, in SQL/Developer the query executes very quickly indeed.
Many thanks
Kind regards
Simon GaddHi Simon,
I had something similar. When my code ran is SQL is took seconds, but took minutes in an ApEx page. There seems to the something strange with the way ApEx and/or the optimizer execute code via ApEx pages.
I gave my code to one of my tuning experts and they added a simple rule to force index use:
select /*+RULE index(r AMS_REQUESTS_IDX4) */
r.requester_name,
r.ams_type_code_name,
d.display_date
from
(select TO_DATE(:P23_CALENDAR_DATE,'YYYYMMDD')-100+rownum display_date from all_objects where rownum < 300) d,
ams_requests_v r
where
trunc(d.display_date) between trunc(r.start_date) and trunc(r.end_date)
and r.approval_status != 'R'
and to_char(d.display_date,'DY') NOT IN ('SAT','SUN')
and d.display_date not in (select trunc(closure_date) from ams_closures)
and (nvl(r.restricted_view_flag,'N') = 'N' or trunc(r.end_date) >= trunc(sysdate))
and r.department_id in ( select
d.department_id
from
ams_departments d
connect by ( PRIOR DEPARTMENT_ID = PARENT_DEPARTMENT_ID)
start with department_id = (
select
department_id
from
ams_departments
where
nvl(terminating_dept_flag,'N') = 'Y'
and level = (select
min(level)
from
ams_departments
where
nvl(terminating_dept_flag,'N') = 'Y'
connect by department_id = prior parent_department_id
start with department_id = :P0_DEPARTMENT_ID)
connect by department_id = prior parent_department_id
start with department_id = :P0_DEPARTMENT_ID))
UNION
select /*+RULE index(r AMS_REQUESTS_IDX4) */
r.requester_name,
r.ams_type_code_name,
d.display_date
from
(select TO_DATE(:P23_CALENDAR_DATE,'YYYYMMDD')-100+rownum display_date from all_objects where rownum < 300) d,
ams_requests_v r
where
trunc(d.display_date) between trunc(r.start_date) and trunc(r.end_date)
and r.approval_status != 'R'
and to_char(d.display_date,'DY') NOT IN ('SAT','SUN')
and d.display_date not in (select trunc(closure_date) from ams_closures)
and nvl(r.restricted_view_flag,'N') = 'Y'
and r.department_id in ( select distinct
d.department_id
from
ams_departments d
connect by ( PRIOR DEPARTMENT_ID = PARENT_DEPARTMENT_ID)
start with department_id in (
select
department_id
from
ams_departments_v
where
:P0_PERSON_ID in (manager_id, deputy_id,administrator_id)))
UNION
select
NULL requester_name,
t.name ams_type_code_name,
trunc(c.closure_date) display_date
from
ams_closures c,
ams_closure_type_codes_v t
where
c.AMS_CLOSURE_TYPE_CODE = t.AMS_CLOSURE_TYPE_CODE
order by 3,1It might be worth you trying something similar.
best regards,
Martin -
Hi all,,,
Assume that my lov is listing (eg empno, ename,dept) when i query for empno. Actually i want to change that empno to deptno ie(deptno,ename,dept) when i queried for empno. How to accomplish this. I think by form personalization we can do ie. BY creating own record group and replacing that with old one, we can do. But i want to know the query used in old record group. How to accomplish this task. plz help me.. Its urgent ... plz... Advance thanks. Any other approach most welcome.. PlzHi all,,,
Assume that my lov is listing (eg empno, ename,dept) when i query for empno. Actually i want to change that empno to deptno ie(deptno,ename,dept) when i queried for empno. How to accomplish this. I think by form personalization we can do ie. BY creating own record group and replacing that with old one, we can do. But i want to know the query used in old record group. How to accomplish this task. plz help me.. Its urgent ... plz... Advance thanks. Any other approach most welcome.. Plz -
How to get and use connection from Web AS 7.0 pool?
Hello!
I've set up a datasource and connection pool in my Web AS according http://help.sap.com/saphelp_nw70/helpdata/en/c0/3ad4d5cdc66447a188b582aad537d3/frameset.htm
And I have some questions about using connection pool:
1) here is my code which get connection form pool:
InitialContext ctx = new InitialContext();
DataSource datasource = (DataSource) ctx.lookup("jdbc/IndexesDataSource");
Connection con = datasource.getConnection();
return con;
Does it properly?
2) Which method I should call when I need to pass connection back to the pool? Will it be enough to call con.close()?
regards, LevHi Lev,
Write code like this
try{
InitialContext ctx=new InitialContext();
DataSource ds=(DataSource)ctx.lookup("jdbc/indexsDataSource");
Connection con=ds.getConnection();
Statement stmt=con.createStatement();
con.close();
catch(Exception e)
wdComponentAPI.getMessageManager().reportException("Exception "+e,true);
also have a look at below links[Link|http://help.sap.com/saphelp_nw04/helpdata/en/61/fdbc3d16f39e33e10000000a11405a/frameset.htm][Link1|http://help.sap.com/saphelp_nw04/helpdata/en/46/ddc4705e911f43a611840d8decb5f6/frameset.htm][Tutorial|http://help.sap.com/saphelp_nw04/helpdata/en/91/9c2226df76f64fa7783dcaa4534395/frameset.htm]
Regards,
Krishna kattu -
Query tuning for query using connect by prior
I have written following query to fetch the data. The query is written in this format because there are multiple rows, which make one recrd and we need to bring that record into one row.
For one CAT(commented here), this query takes around 4 minutes and fetches 6900 records but when it runs for 3 CAT, it takes 17 mins.
I want to tune this as this has to run for 350 CAT values.
It is doing FTS on the main table. I tried to use different hints like PARALLEL, APPEND (in insert) but nothing worked.
The cost of the query is 51.
Any help/suggestions will be appreciated.
SELECT DISTINCT MIN(SEQ) SEQ,
PT, APP, IT, LR, QT,CD, M_A_FLAG,
STRAGG(REM) REM, -- aggregates the data from different columns to one which is parent
CAT
FROM (WITH R AS (SELECT CAT, SEQ, PT, M_A_FLAG, IT, LR,QT,CD, REM, APP
FROM table1
WHERE REC = '36' AND M_A_FLAG = '1'
--AND CAT = '11113')
SELECT CAT, SEQ,
CONNECT_BY_ROOT PT AS PT,
CONNECT_BY_ROOT APP AS APPL,
M_A_FLAG,
CONNECT_BY_ROOT IT AS IT,
CONNECT_BY_ROOT LR AS LR,
CONNECT_BY_ROOT QT AS QT,
CONNECT_BY_ROOT CD AS CD,
REM
FROM R A
START WITH PT IS NOT NULL
CONNECT BY PRIOR SEQ + 1 = SEQ
AND PRIOR CAT = CAT
AND PT IS NULL)
GROUP BY PT, APP, IT,LR, QT, CD, M_A_FLAG, CAT
ORDER BY SEQ;
Thanks.
Edited by: user2544469 on Feb 11, 2011 1:12 AMThe following threads detail the approach and information required.
Please gather relevant info and post back.
How to post a SQL tuning request - HOW TO: Post a SQL statement tuning request - template posting
When your query takes too long - When your query takes too long ... -
How to tune query using dblink?
Hello,
I have query running on production DB(Version 9i).
The query is fetching data over the dblink.
Its taking around 14 minutes and total number of rows fetched are around 2 million.
Is there any way I can tune the query? I have used driving site hist but that didn't helped.Ario wrote:
Hello,
I have query running on production DB(Version 9i).
The query is fetching data over the dblink.
Its taking around 14 minutes and total number of rows fetched are around 2 million.
Is there any way I can tune the query? I have used driving site hist but that didn't helped.
Do some math and tell us if there is a way you can tune this.
We have no idea what "2 million rows" means in terms of size. That could be 1GB of data, it could be 1PB of data. Figure out how much data you're trying to move (size wise), then figure out roughly how fast your network can pipe the data (again, we have no idea if you have a fiber line and this is an internal network between machines, or if you're transporting the data 1/2 way around the globe over a dial up connection).
When you have all that figured out you'll know if the 14 minutes is / or is not an acceptable amount of time.
Cheers, -
How to setup and use connection pool in Web AS 7.0?
Hello SDN!
I'm developing a Web Dynpro Java application on SAP Web AS 7.0 My app using a couple of DAO classes as model instead of EJBs. These classes perform direct requests to database using JDBC drivers.
I've read that many of web-servers use their own connection pools. So I want to know:
1) how I can setup connection pool in SAP Web AS ?
2) how I can call this pool in my application?
Could you please give me some examples?
Regards, Lev.hi lev,
check this document for jco pools
http://www.winfobase.de/lehre/lv_materialien.nsf/intern01/FB09D79A41930E34C125709F0046180C/$FILE/Tips&Tricks_JCo_Programming.pdf
ravindra -
How to determine query used in Inventory Status (Available-to-Promise) List
Hi,
I'm trying to create a query that will give me commited qty per item per day. I tried checking table used in the Inventory Status(Available-to-Promise) by enabling systmen information under view buth the SAP is giving me "form=154 Item100000054 Pane3 ......... " Can somebody explain or guide me on how i can create the query.
RegardsHi,
Welcome you post on the forum.
Do you need a query report or a formatted search query?
Thanks,
Gordon -
Reg : Hierarchical Query(Using Connect By)
Hi all,
I got the result with the hierarchical query as :
*/qxxh*
*/qxxh/jxobcbg*
*/qxxh/jxobcbg/n00wcp4*
*/qxxh/jxobcbg/n00wcp4/x000263*
*/qxxh/jxobcbg/n00wcp4/x000263/p0263*
*/qxxh/jxxocbg*
*/qxxh/jxxocbg/n00voc1*
*/qxxh/jxxocbg/n00voc1/x000589*
*/qxxh/jxxocbg/n00voc1/x000589/p0589*
*/qxxh/jxuwxxh*
*/qxxh/jxuwxxh/n00xpxf*
*/qxxh/jxuwxxh/n00xpxf/m00bxpl*
*/qxxh/jxuwxxh/n00xpxf/m00bxpl/x000522*
*/qxxh/jxuwxxh/n00xpxf/m00bxpl/x000522/p0522*
Here I want to select only maximum path . Here I used "SYS_CONNECT_BY_PATH"
Please let meknow how to do this ?
Thanks in advance .
Edited by: udeffcv on Dec 9, 2009 10:03 PMudeffcv wrote:
Hi all,
I got the result with the hierarchical query as :
*/qxxh*
*/qxxh/jxobcbg*
*/qxxh/jxobcbg/n00wcp4*
*/qxxh/jxobcbg/n00wcp4/x000263*
*/qxxh/jxobcbg/n00wcp4/x000263/p0263*
*/qxxh/jxxocbg*
*/qxxh/jxxocbg/n00voc1*
*/qxxh/jxxocbg/n00voc1/x000589*
*/qxxh/jxxocbg/n00voc1/x000589/p0589*
*/qxxh/jxuwxxh*
*/qxxh/jxuwxxh/n00xpxf*
*/qxxh/jxuwxxh/n00xpxf/m00bxpl*
*/qxxh/jxuwxxh/n00xpxf/m00bxpl/x000522*
*/qxxh/jxuwxxh/n00xpxf/m00bxpl/x000522/p0522*
Here I want to select only maximum path . Here I used "SYS_CONNECT_BY_PATH"
Please let meknow how to do this ?
Thanks in advance .
Edited by: udeffcv on Dec 9, 2009 10:03 PMwhat do you mean by maximum path?? is it...
*/qxxh/jxobcbg/n00wcp4/x000263/p0263*
*/qxxh/jxxocbg/n00voc1/x000589/p0589*
*/qxxh/jxuwxxh/n00xpxf/m00bxpl/x000522/p0522*
is it child nodes??
then you might like to see
CONNECT_BY_ISLEAF pseudo column..example you can find it in below link
http://download.oracle.com/docs/cd/B14117_01/server.101/b10759/pseudocolumns001.htm#sthref670
Ravi Kumar -
How to define and use special characters
I need to define some special characters (such as an up-arrow and a down-arrow). I need to add these characters to Strings along with "normal" ASCII characters, and finally display the Strings with graphics.drawString().
Can anyone get me started or tell me where to look? I looked at the archive but apparently I'm not using the correct keywords.
Thanks in advance, Rickpublic void paint(Graphics g) {
super.paint(g);
g.drawString("up arrow: \u2191 down arrow: \u2193", 100, 100);
}Works for me.
See www.unicode.org for code charts. -
How to define and use FMS in UDF?
Hello,
I searched the forum and find the link:
https://websmp103.sap-ag.de/~sapidb/011000358700004565932005E/How_To_DefindeAndUseFMS.pdf
but I can't access this link. I think it's useful to me. can anyone send it to me? Thanks a lot.ok
-
How to define in the optional the clause, the maximum value of a colum
Hi!
I'm using select data block from database toolset.
I want to use optional clause wiring to import only the maximum value of the column 2.
E.g. to import only the values greater than 1 you have to wire a string like this col2>1. But i don't know which string i have to wire to iport the maximum value of this column.
Thank you in advance.
LarsonHi Larson
To select row n you either need a certain value (e.g. an index or something you know is unique in row n) or you select all values and extract one row in LV.
So the first statement would be "SELECT * (* means all columns, you could also specify the columns as well) FROM data WHERE [a value = another value]. This would give you just the rows where the where-clause is correct. The other way is to select all data and just extract row n from the array you get from the recordset.
There might be other ways, but these are two solutions I just had in mind.
Manuals - just google the internet for SQL (Structured Query Language) and you will find lots of websites providing this information.
Hope this helps.
Thomas
Using LV8.0
Don't be afraid to rate a good answer...
Maybe you are looking for
-
Hello Friends: In UK Payroll new tax code 1000L for year 2014-April 06th. I have updated in Constant table V_T511K effective April 06th 2014 but this code is not going to Ceridian interface(PU12). I would like to know where we need to apply this
-
Issue while generating report using web.show_document with https
Hi All, I am facing some issue while seeing the report using web.show_document as shown below: https://ucrmskr.apac.nsroot.net:10301/forms/html/001725032_gca.rtf_ In this case the report opens directly without asking for me to save or open or cancel
-
I have a problem with a software update!
I recently got a new macbook pro and I was doing a software update last night as it was required but then when it started to do the update the computer installed the software and then a barring noise came from the mac and it tried to restart 4 times
-
Overheating for iBook G4 (14 inch)
hello. first time poster, and far from computer literate, so your help would be greatly appreciated, thanks. i am presently in Malta, where there is no apple service centers, and am worried after hearing about the battery recall / overheating problem
-
Missed call with iOS won't disappear
When I have a missed call in my Lock screen in iOS5 and I use the slide function to directly return the call, the missed call won't disappear when I hang up the phone. Also when I unlock it, the Phone icon stats that I have still one missed call. Any