Help! -partition by clause
Hello. I have the following tableTable: item_tracker
Date | item_code | begin_price
end of each day, a record for each item is written in this table. now, after the records are written, I want to calculate the change start price
the formula is change_in_price =(todays_begin / yesterdays_begin) * 100
I wrote the following query, but it doesn't seem to work. Please advise me on what I do wrong... also the approach is the problem?
Please notify.
Select date, item_code, begin_price, first_value (begin_price) over (partition by date, trunc item_code order (date) desc lines between 2 above and 1 previous) as yesterdays_begin
of item_tracker
Here, the partition column does not display any value.
is there a way I can include the date? (as a 'having clause' for group, what is the method to divide by)
I tried vary as well. He reurned the error that I need to mention a numeric value instead of the date to check a range.
Please help me :(
Thanks in advance
First, it is not advisable to use names such as 'date' for columns
Then, you must use the 'delay' analytical function in this case: see http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/functions070.htm
select
date,
item_code,
begin_price,
lag(begin_price) over (partition by item_code order by date) as yesterdays_begin
from item_tracker
Kind regards
Sayan M.
Tags: Database
Similar Questions
-
Request Cross-listing (Partition by Clause? Self-join?)
Hello
I need a query that cross-list will be a teacher teaches this semester of courses. Essentially, both fields must be the same (i.e.: CourseTitle & Section), while the third field is different (for example: subject).
For example, Max Power is a Professor teaching the 3 course, one co-inscrit (123 ENG and JRL 123):
The result of the desired query is:LastName FirstName Subject Section CourseTitle Power Max ENG 123 English Composition Power Max ENG 452 Robert Frost Poetry Power Max JRL 123 English Composition Power Max ENG 300 Faulkner & Twain
Basically, I don't need that the courses co-inscrit in the output. Is this an instance where I use a "Partition by Clause' or should I create a self-join?LastName FirstName Subject Section CourseTitle Power Max ENG 123 English Composition Power Max JRL 123 English Composition
Well thanks for all help and comments.Hello
user11137889 wrote:
Unfortunately, I can't create tables. I don't have permission. I can't modify, add or remove all the data.It is no possible to work! You should have your own schema in a database of development, perhaps on your own machine, where you can create tables freely. You can develop or test software effectively without this ability.
You must post CREATE TABLE and INSERT to your sample data, even if you can't test. Without this, all anyone can do is guess.If you want to only count each topic once, then replace COUNT (*) by COUNT (DISTINCT subject). Here, 'subject' can be almost any kind of expression, for example:
count( DISTINCT substr ( ssbsect_ptrm_code , 1 , 1 ) ) over ( partition by ssbsect_crse_numb , scbcrse_title ) cnt
-
How many fields we can add to the inside on the partition by clause?
Hello world
My version of DB is
BANNER ---------------------------------------------------------------- Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - 64bi PL/SQL Release 10.2.0.1.0 - Production CORE 10.2.0.1.0 Production TNS for Linux: Version 10.2.0.1.0 - Production NLSRTL Version 10.2.0.1.0 - Production
How many fields we can add to the inside on the partition by clause? I have added two and tried it works. So how can we add?
Please, share your ideas.
Kind regards
BS2012.
What is the purpose of this question? You can add as many as you have in the table.
----
Ramin Hashimzade
-
Oracle version 8i (we will migrate the db to 12 c in the next few months)
This is defination and data table:
create table sales
(
prod_id varchar2 (10),
PROD_NAME varchar2 (30),
salesman_id varchar2 (10),
salesman_name varchar2 (30),
commission_per varchar2 (10),
commission_amt varchar2 (10)
)
/
Insert in the value of sales ('prd1',' 1 ', ' 1', 'sm1' ", '10');
Insert in the value of sales ('prd1',' 1 ', ' 2', 'sm2' ", '10');
Insert in the value of sales ('prd1',' 1 ', ' 3', 'sm3' ", '10');
Insert in the value of sales ('2 ', 'prd2', '1', 'sm1', '10.00' ");
Insert in the value of sales ('2 ', 'prd2', '2', 'sm2', '10.00' ");
Insert in the value of sales ('3 ', 'prd3', '2', 'sm2', '10.00' ");
Insert in the value of sales ('3 ', 'prd3', '1', 'sm1', '15,00' ");
Insert in the value of sales ('3 ', 'prd3', '3', 'sm3', '10.00' ");
Insert in the value of sales ('4 ', 'prd4', '1', 'sm1', '10.00' ");
Insert in the value of sales ('4', 'prd4', ' 2 ', 'sm2' ", '15');
Insert in the value of sales ('5', 'prd5', ' 1 ', 'sm1' ", '15');
Insert in the value of sales ('6', 'prd6', ' 1 ', 'sm1' ", '15');
Insert in the value of sales ('6', 'prd6', ' 2 ', 'sm2' ", '15');
Insert in the value of sales ('6', 'prd6', ' 3 ',' sm3 ', 15', ");
Insert in the value of sales ('6', 'prd6', ' 4 ',' sm4 ', 15', ");
Insert in the value of sales ('7', 'prd7', ' 1 ', 'sm1' ", '15');
Insert in the value of sales ('7', 'prd7', ' 2 ', 'sm2' ", '15');
Insert in the value of sales ('7', 'prd7', ' 3 ', 'sm3' ", '15');
Insert in the value of sales ('7', 'prd7', ' 4 ', 'sm4' ", '15');
Insert in the value of sales ('7', 'prd7', ' 7 ', 'sm7' ", '15');
Insert in the value of sales ('7', 'prd7', ' 5 ', 'sm1' ", '10');
Insert in the value of sales ('7', 'prd7', ' 6 ', 'sm6' ", '15');
Dress uniform please:
Unique Commision Amount/Commission Percentage: PROD_ID PROD_NAME SALESMAN_I SALESMAN_NAME COMMISSION COMMISSION ---------- ------------------------------ ---------- ------------------------------ ---------- ---------- 1 prd1 1 sm1 10 2 prd2 1 sm1 10.00 5 prd5 1 sm1 15
Means, we must identify the product ID for the business that gives the same percentage of commission sales amount or commission. Percentage of different Commission amount and the Commission:
Different Commision Amount/Commission Percentage: PROD_ID PROD_NAME SALESMAN_I SALESMAN_NAME COMMISSION COMMISSION ---------- ------------------------------ ---------- ------------------------------ ---------- ---------- 3 prd3 2 sm2 10.00 3 prd3 1 sm1 15.00 3 prd3 3 sm3 10.00 4 prd4 1 sm1 10.00 4 prd4 2 sm2 15 6 prd6 1 sm1 15 6 prd6 2 sm2 15 6 prd6 3 sm3 15 6 prd6 4 sm4 15 7 prd7 1 sm1 15 7 prd7 2 sm2 15 7 prd7 3 sm3 15 7 prd7 4 sm4 15 7 prd7 7 sm7 15 7 prd7 5 sm1 10 7 prd7 6 sm6 15
Means, for the highest of the product ID, assigned amount/percentage of different sales commission to sellers; that is for a product id, there is same amount/percentage of commission which would come at the first exit, if these lines of product codes should be in the second output. Kindly help me, since we use very old version, so I think that we cannot use analytics or new SQL functions. So, please help me in a simple sql. Thank you.
I hope that this works in version 8i.
SQL > select prod_id
2, prod_name
3, salesman_id
4, salesman_name
5, commission_per
6, commission_amt
7 of)
8. Select s.*
9, mod (nvl (sum (commission_per) on (prod_id partition), 0), count (*) on (prod_id partition)) +.
10 mod (nvl (sum (commission_amt) on (prod_id partition), 0), count (*) on (prod_id partition)) val
11, row_number() over (partition by prod_id arrested by salesman_id) rno
12 s sale
13 )
14 where val = 0
15 and rno = 1;PROD_ID PROD_NAME SALESMAN_I SALESMAN_NAME COMMISSION COMMISSION
---------- ------------------------------ ---------- ------------------------------ ---------- ----------
1 prd1 1 sm1 10
2 prd2 1 sm1 10.00
5 prd5 1 sm1 15SQL > select prod_id
2, prod_name
3, salesman_id
4, salesman_name
5, commission_per
6, commission_amt
7 of)
8. Select s.*
9, mod (nvl (sum (commission_per) on (prod_id partition), 0), count (*) on (prod_id partition)) +.
10 mod (nvl (sum (commission_amt) on (prod_id partition), 0), count (*) on (prod_id partition)) val
11, row_number() over (partition by prod_id arrested by salesman_id) rno
12 s sale
13 )
14 where val! = 0 ;PROD_ID PROD_NAME SALESMAN_I SALESMAN_NAME COMMISSION COMMISSION
---------- ------------------------------ ---------- ------------------------------ ---------- ----------
3 prd3 2 sm2 10.00
3 prd3 1 sm1 15.00
3 prd3 3 sm3 10.00
4 prd4 1 sm1 10.00
4 prd4 2 sm2 15
6 prd6 2 sm2 15
6 prd6 1 sm1 15
6 prd6 4 sm4 15
6 prd6 3 sm3 15
7 prd7 7 sm7 15
7 prd7 5 sm1 10
7 prd7 4 sm4 15
7 prd7 3 sm3 15
7 prd7 6 sm6 15
7 prd7 1 sm1 15
7 prd7 2 sm2 1516 selected lines.
SQL >
-
I have a query as below
Select empno, deptno, sum (sal) on tot_sal (partition deptno) from scott.emp;
will give the result like this
EMPNO DEPTNO TOT_SAL
---------- ---------- ----------
7934 10 8750
7782 10 8750
7839 10 8750
7902 20 10875
7876 20 10875
7566 20 10875
7369 20 10875
7788 20 10875
8 selected lines.
but as you can see the total sal and deptno repeats, what I want is wise dept total salary as shown below
EMPNO DEPTNO TOT_SAL
---------- ---------- ----------
7934
7782
7839
Total for deptno 10 = 8750
7902
7876
7566
7369
7788
Total for deptno = 20 10875
I want to use the type clause and get the above result970426 wrote:
Karthik repliedPublished by: 970426 on January 24, 2013 04:13
You are not yet able to determine good projection on your own?
select nvl(to_char(empno),'Total for deptno '||deptno||': '||sal) empno from emp model partition by (deptno) dimension by (row_number() over( partition by deptno order by empno) rn) measures (sal, empno) rules( sal[0]=sum(sal) [any] ) order by deptno, empno
Katrick solution uses my idea of deptno [0]. It has a useless subquery, a useless measure, two unnecessary rules.
Please don't give any of my points.
Jiust learn a thing about it.
Introduction to thinking on your own. -
Helps the model clause. Date field in the Dimension
Hello, all.
I'm having a problem with the clause type in my query. In my view, it may be a misunderstanding on my part, but I can't find any documentation that is opposed to the use of the date fields in the dimensions of my model.
Given these data;
I write my SQL like this:LOGDATE SHIFTNAME BUILDING UNIT DOORNUMBER INOUT EVENTCOUNT 2012-02-01 06:00:00 A 1800 R Door 4 Out 14 2012-02-01 06:00:00 A 1800 R Door 4 In 15 2012-02-01 18:00:00 D 1800 R Door 4 Out 17 2012-02-01 18:00:00 D 1800 R Door 4 In 19 2012-02-02 06:00:00 A 1800 R Door 4 Out 14 2012-02-02 06:00:00 A 1800 R Door 4 In 14 ...
And I get the expected result. (The same table as above) But when I try to add rows using rules, I get no new line until the LOGDATE field is a date. For example, by using this code, I expect to get a new line, with a 100 EVENTCOUNT (and other fields as written) but I stiill get the same set of data (no line is added)SELECT logdate, shiftname, building, unit, doornumber, inout, eventcount FROM door_events WHERE building='1800' AND unit='R' AND doornumber=4 MODEL DIMENSION BY (logdate, shiftname, building, unit, doornumber, inout) MEASURES (eventcount) RULES ()
However, when I turn the LOGDATE field in a CHAR data type (and make the SYSDATE a TANK in the same way) with the code below, all of a sudden I get the line I was expecting!SELECT logdate, shiftname, building, unit, doornumber, inout, eventcount FROM door_events WHERE building='1800' AND unit='R' AND doornumber=4 MODEL DIMENSION BY (logdate, shiftname, building, unit, doornumber, inout) MEASURES (eventcount) RULES (eventcount[SYSDATE,'X','1800','R',4,'In'=100)
Can someone explain this behavior? I intend to do some calculations on the LOGDATE dimension and I would like to keep it as a date. Thanks in advance for any help!SELECT TO_CHAR(logdate,'YYYY-MM-DD HH24:MI:SS') logdate, shiftname, building, unit, doornumber, inout, eventcount FROM door_events WHERE building='1800' AND unit='R' AND doornumber=4 MODEL DIMENSION BY (logdate, shiftname, building, unit, doornumber, inout) MEASURES (eventcount) RULES (eventcount[TO_CHAR(SYSDATE,'YYYY-MM-DD HH24:MI:SS'),'X','1800','R',4,'In'=100) LOGDATE SHIFTNAME BUILDING UNIT DOORNUMBER INOUT EVENTCOUNT 2012-02-01 06:00:00 A 1800 R Door 4 Out 14 2012-02-01 06:00:00 A 1800 R Door 4 In 15 2012-02-01 18:00:00 D 1800 R Door 4 Out 17 2012-02-01 18:00:00 D 1800 R Door 4 In 19 2012-02-02 06:00:00 A 1800 R Door 4 Out 14 2012-02-02 06:00:00 A 1800 R Door 4 In 14 ... 2012-02-07 15:18:33 X 1800 R Door 4 In 100
I do not get the same result. When I use SYSDATE, I don't get the extra line. When I use TO_CHAR (SYDATE...) as you say, I still don't get the extra line.
However, when I use a constant date, such as TO_DATE (' 2012/02/07 02:00 ',' YYYY-MM-DD HH24:MI:SS'), then I get the extra line.
This behavior is described in http://docs.oracle.com/cd/E11882_01/server.112/e25554/sqlmodel.htm#BEIGGGFJ
«Using UPSERT creates a new cell corresponding to that referenced on the left-hand side of the rule when the cell is absent, and the cell reference contains only positional references * qualified constants *.»
Best regards, stew Ashton
P.S. "Oracle Database 11g Enterprise Edition Release 11.2.0.2.0.
Published by: stew Ashton on 7 February 2012 23:03
-
Hi all
Could someone please help me on this one where clause.
The structure of the table like this:
< code >
create table (test)
primary key ID number,
TYPE_ID number not null,
name varchar2 (10));
insert into test values (1, 10, 'name 1');
insert into test values (2, 10, 'name 2');
insert into test values (3, 20, 'name 3');
insert into test values (4, 20, 'name 4');
insert into test values (5, 20, 'name 5');
insert into test values (6, 10, 'name 6');
insert into test values (7, 10, 'name 7');
commit;
SQL * more > select * from test;
TYPE_ID ID NAMES
---------- ---------- ----------
1 10 name 1
2 10 2 name
3 20 name 3
4 20 4 name
5 20 5 name
6 10 6 name
7 10 7 name
7 selected lines.
< code >
Here are the values of the drop-down list box which the user will be able to select
0 all the project
Type of project 10 10
20 of the Type 20
Based on the selection of the user, we need a SELECT to get all records or certain types of records.
Thanks in advance!Hello
If: selected_type_ID is the value in the drop-down list (0, 10, or 20), then:
WHERE :selected_type_id IN (test.type_id, 0)
This assumes that test.type_id is never NULL.
-
Help with the clause of "default".
Hi all
I have a scenario where there is a table with 75 columns. out of the 75 columns, 30 are varchar and 30 are digital. The remaining columns are other types of data when creating the table, that I forgot to mention the default values to varchar and numeric columns.
Now, approximately 20,000 records are inserted into the table where some records contain NULL varchar and numeric. Now, I want to replace the null with varchar values and digital default values respectively.
I can change the columns by adding the clause by default now and update the lines of NULL for the default values accordingly. But this process could be very heavy.
Is there a better way to do this. Please help me.
Thanks in advance,
RambeauI want to know that is it possible to set the default values for all columns of type varchar, both using a single SQL statement. Then you can update all the values NULL with default values.
You are looking for something like this?
SQL> create table t (empno default 1, ename default 'x') as select cast(null as integer) empno, cast(null as varchar2(10)) ename from emp where rownum <= 4 / Table created. SQL> select * from t / EMPNO ENAME ---------- ---------- 4 rows selected. SQL> update t set empno = default, ename=default / 4 rows updated. SQL> select * from t / EMPNO ENAME ---------- ---------- 1 x 1 x 1 x 1 x 4 rows selected.
-
Hello world
I have an argument to function entry, lets call this file number. This argument can be 0 (zero) or any number like 9800. No negative number.
My where clause, I would like to say, if the number is 0 (zero), then extract all the cases, otherwise just retrieve the lines/records that belong to the passed in file number.
select * from table1 where case_num = (if case num is 0 then retrieve all cases, otherwise retrieve only the records that belong to the number passed in)
Thanks in advance.
Select *.
FROM table1
where case_num = num
or num = 0;
SY.
-
I have a temp table as below
LOC X CNT
20 20 C
80 20 N
60 30 C
40 30 N
100 40 N
the rule is for each column of the LOC, there are 2 records of C, N and sum (NTC) must be = 100. If there is no value for C for a LOC (LOC = 40) then the value of the CNT corresponding to C should appear as null or 0 if the value of N is > 0.
I need a model clause to do this, see below,
LOC X CNT
20 20 C
80 20 N
60 30 C
40 30 N
100 40 N
0 TO 40 C
Why why why? I am not able to understand the model clause. How did you learn model clause?
-
With the help of the clause...
Hello
I try to use the With clause to preset some values in the table - as temporary structures. Currently, I need to create two of these tables called Param1 and Param2. I want this is to select than a Params array and make it have 2 fields. I get an error of parenthesis wrong however...
Work with two "tables".
-------------------
with param1 as
(select sum (stm.qty_rec) inco
stock stm
WHERE pnm_auto_key = 1
GROUP BY pnm_auto_key
ORDER BY pnm_auto_key),
param2 as
(select Select sum (saj.qty_adj)
of stock_adjust saj
join in-house stock stm on saj.stm_auto_key = stm.stm_auto_key
parts_master MFN on stm.pnm_auto_key = pnm.pnm_auto_key inner join
where pnm_auto_key = 1
Group of pnm_auto_key)
Select (INCO + SELECT) in param1, param2
-------------------
Does not not (with params), select (...)-> invalid table name
----------------
with params as
(select sum (stm.qty_rec) INCO
stock stm
WHERE pnm_auto_key = 1
GROUP BY pnm_auto_key
ORDER BY pnm_auto_key),
(select sum (saj.qty_adj) SELECT
of stock_adjust saj
join in-house stock stm on saj.stm_auto_key = stm.stm_auto_key
parts_master MFN on stm.pnm_auto_key = pnm.pnm_auto_key inner join
where pnm_auto_key = 1
Group of pnm_auto_key)
Select params (INCO + SELECT)
-----------------
Does not (with params (select), (double) select...)-> lack of right parenthesis
-------------------
with params as
(select (select sum (stm.qty_rec)
stock stm
WHERE pnm_auto_key = 1
GROUP BY pnm_auto_key
ORDER BY pnm_auto_key) INCO.
(select sum (saj.qty_adj)
of stock_adjust saj
join in-house stock stm on saj.stm_auto_key = stm.stm_auto_key
parts_master MFN on stm.pnm_auto_key = pnm.pnm_auto_key inner join
where pnm_auto_key = 1
(Pnm_auto_key group) SELECT DOUBLE)
Select params (INCO + SELECT)
-------------------for example
with params as (select (select sum(stm.qty_rec) from stock stm WHERE pnm_auto_key = 1) INCO, (select sum(saj.qty_adj) from stock_adjust saj inner join stock stm on saj.stm_auto_key = stm.stm_auto_key inner join parts_master pnm on stm.pnm_auto_key = pnm.pnm_auto_key where pnm_auto_key = 1) OUTG FROM dual ) select (INCO + OUTG) from params
-
Help - >; Partition by report SQL
Our users have a problem with the formula on the reports below. I don't know what that means. Our users have said that they need
(Tons used (s) / spread miles) * 2000 lb/ton = thousand lbs/spread for their report.
And I see below the formula used by our developer. Don't know how to make for good reading. Can you let me know the formula used in the code below could in fact represent the formula above. Users complained as below
user-> column double and triple the amount, if there are multiple records for the same truck/driver/beat/with the same amount for used dry carpet and Miles to spread. If miles of spread or used dry carpet is different if calculated correctly.
Published by: Lucy discover March 28, 2011 13:50Have you tried my suggestion above?
(i.e. without using the SUM)
-
[10g] need help with order by hierarchical query clause
I have the following data samples:
And the following query gives me exactly what I want, in the order I want. However, I wonder if there is a way to get this order without creating the column SEQ, given that I don't need in my resultsCREATE TABLE bill_test1 ( parent_part CHAR(25) , child_part CHAR(25) , line_nbr NUMBER(5) , qty_per NUMBER(9,5) ); INSERT INTO bill_test1 VALUES ('ABC-1','ABC-10',100,1); INSERT INTO bill_test1 VALUES ('ABC-1','ABC-20',200,2); INSERT INTO bill_test1 VALUES ('ABC-1','ABC-30',300,3); INSERT INTO bill_test1 VALUES ('ABC-1','HARDWARE-1',401,10); INSERT INTO bill_test1 VALUES ('ABC-1','HARDWARE-2',402,5); INSERT INTO bill_test1 VALUES ('ABC-10','ABC-155',100,2); INSERT INTO bill_test1 VALUES ('ABC-10','HARDWARE-1',200,1); INSERT INTO bill_test1 VALUES ('ABC-155','RAW-2',100,4.8); INSERT INTO bill_test1 VALUES ('ABC-155','HARDWARE-3',200,3); INSERT INTO bill_test1 VALUES ('ABC-20','RAW-1',100,10.2); INSERT INTO bill_test1 VALUES ('ABC-30','RAW-3',100,3);
SELECT part_nbr , parent_part , child_part FROM ( SELECT CONNECT_BY_ROOT b.parent_part AS part_nbr , b.parent_part , b.child_part , SYS_CONNECT_BY_PATH(b.line_nbr,' ') AS seq FROM bill_test1 b , dual CONNECT BY parent_part = PRIOR child_part ) WHERE part_nbr = 'ABC-1' ORDER BY seq ; Results of above query, except with SEQ included in SELECT (just to show what I'm sorting off of): PART_NBR PARENT_PART CHILD_PART SEQ ---------------------------- ---------------------------- ---------------------------- ----------------- ABC-1 ABC-1 ABC-10 100 ABC-1 ABC-10 ABC-155 100 100 ABC-1 ABC-155 RAW-2 100 100 100 ABC-1 ABC-155 HARDWARE-3 100 100 200 ABC-1 ABC-10 HARDWARE-1 100 200 ABC-1 ABC-1 ABC-20 200 ABC-1 ABC-20 RAW-1 200 100 ABC-1 ABC-1 ABC-30 300 ABC-1 ABC-30 RAW-3 300 100 ABC-1 ABC-1 HARDWARE-1 401 ABC-1 ABC-1 HARDWARE-2 402
Hello
As long as there is that a single root, brothers and SŒURS of ORDER BY, you say, but you can not do in a subquery (well, you can, but usually there is no interest in a subquery). If the CONNECT BY in a subquery, there is no guarantee that the main request will preserve the hierarchical order which provides the subquery.
The query you posted does not require a query of Tahina, so you can say:
SELECT CONNECT_BY_ROOT b.parent_part AS part_nbr , b.parent_part , b.child_part --, SYS_CONNECT_BY_PATH(b.line_nbr,' ') AS seq FROM bill_test1 b WHERE CONNECT_BY_ROOT b.parent_part = 'ABC-1' CONNECT BY parent_part = PRIOR child_part ORDER SIBLINGS BY b.line_nbr ;
I said the query you posted does not require a subquery. It also does not require double, so I guess that what you posted is a simplification of what you are really doing, and that could have a subquery. In particular, if you want to GROUP BY part_nbr, you need the subquery. We can use CONNECT_BY_ROOT expression in the WHERE clause (or, come to think of it, use a START WITH clause instead WHERE), but, for some reason, we cannot use CONNECT_BY_ROOT in a clause GROUP BY; We need to calculate CONNECT_BY_ROOT in a subquery, give it a name (like part_nbr) and Super GROUP OF this column in a query.
This requires that there is that one node root. ORDER OF brothers and SŒURS means just that: children of a common parent will appear in the order, but the root nodes, which have no parents, may not be in order.
Here's what I meant by using START WITH place WHERE:
SELECT CONNECT_BY_ROOT b.parent_part AS part_nbr , b.parent_part , b.child_part --, SYS_CONNECT_BY_PATH(b.line_nbr,' ') AS seq FROM bill_test1 b START WITH b.parent_part = 'ABC-1' CONNECT BY parent_part = PRIOR child_part ORDER SIBLINGS BY b.line_nbr ;
This should be much more effective, because it reduces the results before waste you your time by getting their descendants.
With the help of a clause START WITH here is analogous to me sending you an email, saying "come to a meeting one my office at 03:00."
By using a WHERE clause here is similar by sending me an e-mail to all members of society, saying: "come to a meeting one my office at 3:00" and then, as people get here, tell everyone except you could go back.Brothers and SŒURS ORDER BY was introduced in Oracle 9.
Published by: Frank Kulash, December 9, 2010 14:39
Added version with the START WITH clause -
How to use the clause type conditionally create new lines
This question is just for learning - no object other than to start real world to create versions of some queries known to learn how to measure the performance of the MODEL clause clause of the MODEL versions.
The question is: How can you write a STANDARD clause that pivots (updates) ONLY the lines that actually need to rotate and do NOT change the lines that have no value and cannot be rotated.
I found a good site that has about two dozen example articles on different pieces. This first link is the beginning of the series.
SQL features tutorials - Clause TYPE
http://www.sqlsnippets.com/en/topic-11663.html
This link is to a clause of FAQ of BluShadow MODEL version "how to convert rows to columns."
Line - MODEL method string
http://www.sqlsnippets.com/en/topic-11987.html
The solution to this link uses this line of source:
POSITION KEY VAL
--- ---------- ----------
R08 0 v1, v2, v3,.
and this result set:
POSITION KEY VAL
--- ---------- ----------
R08 1 v1
R08 2 v2
R08 3 v3
The reason why it produces that result set is this clause of the statement of MODEL
RETURN THE UPDATED LINES
If you comment on this article, you will see that the original lines are all in position 0. This means that the solution reproduced EACH SOURCE LINE even if there is only one item in the list of values.
Thus, even a line source with a single value (for example, the r01 'a') will be updated and updated this line is returned.
For a large number of data sources with only a few lines that actually need to rotate it would be a great performance of infringement.
This is the sample data source
with t as (button 1, 'a' value 'abc' col2, col3 'def' of the double
Select Union all 2, 'b', 'ghi', 'jkl' from dual
Union all select 3, 'c, d, e, 'mno', 'pqr' from dual.
Union all select 4, 'f', 'stu', 'vwx' from dual
)
Select the key, value, col2, col3
tKEY, VALUE, COL2, COL3
1, a, abc, def
2, b, GHI, jkl
3, "c, d, e, mno, pqr.
4, f, stu, vwxLines 1, 2 and 4 only have a value of (a, b, c respectively.
Only line 3 must rotate. It contains "c, d, e" and which must become 3 lines in the result set
Value of the key, col2 col3
1 an abc def
2B ghi jkl
3 c mno, pqr
3 d mno, pqr
3 e mno, pqr
4 f stu vwxThe nut of the problem is that if the clause 'RETURN UPDATED ROWS' is used, then the lines 1, 2 and 4 must be updated in order to be returned in the result set. And if this clause is NOT used, then line 3 must be updated by 'c', d, e 'c' and two new product lines: one line for a ' and one for 'e '.
How can you do this with the TYPE clause without the help of the clause "Return LINES UPDATE"? I don't have a solution to sample showing what I tried because I can't understand what it takes to even try.
I have a solution that uses "RETURN UPDATED ROWS" but I want to compare this performance to the exercise, when this clause is NOT used.
We ask BluShadow to add the solution of MODEL clause to this FAQ with other similar solutions.
Like this?
SQL > with t as
() 2
3 select the 1 key, 'a' val, 'abc' col2, col3 'def' of the double
Select 4 Union all 2, 'b', 'ghi', 'jkl' from dual
5 union all select 3, 'c, d, e, 'mno', 'pqr' from dual.
Select 6 Union all the 4 'f', 'stu', 'vwx' from dual
7)
8. Select the key
9, key_1
10, val
11, regexp_substr (val, ' [^,] +', 1, key_1) val_new
12, col2
13, col3
14 t
model 15
16 partition by (key)
dimension (1 key_1) 17
18 measures (val, col2, col3, (length (val) - length (replace (val, ",")) + 1) as len)
19 rules
(20)
21 val [for 1 to increment of len key_1 [1] 1] = val [1]
22, col2 [for 1 to increment of len key_1 [1] 1] = col2 [1]
23, col3 [for 1 to increment of len key_1 [1] 1] = col3 [1]
24)
25 order
26 by key
27, key_1;KEY KEY_1 VAL VAL_N NECK NECK
---------- ---------- ----- ----- --- ---
1 1 has an abc def
2 b 1 b ghi jkl
3 1 c, d, e c mno pqr
3 2 c, d, e d mno pqr
3 3 c, d, e e mno pqr
4 1 f f stu vwx6 selected lines.
-
Hi, I need help with a query that has a running total which resets at certain points
Version
examples of dataBANNER -------------------------------------------------------------------------------- Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production PL/SQL Release 11.2.0.3.0 - Production CORE 11.2.0.3.0 Production TNS for Linux: Version 11.2.0.3.0 - Production NLSRTL Version 11.2.0.3.0 - Production
It's close.create table t ( stat varchar2(3) , beg_dt date , end_dt date , dur number); Insert into t values ('ACT',to_date('01-JUL-2012','DD-MON-YYYY'),to_date('31-JUL-2012','DD-MON-YYYY'),31); Insert into t values ('ACT',to_date('01-AUG-2012','DD-MON-YYYY'),to_date('31-AUG-2012','DD-MON-YYYY'),31); Insert into t values ('ACT',to_date('01-SEP-2012','DD-MON-YYYY'),to_date('30-SEP-2012','DD-MON-YYYY'),30); Insert into t values ('LOA',to_date('01-OCT-2012','DD-MON-YYYY'),to_date('25-OCT-2012','DD-MON-YYYY'),25); Insert into t values ('LOA',to_date('26-OCT-2012','DD-MON-YYYY'),to_date('31-OCT-2012','DD-MON-YYYY'),6); Insert into t values ('LOA',to_date('01-NOV-2012','DD-MON-YYYY'),to_date('04-NOV-2012','DD-MON-YYYY'),4); Insert into t values ('ACT',to_date('05-NOV-2012','DD-MON-YYYY'),to_date('10-NOV-2012','DD-MON-YYYY'),6); Insert into t values ('LOA',to_date('11-NOV-2012','DD-MON-YYYY'),to_date('30-NOV-2012','DD-MON-YYYY'),20); Insert into t values ('LOA',to_date('01-DEC-2012','DD-MON-YYYY'),to_date('15-DEC-2012','DD-MON-YYYY'),15); Insert into t values ('ACT',to_date('16-DEC-2012','DD-MON-YYYY'),to_date('31-DEC-2012','DD-MON-YYYY'),16); Insert into t values ('LOA',to_date('01-JAN-2013','DD-MON-YYYY'),to_date('31-JAN-2013','DD-MON-YYYY'),31); Insert into t values ('LOA',to_date('01-FEB-2013','DD-MON-YYYY'),to_date('05-FEB-2013','DD-MON-YYYY'),5); Insert into t values ('LOA',to_date('06-FEB-2013','DD-MON-YYYY'),to_date('15-FEB-2013','DD-MON-YYYY'),10); Insert into t values ('LOA',to_date('16-FEB-2013','DD-MON-YYYY'),to_date('25-FEB-2013','DD-MON-YYYY'),10); Insert into t values ('LOA',to_date('26-FEB-2013','DD-MON-YYYY'),to_date('28-FEB-2013','DD-MON-YYYY'),3); Insert into t values ('LOA',to_date('01-MAR-2013','DD-MON-YYYY'),to_date('10-MAR-2013','DD-MON-YYYY'),10); Insert into t values ('ACT',to_date('11-MAR-2013','DD-MON-YYYY'),to_date('31-MAR-2013','DD-MON-YYYY'),21); Insert into t values ('ACT',to_date('01-APR-2013','DD-MON-YYYY'),to_date('30-APR-2013','DD-MON-YYYY'),30); Insert into t values ('LOA',to_date('01-MAY-2013','DD-MON-YYYY'),to_date('25-MAY-2013','DD-MON-YYYY'),25); Insert into t values ('LOA',to_date('26-MAY-2013','DD-MON-YYYY'),to_date('31-MAY-2013','DD-MON-YYYY'),6); Insert into t values ('LOA',to_date('01-JUN-2013','DD-MON-YYYY'),to_date('04-JUN-2013','DD-MON-YYYY'),4); Insert into t values ('ACT',to_date('05-JUN-2013','DD-MON-YYYY'),to_date('30-JUN-2013','DD-MON-YYYY'),26);
I need a total of stat LOA who is recovering with each Act break to get me:select stat , beg_dt , end_dt , dur , sum( case stat when 'LOA' then dur else 0 end ) over ( partition by stat order by beg_dt) tot1 from t order by beg_dt STAT BEG_DT END_DT DUR TOT1 ---- ----------- ----------- --- ---- ACT 01-JUL-2012 31-JUL-2012 31 0 ACT 01-AUG-2012 31-AUG-2012 31 0 ACT 01-SEP-2012 30-SEP-2012 30 0 LOA 01-OCT-2012 25-OCT-2012 25 25 LOA 26-OCT-2012 31-OCT-2012 6 31 LOA 01-NOV-2012 04-NOV-2012 4 35 ACT 05-NOV-2012 10-NOV-2012 6 0 LOA 11-NOV-2012 30-NOV-2012 20 55 LOA 01-DEC-2012 15-DEC-2012 15 70 ACT 16-DEC-2012 31-DEC-2012 16 0 LOA 01-JAN-2013 31-JAN-2013 31 101 LOA 01-FEB-2013 05-FEB-2013 5 106 LOA 06-FEB-2013 15-FEB-2013 10 116 LOA 16-FEB-2013 25-FEB-2013 10 126 LOA 26-FEB-2013 28-FEB-2013 3 129 LOA 01-MAR-2013 10-MAR-2013 10 139 ACT 11-MAR-2013 31-MAR-2013 21 0 ACT 01-APR-2013 30-APR-2013 30 0 LOA 01-MAY-2013 25-MAY-2013 25 164 LOA 26-MAY-2013 31-MAY-2013 6 170 LOA 01-JUN-2013 04-JUN-2013 4 174 ACT 05-JUN-2013 30-JUN-2013 26 0
Any help would be appreciated, thanks.STAT BEG_DT END_DT DUR TOT1 ---- ----------- ----------- --- ---- ACT 01-JUL-2012 31-JUL-2012 31 0 ACT 01-AUG-2012 31-AUG-2012 31 0 ACT 01-SEP-2012 30-SEP-2012 30 0 LOA 01-OCT-2012 25-OCT-2012 25 25 LOA 26-OCT-2012 31-OCT-2012 6 31 LOA 01-NOV-2012 04-NOV-2012 4 35 ACT 05-NOV-2012 10-NOV-2012 6 0 LOA 11-NOV-2012 30-NOV-2012 20 20 LOA 01-DEC-2012 15-DEC-2012 15 35 ACT 16-DEC-2012 31-DEC-2012 16 0 LOA 01-JAN-2013 31-JAN-2013 31 31 LOA 01-FEB-2013 05-FEB-2013 5 36 LOA 06-FEB-2013 15-FEB-2013 10 46 LOA 16-FEB-2013 25-FEB-2013 10 56 LOA 26-FEB-2013 28-FEB-2013 3 59 LOA 01-MAR-2013 10-MAR-2013 10 69 ACT 11-MAR-2013 31-MAR-2013 21 0 ACT 01-APR-2013 30-APR-2013 30 0 LOA 01-MAY-2013 25-MAY-2013 25 25 LOA 26-MAY-2013 31-MAY-2013 6 31 LOA 01-JUN-2013 04-JUN-2013 4 35 ACT 05-JUN-2013 30-JUN-2013 26 0
Hello
So you want a total separate whenever there is an "Act". It is what PARTITION BY. You must add the number of acts already encountered (which I called act_cnt) to the PARTITION BY clause that gets you the SUM.
We can get act_cnt using the analytical functions such as COUNT or SUM, but, given an analytic function cannot be dependent on another calculated the same query, we need to use a subquery to calculate act_cnt.Here's one way:
WITH got_act_cnt AS ( SELECT stat, beg_dt, end_dt, dur , COUNT (CASE WHEN stat = 'ACT' THEN 1 END) OVER (ORDER BY beg_dt) AS act_cnt FROM t ) SELECT stat, beg_dt, end_dt, dur , SUM (CASE stat WHEN 'LOA' THEN dur ELSE 0 END) OVER ( PARTITION BY stat , act_cnt -- ***** NEW ***** ORDER BY beg_dt ) AS totl FROM got_act_cnt ORDER BY beg_dt ;
Are 'Act' and 'AOL' the only possible values for the stat? If so, the query above can probably be simplified.
Maybe you are looking for
-
Is it possible that I can do my URL of the Mexico look like, this is a URL we?
We live in Tulum, the Mexico. An AppleTV box allows us to watch TV shows and movies from Netflix and iTunes. A few days, Apple has added content providers more to their AppleTV - suppliers like HBOGo. CBS, Showtime, ABC, HULU, etc. When I tried to si
-
APPCRASH explorer.exe hpswp_BHO.dll
Signature of the problem Additional information about the problem Bucket ID: 1048022001 Problem event name: APPCRASH Application name: explorer.exe Application version: 6.0.6001.18164 Application timestamp: 4907e242 Fault Module name: hpswp_BHO.dll F
-
I need space on my hard drive. Can I uninstall these programs 5.3 HP area picture 1) and (2) Extended this... My uninstall program said I used these programs about 2 years ago.
-
I did a Windows 'backup system image"when first of all, I got my new computer and put it on a hard drive internal, designated as"D", which was intended to be used for data. I tried to do a restore, and although very "fussy" has been able to do. The i
-
Message of Smartphones from blackBerry on facebook
Hi everyone I have a blackberry Curve 8320 and when I post a message on a wall of friends on my mobile, it is said that persons privacy settings prevented the update if anyone can tell me what this error is, or there's an error on facebook as have no