Connect by PRIOR vrs Connect by LEVEL
Hello
I'm trying to understand the difference between CONNECT BY PRIOR and CONNECT BY LEVEL
I know that CONNECT BY PRIOR is basically specifying a hierarchy and which column / field is the parent (using the PRIOR)
for example
SELECT employe_id, employee_name, level
Employees
CONNECT BY PRIOR employee_id = manager_id;
Q1. What is CONNECT BY LEVEL (for example as shown below) practice? What type of relationship normally described? (below, it is just producucing a series of integers)
SELECT the level
OF the double
CONNECT BY LEVEL < = 10;
T2. Use LEVEL with CONNECT BY in this way, a subversive use of the CONNECT IN knowledge is really intended to be used this way?
any advice appreciated,
Jim
Hi, Jim,.
Jimbo wrote:
Hello
I'm trying to understand the difference between CONNECT BY PRIOR and CONNECT BY LEVEL
I know that CONNECT BY PRIOR is basically specifying a hierarchy and which column / field is the parent (using the PRIOR)
for example
SELECT employe_id, employee_name, level
Employees
CONNECT BY PRIOR employee_id = manager_id;
Sometimes, CONNECT BY using hierarchical data, such as a tree. In these cases, it may be useful to use the terns as "parent" and "child." The query above is one of these cases.
In more general terms, CONNECT BY returns the union (UNION ALL, specifically) of lines found by the START WITH clause (LEVEL = 1) and lines found by the clause CONNECT BY (LEVEL > 1). If there is no START WITH clause, every row in the table will appear on LEVEL = 1.
If there is no line on LEVEL = N, then Oracle research lines that meet the CONNECT BY clause, to search for the lines that are connected to each line level = N, for put the LEVEL N + 1. If the CONNECT BY clause is set to TRUE for a given row, while the rank will appear on level N + 1 =. Often (but not always) the CONNECT BY clause uses the PRIOR operator. After PRIOR consent refers to something = N level.
Q1. What is CONNECT BY LEVEL (for example as shown below) practice? What type of relationship normally described? (below, it is just producucing a series of integers)
SELECT the level
OF the double
CONNECT BY LEVEL<>
Since there is no START WITH clause, each line of the double table will be LEVEL = 1.
The CONNECT BY clause means that CONNECT BY always means: If there is a line at = N, then it will be connected to any line that fills the CONNECT BY conditions.
So that the lines will be added to the level = 2? All the lines where condition 2<= 10="" is="" true,="" so="" every="" row="" in="" the="" dual="" table="" will="" be="" on="" level="">=>
What lines will be added to the level = 3? All the lines where condition 3<= 10="" is="" true,="" so="" every="" row="" in="" the="" dual="" table="" will="" be="" on="" level="">=>
...
What lines will be added to the level = 10? All the lines where the condition of 10<= 10="" is="" true,="" so="" every="" row="" in="" the="" dual="" table="" will="" be="" on="" level="10." 10="" is="" true,="" so="" every="" row="" in="" the="" dual="" table="" will="" be="" on="" level="">=>
What lines will be added to the level = 11? All the lines where condition 11<= 10="" is="" true,="" so="" no="" row="" in="" the="" dual="" table="" will="" be="" on="" level="">=>
Since no rows are added to the LEVEL = 11, CONNECT BY query stop right there.
Q2. Use LEVEL with CONNECT BY in this way, a subversive use of the CONNECT IN knowledge is really intended to be used this way?
You can think of it as being subversive. It was certainly not how planners and the deveopers at Oracle for CONNECT BY work in versions 2 to 8; you got an error if you tried in these versions. It is also an exception to the rule that no line can be his own ancestor.
Tags: Database
Similar Questions
-
CONNECTION BY level/prior/root
Hi all
I'm trying to CONNECT BY level/prior/root. But I can barely decipher what it actually does. I think it is a very useful method provided by oracle and can have several uses. can someone help me on this please.
I googled on this topic but everywhere I could find only employee and manager problem that will be solved by query below.
But if we just query as below, we can get the employee and his manage without CONNECT BY.SELECT employee_id, last_name, manager_id FROM employees CONNECT BY PRIOR employee_id = manager_id;
Other queries is obtained to connect by is below, but he could not also include:SELECT employee_id, last_name, manager_id FROM employees;
Please help me.select SYSDATE-41 + level - 1 the_date from dual connect by level <= SYSDATE - SYSDATE-41 + 1
Published by: J2EE_Life on December 6, 2011 08:12J2EE_Life wrote:
Hi allI'm trying to CONNECT BY level/prior/root. But I can barely decipher what it actually does. I think it is a very useful method provided by oracle and can have several uses. can someone help me on this please.
I googled on this topic but everywhere I could find only employee and manager problem that will be solved by query below.
SELECT employe_id, last_name, manager_id
Employees
CONNECT BY PRIOR employee_id = manager_id;But if we just query as below, we can get the employee and his manage without CONNECT BY.
SELECT employe_id, last_name, manager_id
Employees;In fact, those who are not the same.
If you use the standard table of hr.employees, the CONNECT BY query above returns 315 lines, but the secoind query return lines only 107.CONNECTION is useful to show the recursive relationships. It can show you, for example, not only that reports directly to a data manager, but the whole hierarchy of persons under a given head. For example, the following shows the hierarchy starting with an employee named "Rooster":
SELECT employee_id, last_name, manager_id , LEVEL AS lvl , SYS_CONNECT_BY_PATH (last_name, '/') AS path FROM hr.employees START WITH last_name = 'De Haan' CONNECT BY PRIOR employee_id = manager_id; SELECT SYSDATE - SYSDATE-41 + 1 FROM DUAL;
Output:
EMPLOYEE_ID LAST_NAME MANAGER_ID LVL PATH ----------- ---------- ---------- --- -------------------------- 102 De Haan 100 1 /De Haan 103 Hunold 102 2 /De Haan/Hunold 104 Ernst 103 3 /De Haan/Hunold/Ernst 105 Austin 103 3 /De Haan/Hunold/Austin 106 Pataballa 103 3 /De Haan/Hunold/Pataballa 107 Lorentz 103 3 /De Haan/Hunold/Lorentz
Very briefly, here's how it works.
The result set of a query CONNECT BY is the UNION ALL of several queries, all sharing a common SELECT clause.
Any line that meets the conditions of the START WITH clause is supposed to be at LEVEL 1. So you can think of the first branch of the UNION of ALL being (in this case):SELECT employee_id, last_name, manager_id , LEVEL AS lvl , SYS_CONNECT_BY_PATH (last_name, '/') AS path FROM hr.employees WHERE last_name = 'De Haan' UNION ALL ...
Other rows in the result set will be LEVEL = N if the conditions of the CONNECT BY clause, where the PRIOR operator refers to any line level = N - 1.
For example, the line with name = "Hunold" satisfies the condition to connect BY when the FIRST line is the line where last_name = "Rooster", Hunold is in the game with the LEVEL of results = 2.
Another example: the line with name = 'Lorentz' meets the condition to connect BY when the FIRST line is the line where last_name = "Hunold", Hunold is in the result set with LEVEL = 3.
This is a classic example of a tree structure, where the rows in the table are connected to other lines in a parent-child relationship. In a tree, each row has 0 or 1 of the parents, but a line can have any humber children. Haan has 0 parent and 1 child (that is, Hunold). Hunold has 1 parent (De Haan) and 4 children. CONNECT BY queries are useful to deal with the trees like this, especially when the parent-child relationship may extend to a number any levels.Other queries is obtained to connect by is below, but he could not also include:
Select the level - 1 the_date + SYSDATE-41
of the double
connect by level<= sysdate="" -="" sysdate-41="" +="">=>I don't understand whether you.
SYSDATE-SYSDATE is just 0
0 - 41 + 1 is just-40, so you might as well say:select SYSDATE-41 + level - 1 the_date from dual connect by level <= -40;
In addition, LEVEL is always a positive integer, then the CONNECT BY State will never have the value TRUE, and if the CONNECT BY clause can never be TRUE, there is no reason to have a CONNECT BY clause.
A more reasonable request is:SELECT SYSDATE + LEVEL - 1 AS the_date FROM dual CONNECT BY LEVEL <= 4 -- or any positive number ;
which produces this output, the given number of days (4 in this example) from today:
THE_DATE -------------------- 06-Dec-2011 11:08:24 07-Dec-2011 11:08:24 08-Dec-2011 11:08:24 09-Dec-2011 11:08:24
This is a very common way to generate a Counter of Table (a table, or, as in this case, a result set, that matters). For example, if you need for the aggregates per day in a table and you want to show 0 for days that never exist in the table, then you outer join in a table of counter like the one shown above.
This is a common use of CONNECT BY, but this is an unusual exception to the rules that is prohibited govern CONNECT BY queries. I suggest you uderstand how the query on hr.employees, above, the work before you start trying to figure out how it works.Published by: Frank Kulash, December 6, 2011 11:15
The pages below are introductions to CONNECT BY:
http://asktom.Oracle.com/pls/asktom/f?p=100:11:0:P11_QUESTION_ID:489772591421
http://Philip.Greenspun.com/SQL/trees -
Generation SQL line with connection by level
Hello
I'm looking for generate lines based on a quantity of part numbers. I have looked all around several techniques for the generation of the line, but have failed to address this problem. The current method, it seems pulls a permutation of all possible combinations, and the result is rising quickly to millions / billions of documents... which is not the answer. Someone out there who could help writing a query to do this?
What I try to achieve if a quantity of part 5, I want to display 5 Qty 1 documents. If the Qty 12, then 12 Qty 1 documents. So on and so forth.
Out Oracle 10.2.0.4 operation
Sample data:
drop table test;
create table test (part varchar2 (20), number of quantity);
Insert test values ('WIDGET', 12);
Insert test values ('FOO', 5);
Insert test values ('THING', 3);
Insert test values ("WAD", 8);
commit;
-This query does not work... I think it's getting all the possible permutations
-Select a part, 1
-to test
-connect by level < = Qty
-what I want to see...
Part Qty.
------ ----
WIDGET 1
WIDGET 1
WIDGET 1
WIDGET 1
WIDGET 1
WIDGET 1
WIDGET 1
WIDGET 1
WIDGET 1
WIDGET 1
WIDGET 1
WIDGET 1
FOO 1
FOO 1
FOO 1
FOO 1
FOO 1
THING 1
THING 1
THING 1
BUNDLE 1
BUNDLE 1
BUNDLE 1
BUNDLE 1
BUNDLE 1
BUNDLE 1
BUNDLE 1
BUNDLE 1
Hello
You want something like this:
SELECT part
Qty. 1
OF the test
CONNECT NOCYCLE LEVEL<=>=>
AND an EARLIER part = part
AND PRIOR SYS_GUID () IS NOT NULL
;
You may have copied the request of something that just use the array of double. When you have more than 1 row of the table, then you need by using the PRIOR operator in the CONNECT BY clause.
-
Connect by level - with several lines of inpur
Very simplified, I have this table - it has a 'table' as a variable-length structure.
I need x line of output for each input line (do not use pipelining og PL/SQL function)
Wih a single line as input of the "log in"-it works of course.
How to achieve the same thing for several input lines?drop table test3; create table test3 (id number, tekst varchar2(20)); insert into test3 values (1, 'acbdef'); insert into test3 values (2, '123'); insert into test3 values (3, 'HUUGHFTT'); insert into test3 values (4, 'A'); insert into test3 values (5, 'AKAJKSHKJASHKAJSHJKJ'); commit; with tal as ( select * from (select a.*, rownum rn from test3 a) where rn < 2) ------ select tekst, level , substr(tekst,(level-1)*1+1, 1) content from tal connect by level < length(tekst) ;
I know I can do it in PL/SQL using plan either pl or just a function in the pipeline, but I prefer a clean if possible SQL.
I tried to do in a cross join test3 and (select different values of double the test3 table) and other versions, but all with syntax errors
Oracle version will be 10.2 and 11 +.with tal as ( select * from (select a.*, rownum rn from test3 a) where rn < 3) ------ select * from test3 cross join table ( select tekst, level , substr(tekst,(level-1)*1+1, 1) content from dual connect by level < length(tekst) ) ;
I think it's kind of what you're looking for:
with tal as ( select 1 id, 'acbdef' tekst from dual union select 2 , '123' from dual union select 3 , 'HUUGHFTT' from dual union select 4 , 'A' from dual ) ------ select id, tekst, level, substr(tekst,(level-1)*1+1, 1) content from tal connect by ( level <= length(tekst) and prior id = id and prior dbms_random.value is not null ) ; ID TEKST LEVEL CONTENT ---------- -------- ---------- ------- 1 acbdef 1 a 1 acbdef 2 c 1 acbdef 3 b 1 acbdef 4 d 1 acbdef 5 e 1 acbdef 6 f 2 123 1 1 2 123 2 2 2 123 3 3 3 HUUGHFTT 1 H 3 HUUGHFTT 2 U 3 HUUGHFTT 3 U 3 HUUGHFTT 4 G 3 HUUGHFTT 5 H 3 HUUGHFTT 6 F 3 HUUGHFTT 7 T 3 HUUGHFTT 8 T 4 A 1 A
-
Question on connect by level 2 records
My query begins with the records of two pairs and continues with the rank and adds two records to pair again.
However, the unwanted extra each level (mbc 11, 12 wtc...) in the result.
would you help me.
My request;
SELECT CUT (SUBSTR ("abc 11 12 11 22 33 kbc vbc wtc mbc mbc 17 45', INSTR kbs (" abc 11 12 11 22 33 17 mbc kbc vbc wtc mbc kbs 45 ', ' ', 1, level) + 1, "))
INSTR ("abc 11 12 11 22 33 17 mbc kbc vbc wtc mbc kbs 45 ', ' ', 2, level + 1)-INSTR (" abc 11 12 11 22 33 17 mbc kbc vbc wtc mbc kbs 45 ', ' ', 1, level)-1)) AS PAIR
OF THE DOUBLE
CONNECT BY level < = LENGTH(' abc 11 mbc 12 wtc 11 vbc 22 kbc 33 mbc 17 kbs 45 ') LENGTH (REPLACE ('abc 11 12 11 22 33 17 mbc kbc vbc wtc mbc kbs 45', ' ', "))-1
Outcome;
ABC 11 MBC 12 WTC 11 Vbc 22 KBC 33 MBC 17 KBS 45 Problem result;
ABC 11 11 mbc MBC 12 WTC 12 WTC 11 11 vbc Vbc 22 22 kbc KBC 33 MBC 33 MBC 17 17 kbs KBS 45 Hello
Jimmie_M wrote:
I thank very you much for your time. Thank you!!!
is it possible to keep my request to get the result without regexp_substr.
If I run regexp_substr in the table, the slow performance.
Thank you
You were on the right track with your first message. The problem with this request was that you were each pair of words:
- Word 1 and 2,
- Word 2 and 3,
- Word 3 and 4,
- Word 4 and 5,
and so on. You don't want that every other pair:
- Word 1 and 2,
- Word 3 and 4,
and so on.
Here's a way to do it:
WITH the data AS
(
SELECT ' abc 11 12 11 22 33 kbc vbc wtc mbc mbc 17 45' AS str kbs
OF the double
)
got_space_pos AS
(
SELECT 1 * LEVEL AS n
, INSTR (str, ' ', 1, (2 * LEVEL)-1) + 1 AS start_pos
, INSTR (str, ' ', 1, (2 * LEVEL) + 1) AS end_pos
FROM the data
CONNECT BY LEVEL<= (="" length="">=>
-LENGTH (REPLACE (str, ' '))
) / 2
)
SUBSTR (str, start_pos, end_pos - start_pos) SELECT as a PAIR
OF got_space_pos
JOIN CROSS data
ORDER BY start_pos
;
Output:
PAIR
-------------
ABC 11
MBC 12
WTC 11
Vbc 22
KBC 33
MBC 17
KBS 45
Are you regular expressions of course are initially slow Paulzip query? That's maybe something about your data you have not shown or explained. As said by ground, you must always follow the Forum FAQ:
Re: 2. How can I ask a question in the forums?
so that people who want to help you can recreate the problem and test their ideas.
In addition, the sample data (CREATE TABLE and INSERT statements), be sure to upgrade your version of Oracle. A recursive (available in Oracle 11.2 and upwards) WITH clause might help in this problem. or maybe CROSS APPLY (Oracle 12.1) would be useful.
Simplify the prblem as much as possible; for example, you generally need not show all columns in your actual tables, only those involved in the problem. But make sure that you validate the example data illustrate the real problem.
-
Hi gurus. Can someone explain to me under requests, why connect by the work in this way. Differences between 2 requests is marked by red color.
WITH TAB(KEY_ID,KEY_1) AS (SELECT 1,'Z/D/YYYY/MM' FROM DUAL UNION ALL SELECT 2,'YYYY/MM/Z/D' FROM DUAL) SELECT LISTAGG(CASE WHEN KEY_1 = 'Z' THEN 'TEST_Z' WHEN KEY_1 = 'D' THEN TO_CHAR(EXTRACT(DAY FROM SYSDATE)) WHEN KEY_1 = 'YYYY' THEN TO_CHAR(EXTRACT(YEAR FROM SYSDATE)) WHEN KEY_1 = 'MM' THEN TO_CHAR(EXTRACT(MONTH FROM SYSDATE)) END, '/') WITHIN GROUP (ORDER BY LVL)CONVERT_KEY, MAX(KEY_ID) KEY_ID, COUNT(DISTINCT KEY_ID) CNT FROM (SELECT REGEXP_SUBSTR(REGEXP_REPLACE(KEY_1, '[^[:graph:]]', ''), '[^/]+', 1, LEVEL)KEY_1, LEVEL LVL, KEY_ID FROM(SELECT * FROM TAB WHERE KEY_ID = 1) CONNECT BY LEVEL <= LENGTH(REGEXP_REPLACE(KEY_1, '[^/]+')) + 1);
Correct output:
convert_key Key_ID cnt TEST_Z/20/2015/4 1 1 WITH TAB(KEY_ID,KEY_1) AS (SELECT 1,'Z/D/YYYY/MM' FROM DUAL UNION ALL SELECT 2,'YYYY/MM/Z/D' FROM DUAL) SELECT LISTAGG(CASE WHEN KEY_1 = 'Z' THEN 'TEST_Z' WHEN KEY_1 = 'D' THEN TO_CHAR(EXTRACT(DAY FROM SYSDATE)) WHEN KEY_1 = 'YYYY' THEN TO_CHAR(EXTRACT(YEAR FROM SYSDATE)) WHEN KEY_1 = 'MM' THEN TO_CHAR(EXTRACT(MONTH FROM SYSDATE)) END, '/') WITHIN GROUP (ORDER BY LVL)CONVERT_KEY, MAX(KEY_ID) KEY_ID, COUNT(DISTINCT KEY_ID) CNT FROM (SELECT REGEXP_SUBSTR(REGEXP_REPLACE(KEY_1, '[^[:graph:]]', ''), '[^/]+', 1, LEVEL)KEY_1, LEVEL LVL, KEY_ID FROM TAB WHERE KEY_ID = 1 CONNECT BY LEVEL <= LENGTH(REGEXP_REPLACE(KEY_1, '[^/]+')) + 1);
Poor performance:
convert_key Key_ID CNT TEST_Z/20/20/2015/2015/2015/2015/4/4/4/4/4/4/4/4 1 1 WHERE in hierarchical queries this clause is applied AFTER the construction of a hierarchy.
SY.
-
Need help with a query of type "connect by level.
Hello, I recently met 'connect by level' and I think he can solve my problem, but maybe not. I would like to create rows of data where the number of rows created varies according to the data from the original table.
It works:
with times like
(select to_date (' 26/01/2014 01:00 ',' dd/mm/yyyy hh24:mi:ss') starttime,)
TO_DATE (' 27/01/2014 00:00:00 ',' dd/mm/yyyy hh24:mi:ss') stoptime
the double)
Select starttime, stoptime, starttime + rownum / 24
of the time
connect by level < =.
(Select trunc ((stoptime-starttime) * 24: 2) at the time)
I would like to do something similar for several lines (following does not work)
with times like
(select to_date (' 26/01/2014 01:00 ',' dd/mm/yyyy hh24:mi:ss') starttime,)
TO_DATE (' 27/01/2014 00:00:00 ',' dd/mm/yyyy hh24:mi:ss') stoptime
of the double
Union
Select to_date (' 25/01/2014 18:00 ',' dd/mm/yyyy hh24:mi:ss') starttime.
TO_DATE (' 27/01/2014 00:00:00 ',' dd/mm/yyyy hh24:mi:ss') stoptime
the double)
Select starttime, stoptime, starttime + rownum / 24
of the time
connect by level < =.
(Select trunc ((stoptime-starttime) * 24: 2) at the time)
I need to stay away from solutions of PL/SQL, this query will be wrapped in an application that can not handle the PL/SQL, going and coming from the database (I also suck in PL/SQL).
Any help would be greatly appreciated, I have no knowledge here, I tried to read some of the documentation on hierarchical queries, but it is not yet clicking.
Thank you!
Or also:
SQL > WITH times
2 ALSO (SELECT TO_DATE (' 01:00 26/01/2014 ', "hh24:mi:ss dd/mm/yyyy") Starttime)
3, TO_DATE (' 01/26/2014 03:00 ', 'hh24:mi:ss dd/mm/yyyy') Stoptime
4 FROM TWO
5 UNION
6 SELECT TO_DATE (' 25/01/2014 18:00 ', "hh24:mi:ss mm/dd/yyyy") Starttime
7, TO_DATE (' 25/01/2014 22:00 ', 'hh24:mi:ss dd/mm/yyyy') Stoptime
8 DOUBLE)
9. SELECT T.*, Starttime + Lvl / 24
10. OF time T
11, (SELECT LEVEL Lvl
THE DOUBLE 12
13 CONNECT BY LEVEL<>
14 (SELECT MAX (TRUNC ((Stoptime-Starttime) * 24)))
15 AT the time)):
16. WHERE the lvl<= trunc="" (="" (stoptime="" -="" starttime)="" *="">=>
17 ORDER 1, 3
18.
STARTTIME STOPTIME-STARTTIME + LVL/24
----------------------------- ----------------------------- -----------------------------
January 25, 2014 18:00 January 25, 2014 22:00 January 25, 2014 19:00
January 25, 2014 18:00 January 25, 2014 22:00 January 25, 2014 20:00
January 25, 2014 18:00 January 25, 2014 22:00 January 25, 2014 21:00
January 25, 2014 18:00 January 25, 2014 22:00 January 25, 2014 22:00
26 January 2014 01:00 26 January 2014 03:00 January 26, 2014 02:00
26 January 2014 01:00 26 January 2014 03:00 January 26, 2014 03:00
6 selected lines.
-
ORA-01446 tabular with CONNECTION BY LEVEL <; = 2
I created a tabular presentation where I want the 2 last lines be empty so that the user can enter new lines immediately (for example, the first time that no data is found).
Here's the sql code of the form of tables:
I get the following error:select "ID", "REQ_ID", "QUANTITY", "FRAME_SIZE", "FRAME_TYPE", "PROTECTIVE_COVERING", "MATT", "MATT_COLOR" from "#OWNER#"."CREATIVE_SVC_DESIGN_FRAMING" union all select null id, null req_id, null quantity, null frame_size, null frame_type, null protective_covering, null matt, null matt_color from dual connect by level <= 2
Failed to parse the SQL query:
ORA-01446: cannot select ROWID, or sample, a view with DISTINCT, GROUP BY, etc.
The query is not access ROWID view at all. And, as you can see, it does not use a DISTINCT, GROUP BY, etc...
My APEX is Application Express 4.0.2.00.07
My database is 10g (10.2.0.5.0)
Help, please!
Robert
http://apexjscss.blogspot.comRobert,
I wish I had a better explanation for you, but I know something again with 4.1 broke this trick union for tabular forms. I had to do a dynamic action that runs when the page loads and calls the javascript function of the: AddRow(). I think it has something to do with the validation of the form in a table, but I'm not sure.
See you soon,.
Janet Tyson -
Use of RegExp and connect by level in Oracle SQL
Hello
Can someone help me to know about the use of regular expressions and the concept connect by level in oracle SQL?
Thanks adavancePrakash wrote:
Helloselect regexp_substr(str, '[^,]+', 1, level) val from (select 'a,b,c,d' str from dual) connect by level <= length(str) - length(replace(str,','))+1 i tried my level to understand it but couldn't Please explain how it works
Please only put code in tags code, otherwise it's hard to see what you're asking.
In response to your question look at the query...
SQL> select regexp_substr(str, '[^,]+', 1, level) val 2 from (select 'a,b,c,d' str from dual) 3 connect by level <= length(str) - length(replace(str,','))+1 4 / VAL ------- a b c d
OK, so clearly it's split the string anywhere where there is a comma, then, how he does it.
Firsly we look at connect by clause it when we use it as a generator of line...
SQL> select level from dual connect by level <= 4; LEVEL ---------- 1 2 3 4
Now, look at how the number 4 is calculated from the string...
SQL> select length('a,b,c,d') - length(replace('a,b,c,d',','))+1 from dual; LENGTH('A,B,C,D')-LENGTH(REPLACE('A,B,C,D',','))+1 -------------------------------------------------- 4
... looking at the length of the string and the length of it without comma, we can determine how many commas are in the chain and the surmize that there must be 1 more value there are commas.
Now look at the regular expression...
SQL> select regexp_substr('a,b,c,d', '[^,]+', 1, 1) from dual; R - a SQL> select regexp_substr('a,b,c,d', '[^,]+', 1, 2) from dual; R - b SQL> select regexp_substr('a,b,c,d', '[^,]+', 1, 3) from dual; R - c SQL> select regexp_substr('a,b,c,d', '[^,]+', 1, 4) from dual; R - d
The 4th parameter of the regexp_substr function is the 'presence' of the search string to return.
The actual regular expression itself is looking for 1 or more characters (sign +) who are not (the ^ symbol) a comma.
Is that more clear?
-
Connection by level: is there a difference between 10g and 9i?
Hello
When I run the following query
using Oracle10g Release2 database, I have a set of two lines:Select level from dual connect by level <= 2
.1 2
But when I run the same query by using Oracle 9i database, I have an a single row result set:
.1
Please, help me understand why this difference?
Thank you.Yes, there is. In the earlier versions as 9i, you must use see online:
select * from ( select level from dual connect by level <= 2 ) /
SY.
-
Connect by level using the count of the records collection
Hello:
PROBLEM:
...
), (select rownum number months of double connect by level < = Cnt)
Causes not valid ORA-00904 identifier. Why can I not use "Cnt" of my main as request by using a constant in the works?
The value of count is determined for each date range in the section, I return the records that I need?
Thank you, Sidney
BACKGROUND:
I have to be able to display a list of tax returns to my users and the status of these statements. There's no physical feedback so it is necessary to create data dynamically records using appropriate selection. It's not hard and I thought that would be enough to use a connection by level to give me the date, statements of information so that I can calculate and display the individual. However oracle gives me an ORA-00904 when I try to send the "Cnt" value to connect by level. Everything works fine when I provided a constant instead of "CNT". The "CNT" value is determined by a complex process that calculates start and end for several types of return dates, etc as well as the number of periods and frequency of production. The data must be generated dynamically using a master record, which then gives the history of coding in which my collection of basic records is selected. Here is the result of this process:
TaxpayerNo, TaxClass TaxCode, FilingFequency, StartDate, StopDate, Cnt, frequency
10 S 1 M 12-18 6/1/2007-11/30/2008
10 S 2 M 12 10 30/11/2008-9/30/2009
10 S, 2 Q 4 3 11/30/2010-8/18/2011
L 10 8 6/1/2007 9/30/2009 3 1
L 10 8 11/30/1 / 2010 8/18/2011 1
According to the results, I have needs a record for each every month, quarter, etc. ie:
10 S 1 M 6/1/2007 11/30/2008 18 12 6/1/2007
10 S 1 M 6/1/2007 11/30/2008 18 12 7/1/2007
10 S 1 M 6/1/2007 11/30/2008 18 12 8/1/2007
10 S 1 M 6/1/2007 11/30/2008 18 12 9/1/2007
...
10 S 2 M 30/11/2008-9/30/2009 10 12 11/30/2008
10 S 2 M 30/11/2008-9/30/2009 10 12 12/30/2008
...
etc.
DOES NOT WORK
Select y.*, MonthNo, Add_Months(StartDate,MonthNo*Frequency) in)
Select x.*,
(case when FilingFrequency = am' then Ceil (Months_Between (StopDate, StartDate)))
When FilingFrequency = 'Q' then Ceil (Months_Between(StopDate,StartDate)/3)
When FilingFrequency = 'A' Ceil (Months_Between(StopDate,StartDate)/12) then
0 otherwise
end) NTC,
(case when FilingFrequency = am' then 1)
When FilingFrequency = 'Q' then 3
When FilingFrequency = 'A' then 12
end) frequency
de)
... a code complex to calculate values of start and stop dates required above...
) x
), (select rownum number months of double connect by level < = Cnt)
ERROR MESSAGE
The result is ORA-00904: "CNT": invalid identifier. I do not get an error if I use a constant:
WORK WITH THE HELP OF A CONSTANT, BUT MUST HAVE THE VALUE OF THE ACTUAL CNT
... Same code for generating data...
), (select rownum number months of double connect by level < = 3)
How can I get it works using the "CNT" value instead of a constant?A technique like this should solve your problem.
TUBBY_TUBBZ?with data (col1, cnt) as 2 ( 3 select 1, 3 from dual 4 union all 5 select 2, 2 from dual 6 ) 7 select 8 d.col1, 9 t.column_value 10 from 11 data d, 12 table(cast(multiset(select level from dual connect by level <= d.cnt) as sys.OdciNumberList)) t; COL1 COLUMN_VALUE ------------------ ------------------ 1 1 1 2 1 3 2 1 2 2 5 rows selected. Elapsed: 00:00:00.00 TUBBY_TUBBZ?
Contrary to what you have now, which is basically this
TUBBY_TUBBZ?with data (col1, cnt) as 2 ( 3 select 1, 3 from dual 4 union all 5 select 2, 2 from dual 6 ) 7 select 8 d.col1, 9 level 10 from 11 data d 12 connect by level <= d.cnt; COL1 LEVEL ------------------ ------------------ 1 1 1 2 1 3 2 2 1 3 2 1 1 2 1 3 2 2 1 3 10 rows selected. Elapsed: 00:00:00.00 TUBBY_TUBBZ?
-
SQL - connect by level problem
Hello
Under query returns only first 50 records but his having 233 files.
SELECT TO_CHAR (INN. START_DATE + LEVEL-1, ' DD/MM/YYYY') GENERATION_DATE OF
(SELECT TO_DATE (3 JULY 2011 ', ' DD/MM/YYYY') START_DATE, TO_DATE (OCTOBER 25, 2011 ', ' DD/MM/YYYY') END_DATE DOUBLE) INN
CONNECTION OF LEVEL between 1 and INN. END_DATE-INN. START_DATE + 1;
What is the exact problem in the operator level. Please suggest me.
Concerning
Sudhakar P.What are you talking about?
SQL> select count(*) from( 2 SELECT TO_CHAR(INN.START_DATE+LEVEL-1, 'DD/MM/YYYY') GENERATION_DATE FROM 3 (SELECT TO_DATE( '07/03/2011' ,'DD/MM/YYYY') START_DATE, TO_DATE( '25/10/2011' ,'DD/MM/YYYY') E ND_DATE FROM DUAL) INN 4 CONNECT BY LEVEL between 1 and INN.END_DATE-INN.START_DATE+1); COUNT(*) ---------- 233 1 row selected. Elapsed: 00:00:00.00
He returns 233 records.
-
Y at - it something I do wrong below? I need generate 9 lines instead of 39. Can someone please?
Thanks in advance.
With the help of 10g and 11g
CREATE TABLE V9_MONTHLY ( S_ID VARCHAR2(4), P_ID VARCHAR2(4), F_ID VARCHAR2(4), ELIGIBLE_MONTH DATE, CONTRACT VARCHAR2(4), S_ADDRESS_SUFFIX VARCHAR2(1), REV FLOAT, EXPENSE FLOAT, NI FLOAT ) ; INSERT INTO V9_MONTHLY ( S_ID, P_ID, F_ID, ELIGIBLE_MONTH, CONTRACT, S_ADDRESS_SUFFIX, REV, EXPENSE, NI ) VALUES ( 'S001', 'P001', 'F001', '01-DEC-2009', 'C001', 'B', '300', '200', '100' ) ; INSERT INTO V9_MONTHLY ( S_ID, P_ID, F_ID, ELIGIBLE_MONTH, CONTRACT, S_ADDRESS_SUFFIX, REV, EXPENSE, NI ) VALUES ( 'S001', 'P001', 'F001', '01-JAN-2010', 'C001', 'B', '100', '1000', '-900' ) ; INSERT INTO V9_MONTHLY ( S_ID, P_ID, F_ID, ELIGIBLE_MONTH, CONTRACT, S_ADDRESS_SUFFIX, REV, EXPENSE, NI ) VALUES ( 'S001', 'P002', 'F002', '01-FEB-2010', 'C001', 'B', '200', '10000', '-9800' ) ; ( SELECT s_id, p_id, f_id, ADD_MONTHS(ELIGIBLE_MONTH,LEVEL-1) ELIGIBLE_MONTH FROM ( SELECT s_id, p_id, f_id, ELIGIBLE_MONTH FROM V9_MONTHLY ) CONNECT BY LEVEL <= 3 )
could you pass your connection by level go out and do his own table
WITH counter AS ( SELECT LEVEL cnt FROM DUAL CONNECT BY LEVEL <= 3) SELECT s_id, p_id, f_id, ADD_MONTHS (ELIGIBLE_MONTH, cnt - 1) ELIGIBLE_MONTH FROM V9_MONTHLY, counter;
S_ID P_ID F_ID ELIGIBLE_MONTH S001 P001 F001 12/1/2009 S001 P001 F001 1/1/2010 S001 P002 F002 2/1/2010 S001 P001 F001 1/1/2010 S001 P001 F001 2/1/2010 S001 P002 F002 3/1/2010 S001 P001 F001 2/1/2010 S001 P001 F001 3/1/2010 S001 P002 F002 4/1/2010
believe this should work if you're a fan of the model clause
/* Formatted on 12/10/2010 2:30:51 PM (QP5 v5.149.1003.31008) */ SELECT s_id, p_id, f_id, ELIGIBLE_MONTH FROM V9_MONTHLY MODEL PARTITION BY (s_id, p_id, f_id, ELIGIBLE_MONTH x) DIMENSION BY (0 d) MEASURES (eligible_month) RULES ITERATE (3) (eligible_month [ITERATION_NUMBER] = ADD_MONTHS (eligible_month[0], ITERATION_NUMBER));
Published by: pollywog on December 10, 2010 14:38
and once again using recursive with
* Formatted on 12/10/2010 3:56:49 PM (QP5 v5.149.1003.31008) */ WITH t ( s_id, p_id, f_id, nbr, ELIGIBLE_MONTH ) AS ( SELECT s_id, p_id, f_id, 1, ELIGIBLE_MONTH FROM V9_MONTHLY UNION /**/ALL SELECT s_id, p_id, f_id, nbr + 1, ADD_MONTHS(ELIGIBLE_MONTH, 1) FROM t WHERE nbr <= 2 ) SELECT S_ID, P_ID, ELIGIBLE_MONTH FROM t
Published by: pollywog on December 10, 2010 15:57
-
Problem with connection by level
Hello
My desired goes something like this: I would like to generate the dates of hiredate to systdate with an interval of one month. The code that I have to return works well for single empno. I want to generate the dates of hiredate to sysdate with a gap
one month for all employees (empno). Commenting on the code to find out where empno = 7369 does not work and the system a lot of the CPU usage.
Hiredate were suppose to be to_date('01-jan-10') for empno = 7369
examples of code goes something like this:
code for the emp tableselect ADD_MONTHS(trunc(to_date('01-jan-10')),LEVEL-1) from dual connect by level <= trunc((sysdate-to_Date('01-jan-10'))/30 )
Please adviceselect ADD_MONTHS(trunc(hiredate),LEVEL-1) from employee where empno = 7369 connect by level <= trunc((sysdate-trunc(hiredate))/30 )
You should stay with double to generate numbers. You can use the caluase WITH to create a table that contains only the relevant dates.
The min max values for the datatab ann might be emp Subselects.
example not tested
with datatab as (select ADD_MONTHS(trunc(to_date('01-jan-10')),LEVEL-1) mon from dual connect by level <= trunc((sysdate-to_Date('01-jan-10'))/30 ) ) select * from employee e join datatab d on e.hiredate <=d.mon where empno = 7369;
Well, there are other ways to do it. Maybe you can provide some examples of data and also some of the desired output.
-
Dreamweaver offers a secure connection multi level feature?
Hello:
I am trying to create a connection so multi level when guest log in, they see the content specific to their level of play.
This http://www.kingluddite.com/tools/crud-in-dreamweaver will show you how to do what you want to do using deprecated technology.
Maybe you are looking for
-
whenever I run FireFox, he launched (opens) two sessions of the programme
I tried to uninstall and reinstall the latest version. Also deleted all of my shortcuts. Don't know what else to try.
-
Private browsing on right context menu on the shortcut icon of Mozilla's Firefox on MS Win XP
I am already aware that the Mozilla Firefox browser has the ability to always start Firefox in private browsing mode by putting the brand into its Options on... However, I noticed that on MS Win 7 OS, we can run Mozilla Firefox browser directly, by c
-
My dosnt mini iPad for upgrade to iOS 9.2.1
I have a mini iPad and its old software and I'm trying to upgrade to iOS 9.2.1 and he says that there is an error
-
Error: 0X0800C0133 with Outlook Express and cannot receive e-mail
An unknown error has occurred with my Outlook Express and I can't receive emails. It is erroe 0X0800C0133. Please I need help on how to remove it and fix my Email
-
is higher than windows xp windows vista Home premium
I need to download an application online with windows xp or higher and she wondered if vista is higher? :))