understand row_number() and its use in analytic function
Hello all;I've been playing with row_number and trying to figure out how to use it, and yet I still can't understand...
I have the following code below
create table Employee(
ID VARCHAR2(4 BYTE) NOT NULL,
First_Name VARCHAR2(10 BYTE),
Last_Name VARCHAR2(10 BYTE),
Start_Date DATE,
End_Date DATE,
Salary Number(8,2),
City VARCHAR2(10 BYTE),
Description VARCHAR2(15 BYTE)
)
insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
values ('01','Jason', 'Martin', to_date('19960725','YYYYMMDD'), to_date('20060725','YYYYMMDD'), 1234.56, 'Toronto', 'Programmer');
insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
values('02','Alison', 'Mathews', to_date('19760321','YYYYMMDD'), to_date('19860221','YYYYMMDD'), 6661.78, 'Vancouver','Tester')
insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
values('03','James', 'Smith', to_date('19781212','YYYYMMDD'), to_date('19900315','YYYYMMDD'), 6544.78, 'Vancouver','Tester')
insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
values('04','Celia', 'Rice', to_date('19821024','YYYYMMDD'), to_date('19990421','YYYYMMDD'), 2344.78, 'Vancouver','Manager')
insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
values('05','Robert', 'Black', to_date('19840115','YYYYMMDD'), to_date('19980808','YYYYMMDD'), 2334.78, 'Vancouver','Tester')
insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
values('06','Linda', 'Green', to_date('19870730','YYYYMMDD'), to_date('19960104','YYYYMMDD'), 4322.78,'New York', 'Tester')
insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
values('07','David', 'Larry', to_date('19901231','YYYYMMDD'), to_date('19980212','YYYYMMDD'), 7897.78,'New York', 'Manager')
insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
values('08','James', 'Cat', to_date('19960917','YYYYMMDD'), to_date('20020415','YYYYMMDD'), 1232.78,'Vancouver', 'Tester')
I made a simple select statementSelect * from employee
and it returns it below
ID FIRST_NAME LAST_NAME START_DAT END_DATE SALARY CITY DESCRIPTION
---- ---------- ---------- --------- --------- ---------- ---------- ---------------
01 Jason Martin 25-JUL-96 25-JUL-06 1234.56 Toronto Programmer
02 Alison Mathews 21-MAR-76 21-FEB-86 6661.78 Vancouver Tester
03 James Smith 12-DEC-78 15-MAR-90 6544.78 Vancouver Tester
04 Celia Rice 24-OCT-82 21-APR-99 2344.78 Vancouver Manager
05 Robert Black 15-JAN-84 08-AUG-98 2334.78 Vancouver Tester
06 Linda Green 30-JUL-87 04-JAN-96 4322.78 New York Tester
07 David Larry 31-DEC-90 12-FEB-98 7897.78 New York Manager
08 James Cat 17-SEP-96 15-APR-02 1232.78 Vancouver Tester
I wrote another select statement with row_number. See belowSELECT first_name, last_name, salary, city, description, id,
ROW_NUMBER() OVER(PARTITION BY description ORDER BY city desc) "Test#"
FROM employee
and I get this resultFirst_name last_name Salary City Description ID Test#
Celina Rice 2344.78 Vancouver Manager 04 1
David Larry 7897.78 New York Manager 07 2
Jason Martin 1234.56 Toronto Programmer 01 1
Alison Mathews 6661.78 Vancouver Tester 02 1
James Cat 1232.78 Vancouver Tester 08 2
Robert Black 2334.78 Vancouver Tester 05 3
James Smith 6544.78 Vancouver Tester 03 4
Linda Green 4322.78 New York Tester 06 5
I understand the partition by which means basically for each associated group a unique number wiill be assigned for that row, so in this case since tester is one group, manager is another group, and programmer is another group then tester gets its own unique number for each row, manager as well and etc.
What is throwing me, that's the order of and how these numbers are assigned. Why is1 assigned to Alison Mathews for the tester group and 2 assigned to James Cat and assigned 3 Robert Black
I apologize if this is a stupid question, I tried to read about this online and looking at the oracle documentation, but still do not understand why.
user13328581 wrote:
What is throwing me, that's the order of and how these numbers are assigned. Why is1 assigned to Alison Mathews for the tester group and 2 assigned to James Cat and assigned 3 Robert Black
Description (partition by column) and city (control column) values are same for Alison, James and Robert, no sort order of these 3 records be held valid.
Oracle just happened to choose a. What do you think should be the numbering "correct"?
Tags: Database
Similar Questions
-
Oracle 11g Release 2
I'm assuming that the best solution is the use of analytical functions.
create table test3 ( part_type_id varchar2(50) ,group_id number ,part_desc_id number ,part_cmt varchar2(50) ) / insert into test3 values( 'ABC123',1,10,'comment1'); insert into test3 values( 'ABC123',1,10,'comment2'); insert into test3 values( 'ABC123',2,15,'comment1'); insert into test3 values( 'ABC123',2,15,'comment2'); insert into test3 values( 'EFG123',25,75,'comment3'); insert into test3 values( 'EFG123',25,75,'comment4'); insert into test3 values( 'EFG123',25,75,'comment5'); insert into test3 values( 'XYZ123',1,10,'comment6'); insert into test3 values( 'XYZ123',2,15,'comment7'); commit; select * from test3; PART_TYPE_ID GROUP_ID PART_DESC_ID PART_CMT -------------------- ---------- ------------ -------------------- ABC123 1 10 comment1 ABC123 1 10 comment2 ABC123 2 15 comment1 ABC123 2 15 comment2 EDG123 25 75 comment3 EDG123 25 75 comment4 EDG123 25 75 comment5 XYZ123 1 10 comment6 XYZ123 2 15 comment7 9 rows selected. Desired output: PART_TYPE_ID GROUP_ID PART_DESC_ID PART_CMT -------------------- ---------- ------------ -------------------- ABC123 1 10 comment1 ABC123 2 15 comment1 XYZ123 1 10 comment1 XYZ123 2 15 comment2 RULE: where one part_type_id has multiple (2 or more distinct combinations) of group_id/part_desc_id NOTE: There are about 12 columns in the table, for brevity I only included 4.
Post edited by: orclrunner was updated desired output and rule
Hello
Here's one way:
WITH got_d_count AS
(
SELECT part_type_id, group_id, part_desc_id
MIN (part_cmt) AS min_part_cmt
COUNT AS d_count (*) OVER (PARTITION BY part_type_id)
OF test3
GROUP BY part_type_id, group_id, part_desc_id
)
SELECT DISTINCT
group_id, part_desc_id, part_type_id, min_part_cmt
OF got_d_count
WHERE d_count > 1
;
Output:
GROUP_ID PART_DESC_ID MIN_PART_CMT PART_TYPE_ID
------------ ---------- ------------ ------------
ABC123 1 10 comment1
ABC123 2 15 comment1
XYZ123 1 10 comment6
XYZ123 2 15 comment7
Analytical functions, such as the COUNTY and MIN, many global versions, in addition, it can give the same results. Use the analytical versions when each row of output corresponds to exactly 1 row of input and the aggregate and GROUP BY version when each line of output corresponds to a group of lines 1 or more input. In this issue, each line of output appears to be a group of input lines having the same group_id, part_type_id, and part_desc_id (I'm guessing just, this only has never stated), so I used GROUP BY to get 1 row of output for every input lines.
-
SQL using the analytic function
Hi allI want a help in the creation of my SQL query to retrieve the data described below:
I have a test of sample table containing data as below:
State ID Desc
MICHAEL 1 T1
ACTIVE 2 T2
T3 3 SUCCESS
DISABLE THE T4 4
The thing I want to do is to select all the lines with an ACTIVE status in the table but is there is no ACTIVE status, my request will give me the last line with MICHAEL status.
I can do this in a single request by using the analytical function for example, if yes can yiu help me on the request of unpacking.
Kind regards
Raluce
Something like that?
I had to fix it.
with testdata until)
Select 1 id, "T1" dsc "DISABLED" status of Union double all the
Select 2 id, 'T2' dsc, the status "ACTIVE" of all the double union
Select id 3, "T3" dsc, the status of 'SUCCESS' of all the double union
Select 4 id, "T4" dsc "DISABLED" status of double
)Select
ID
dsc
status
of testdata
where
status =
-case when (select count (*) in testdata where status = 'ACTIVE') > 0
then 'ACTIVE '.
Another 'DISABLED '.
end
and)
ID in (select id from testdata where status = ' ACTIVE')
or
ID = (select max (id) in testdata when status = 'DISABLED')
)STATE ID DSC
'2' 'T2' 'ACTIVE '.
Maybe it's more efficient
Select
ID
dsc
status
of testdata
where
status =
-case when (select count (*) in testdata where status = 'ACTIVE') > 0
then 'ACTIVE '.
Another 'DISABLED '.
end
and
ID =)
-case when (select count (*) in testdata where status = 'ACTIVE') > 0
then id
on the other
(select max (id) in testdata when status = 'DISABLED')
end
)Post edited by: correction of chris227
Post edited by: chris227
extended -
Hi gurus,
I ask someone to enlighten me on the analytical functions.
I used the following query:
The sample result set is:select * from (select filename,count(regexp_substr(filename,'.*era'))over( partition by regexp_substr(filename,'.*era') ) cnt FROM l_x12n_835_fileinfo where partitionnum=76000 and clntsysnum=76500 and radt>='01-JAN-2010') where cnt>1
Now, I changed the query to:FILENAME CNT rsmedcalwa.20100105.chpwr.072.era 4 rsmedcalwa.20100105.chpwr.072.era.1 4 rsmedcalwa.20100105.chpwr.072.era.2 4 rsmedcalwa.20100105.chpwr.072.era.3 4 rsmedcalwa.20100105.chpwr.081.era 3 rsmedcalwa.20100105.chpwr.081.era.1 3 rsmedcalwa.20100105.chpwr.081.era.2 3 rsmedcalwa.20100106.chpwr.088.era 3 rsmedcalwa.20100106.chpwr.088.era.1 3 rsmedcalwa.20100106.chpwr.088.era.2 3 rsmedcalwa.20100108.chppr.363.era.3 4 rsmedcalwa.20100108.chppr.363.era.1 4 rsmedcalwa.20100108.chppr.363.era.2 4 rsmedcalwa.20100108.chppr.363.era 4
The result set has beenselect * from (select filename,count(regexp_substr(filename,'.*era'))over( partition by regexp_substr(filename,'.*era') order by filename ) cnt FROM l_x12n_835_fileinfo where partitionnum=76000 and clntsysnum=76500 and radt>='01-JAN-2010') where cnt>1
(1) I don't understand how the addition of the order by clause changes the count. Could someone explain please?FILENAME CNT rsmedcalwa.20100105.chpwr.072.era.1 2 rsmedcalwa.20100105.chpwr.072.era.2 3 rsmedcalwa.20100105.chpwr.072.era.3 4 rsmedcalwa.20100105.chpwr.081.era.1 2 rsmedcalwa.20100105.chpwr.081.era.2 3 rsmedcalwa.20100106.chpwr.088.era.1 2 rsmedcalwa.20100106.chpwr.088.era.2 3 rsmedcalwa.20100108.chppr.363.era.1 2 rsmedcalwa.20100108.chppr.363.era.2 3 rsmedcalwa.20100108.chppr.363.era.3 4 rsmedcalwa.20100112.chpwr.175.era.1 2 rsmedcalwa.20100112.chpwr.175.era.2 3
When I change this order by order of regexp_substr(filename,'.*era'), it gives me the correct number.
My requirement is to check how many similar file names I.
(2) if there are any other better elsewhere, please let me know.Hello
Analytical functions still carried on in a window of the the result set, which can be smaller than the result set.
If you have a PARTITION BY clause, the window for each row includes rows with the same values from all the PARTITION BY expressions.
If you have an ORDER BY clause, the window includes that of a section of consecutive lines in the score, as defined by a windowing clause (in other words, LINES or KEEP them). The default value is "RANGE BETWEEN UNBOUNDED PRECEDING AND LINE CURRENT.In your case, if the analytical clause is:
over( partition by regexp_substr(filename,'.*era') )
and the line you are looking at a filename = 'rsmedcalwa.20100105.chpwr.072.era.2', the window takes up the entire partition, in other words, all lines whose name includes "rsmedcalwa.20100105.chpwr.072.era". In other words, the window is identical to the partition, because theree is no ORDER BY clause.
But if you add an ORDER BY clause:
over( partition by regexp_substr(filename,'.*era') order by filename )
then the window (potentially) decreases. Since there is no clause window, the default value 'RANGE BETWEEN UNBOUNDED PRECDING AND a CURRENT LINE' is used, which means that only the lines with a lower or equal to "rsmedcalwa.20100105.chpwr.072.era.2" file name (using the normal string comparison) will affect the results.
The analytical ORDER BY clause is required for certain features (such as ROW_NUMBER) significance only in regard to certain commands. For the most part fucntions (including COUNTY), the ORDER BY clause is optional, and you do not have to use a. In this case, it seems that you do not want the effect of a smaller than the partition window, just so, do not use an ORDER byclause of Analytics for this function.
Remember, the analytical ORDER byclause is completely independent the ORDER BY query clause. If you want the result presented in a certain order, use an ORDER BY clause at the end of the query. It will not change the results of the analytical functions.
-
How to achieve this using an analytic function - please help
version 10g.
This code works very well with my requirement. I'm tyring learn analytical functions and implement than in the query below. I tried to use row_number,
but I could nt achieve the desired results. Please give me some ideas.
Thank youSELECT c.tax_idntfctn_nmbr irs_number, c.legal_name irs_name, f.prvdr_lctn_iid FROM tax_entity_detail c, provider_detail e, provider_location f, provider_location_detail pld WHERE c.tax_entity_sid = e.tax_entity_sid AND e.prvdr_sid = f.prvdr_sid AND pld.prvdr_lctn_iid = f.prvdr_lctn_iid AND c.oprtnl_flag = 'A' AND c.status_cid = 2 AND e.oprtnl_flag = 'A' AND e.status_cid = 2 AND (c.from_date) = (SELECT MAX (c1.from_date) FROM tax_entity_detail c1 WHERE c1.tax_entity_sid = c.tax_entity_sid AND c1.oprtnl_flag = 'A' AND c1.status_cid = 2) AND (e.from_date) = (SELECT MAX (c1.from_date) FROM provider_detail c1 WHERE c1.prvdr_sid = e.prvdr_sid AND c1.oprtnl_flag = 'A' AND c1.status_cid = 2) AND pld.oprtnl_flag = 'A' AND pld.status_cid = 2 AND (pld.from_date) = (SELECT MAX (a1.from_date) FROM provider_location_detail a1 WHERE a1.prvdr_lctn_iid = pld.prvdr_lctn_iid AND a1.oprtnl_flag = 'A' AND a1.status_cid = 2)
Published by: new learner on May 24, 2010 07:53
Published by: new learner on May 24, 2010 10:50Can be like that not tested...
Select *.
Of
(SELECT c.tax_idntfctn_nmbr irs_number, c.legal_name irs_name,
f.prvdr_lctn_iid, c.from_date as c_from_date, max (c.from_date) more (partition c.tax_entity_sid) as max_c_from_date,
e.from_date as e_from_date, max (e.from_date) more (partition e.prvdr_sid) as max_e_from_date,
PLD.from_date as pld_from_date, max (pld.from_date) more (pld.prvdr_lctn_iid partition) as max_pld_from_dateOF tax_entity_detail c,.
e provider_detail
provider_location f,
LDP provider_location_detail
WHERE c.tax_entity_sid = e.tax_entity_sid
AND e.prvdr_sid = f.prvdr_sid
AND pld.prvdr_lctn_iid = f.prvdr_lctn_iid
AND c.oprtnl_flag = 'A '.
AND c.status_cid = 2
AND e.oprtnl_flag = 'A '.
AND e.status_cid = 2
AND pld.oprtnl_flag = 'A '.
AND pld.status_cid = 2) X
where c_from_date = max_c_from_date AND e_from_date = max_e_from_date AND
pld_from_date = max_pld_from_date -
Nth salary using the analytic function
I use under function to calculate second highest with empno and deptno salary.
Is it possible to get the same result with another query without using Assembly only analytical functions condition.using and windows function is possible to get the desired output?
SELECT e.empno,
e.DEPTNO,
tmp. SAL as second_higher_salary
FROM emp e,.
(SELECT Empno,
DEPTNO,
SAL,
DENSE_RANK() (PARTITION BY deptno ORDER of sal) AS rnk
WCP
) tmp
WHERE tmp.deptno = e.deptno
and tmp.rnk = 2
EMPNO DEPTNO SAL
---------- ---------- ----------
7934 10 2450
7782 10 2450
7839 10 2450
7876 20 1100
7369 20 1100
7902 20 1100
7788 20 1100
7566 20 1100
7900 30 1250
7844 30 1250
7654 30 1250
7521 30 1250
7499 30 1250
7698 30 1250
7900 30 1250
7844 30 1250
7654 30 1250
7521 30 1250
7499 30 1250
7698 30 1250
Here's my solution:
Select empno,
DEPTNO,
FIRST_VALUE (sal) (PARTITION BY deptno ORDER by sal desc)
de)
SELECT EmpNo,
DEPTNO,
Decode (DENSE_RANK () OVER (PARTITION BY deptno order by sal desc), 1,-sal, sal) sal
WCP
)
/
EMPNO DEPTNO FIRST_VALUE (SAL) OVER (PARTITIONBYDEPTNOORDERBYSALDESC) ---------- ---------- -----------------------------------------------------
7782 10 2450 7934 10 2450 7839 10 2450 7566 20 2975 7876 20 2975 7369 20 2975 7788 20 2975 7902 20 2975 7499 30 1600 7844 30 1600 7654 30 1600 7521 30 1600 7900 30 1600 7698 30 1600 -
Determine the maximum length of a column and its use in the same query?
I would like to determine the maximum length of a column and use it in the same query. Is this possible? IE:
SELECT RPAD (last_name, SELECT MAX (LENGTH (last_name)) + 5 FROM user, '-')
OF the user.
Thank youHello
Welcome to the forum!
jfraley wrote:
I would like to determine the maximum length of a column and use it in the same query. Is this possible? IE:SELECT RPAD (last_name, SELECT MAX (LENGTH (last_name)) + 5 FROM user, '-')
OF the user.Thank you
Sure. You almost go in your message: a scalar subquery . Just put brackets around the subquery:
SELECT RPAD ( last_name , ( SELECT MAX (LENGTH (last_name)) + 5 FROM user_tbl -- USER is not a good table name ) , '-' ) AS padded_last_name FROM user_tbl ;
Scalar subqueries in SQL are like tape in your garage in canvas: they are used for thousands of different things and open, maybe 1% of them. Usually, there are better ways to achieve the same results, such as the analytical functions:
SELECT RPAD ( last_name , 5 + MAX (LENGTH (last_name)) OVER () , '-' ) AS padded_last_name FROM user_tbl ;
Still another way is to make the subquery and then join his game as if it were a table of results:
WITH got_max_length AS ( SELECT MAX (LENGTH (last_name)) AS max_length FROM user_tbl ) SELECT RPAD ( u.last_name , m.max_length + 5 , '-' ) AS padded_last_name FROM user_tbl u CROSS JOIN got_max_length m ;
Published by: Frank Kulash, December 12, 2011 21:17
Added cross join example -
by using the analytical function to get the right output.
Hello all;
I have the following date of sample below
It's the output I wantcreate table temp_one ( id number(30), placeid varchar2(400), issuedate date, person varchar2(400), failures number(30), primary key(id) ); insert into temp_one values (1, 'NY', to_date('03/04/2011', 'MM/DD/YYYY'), 'John', 3); insert into temp_one values (2, 'NY', to_date('03/03/2011', 'MM/DD/YYYY'), 'Adam', 7); insert into temp_one values (3, 'Mexico', to_date('03/04/2011', 'MM/DD/YYYY'), 'Wendy', 3); insert into temp_one values (4, 'Mexico', to_date('03/14/2011', 'MM/DD/YYYY'), 'Gerry', 3); insert into temp_one values (5, 'Mexico', to_date('03/15/2011', 'MM/DD/YYYY'), 'Zick', 9); insert into temp_one values (6, 'London', to_date('03/16/2011', 'MM/DD/YYYY'), 'Mike', 8);
Any help is appreciated. I'll post my request as soon as I can think of a good logic for this...placeid issueperiod failures NY 02/28/2011 - 03/06/2011 10 Mexico 02/28/2011 - 03/06/2011 3 Mexico 03/14/2011 - 03/20/2011 12 London 03/14/2011 - 03/20/2011 8
Hello
user13328581 wrote:
... Please note, I'm still learning how to use analytical functions.It doesn't matter; analytical functions will not help in this problem. The SUM aggregate function is all you need.
But what do you need to GROUP BY? What is the value of each row of the result will represent? A placeid? Yes, each line will represent only placedid, but it will be divided further. You want a separate line of the output for each placeid and every week, then you'll want of the week and GROUP BY placeid. You don't want to GROUP BY the raw issuedate; that would put on 3 March and 4 March in separate groups. And you don't want to GROUP BY failures; This would mean that a line with 3 failures could never be in the same group in line with 9 failures.This becomes the output you posted from the sample data you posted:
SELECT placeid , TO_CHAR ( TRUNC (issuedate, 'IW') , 'MM/DD/YYYY' ) || ' - '|| TO_CHAR ( TRUNC (issuedate, 'IW') + 6 , 'MM/DD/YYY' ) AS issueperiod , SUM (failures) AS sumfailures FROM temp_one GROUP BY placeid , TRUNC (issuedate, 'IW') ;
You can use a subquery to calculate TRUNC (issuedate, 'IW') once. The code would be of about as complicated, efficiency probably will not improve substantially and the results would be the same.
-
[8i] can I use an analytical function, or do I need a subquery?
Hi all...
This should be a quick. I hope I can solve my problem with an analytic function, but I don't know if it's possible. Can I use a subquery if I have to, but I'd really rather not.
Here is a very simple version of what I'm trying to do:
I want the results:CREATE TABLE test123 ( field1 VARCHAR2(10) , field2 VARCHAR2(10) , my_date DATE ); INSERT INTO test123 VALUES ('value1', 'a',TO_DATE('12/31/1900','mm/dd/yyyy')); INSERT INTO test123 VALUES ('value1', 'b',TO_DATE('01/02/2010','mm/dd/yyyy')); INSERT INTO test123 VALUES ('value1', 'c',TO_DATE('01/05/2010','mm/dd/yyyy')); INSERT INTO test123 VALUES ('value2', 'a',TO_DATE('12/31/1900','mm/dd/yyyy')); INSERT INTO test123 VALUES ('value2', 'b',TO_DATE('01/01/2010','mm/dd/yyyy')); INSERT INTO test123 VALUES ('value2', 'c',TO_DATE('01/15/2010','mm/dd/yyyy'));
I started with the following query:FIELD1 FIELD2 -------------- value2 a value2 b value2 c value1 a value1 b value1 c
But the problem is the database has a date of 31 December 1900 ' as default / initial for any date field. I don't want these default values taken into account in my calculation of min. I tried to put a WHERE clause in my analytical function [WHERE my_date <>TO_DATE (' 12/31/1900 ',' mm/dd/yyyy')], but I kept getting an error message "missing right parenthesis", so it seems that you can not have a WHERE clause here... or I'm just something wrong?SELECT field1 , field2 FROM test123 ORDER BY MIN(my_date) OVER ( PARTITION BY field1 ) -- removed DESC here , field2
Moreover, it is a 8i database...
Edited by: user11033437 may 20, 2010 17:16: took the 'DESC' criteria out of my order by clause. In my real application, I need DESC, but not the example.Hello
A WHERE clause excludes rows in the results set. Whenever you want you can have a WHERE clause that was more limited (for example, something that would simply exclude MIN calculating values in the ORDER BY clause), then think CASE:
SELECT field1 , field2 FROM test123 ORDER BY MIN ( CASE WHEN my_date > TO_DATE ( '12/31/1900' , 'MM/DD/YYYY' ) THEN my_date END ) OVER (PARTITION BY field1) DESC , field2 ;
This puts the lines for "Value1" first.
The minimum my_date for "Value1" (after excluding the values of 1900) is later than the minimum for "Value2", so I think that you either made a mistake in the desired output, or you do not want sorted by descending order.As always, thanks for the display of the data of the sample and the results so clearly.
-
Using the analytical function.
Hello
I have this scenario.
We have two locations for a given item_id. Primary and in bulk.with t as ( select 21009 item_id,9 primary_available,450 max_qty,100 bulk_available,12122 bulk_locator_id from dual union all select 21009 item_id,9 primary_available,450 max_qty,2775 bulk_available,8704 bulk_locator_id from dual union all select 21009 item_id,9 primary_available,450 max_qty,524 bulk_available,15614 bulk_locator_id from dual union all select 21009 item_id,9 primary_available,450 max_qty,3300 bulk_available,15654 bulk_locator_id from dual) select t.* from t;
I'm trying to get a select statement out of this point of view, where I will be restock the primary AMOUNT of sites in bulk, BUT the smaller bulk first. Once she gets up, I shouldn't take more product.
There is an analytic function that would do this?
That's the max I could come up with.
So, in this scenario, I want to replen bulk_locator_id 100 ' 12122 'and ' 15614 bulk_locator_id 341'. That's all. ZERO of the other rentals (bulk_locator_id). If the question is not clear, please let me know.with t as ( select 21009 item_id,9 primary_available,450 max_qty,100 bulk_available,12122 bulk_locator_id from dual union all select 21009 item_id,9 primary_available,450 max_qty,2775 bulk_available,8704 bulk_locator_id from dual union all select 21009 item_id,9 primary_available,450 max_qty,524 bulk_available,15614 bulk_locator_id from dual union all select 21009 item_id,9 primary_available,450 max_qty,3300 bulk_available,15654 bulk_locator_id from dual) select t.*, max_qty - (primary_available + SUM(bulk_available) over(PARTITION BY item_id ORDER BY bulk_available)) replen_this_much from t;
Published by: RPuttagunta on September 11, 2009 16:23Hello
Thanks for posting the sample data.
It would be useful that you also posted the output you want. Is this?. BULK_ REPLEN_ ITEM_ PRIMARY_ MAX_ BULK_ LOCATOR_ THIS_ ID AVAILABLE QTY AVAILABLE ID MUCH ----- ---------- ----- ---------- ---------- ---------- 21009 9 450 100 12122 100 21009 9 450 524 15614 341 21009 9 450 2775 8704 0 21009 9 450 3300 15654 0
If so, you can get to this:
SELECT t.* , GREATEST ( 0 , LEAST ( TO_NUMBER (bulk_available) , TO_NUMBER (max_qty) - ( TO_NUMBER (primary_available) + NVL ( SUM (TO_NUMBER (bulk_available)) OVER ( PARTITION BY item_id ORDER BY TO_NUMBER (bulk_available) ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING ) , 0 ) ) ) ) AS replen_this_much FROM t ORDER BY item_id , TO_NUMBER (bulk_available) ;
You should really store your numbers in NUMBER of columns.
You essentially posted all what you need analytical functions. The problem was just wrapping this analytical function (or something very close to it) and LESS and more GRAND, so that the replen_this_much column is always between 0 and TO_NUMBER (bulk_available).
-
By using the analytical function to sort without showing this column in the result.
Hello
We use the Oracle 11.2
How to use Oracle Analytics to sort the output of a query without showing this column in the result?
Here's my query:
Select distinct nvl(SRC_CHR_NM,'0') | » #'|| NVL(EXPL_SRC,'0') | » #'|| NVL(DIM_NM,'0') | » #'|| NVL(DIM_CHR_NM,'0') | » #'|| NVL(DIM_CHR_ABR,'0') | » #'||
Decode (InStr (NVL(SRC_COL_NM,'0'), 'trim ('),'1 ', Replace (NVL(SRC_COL_NM,'0'),'trim (',' trim(PRM_DTL.'), '0',' PRM_DTL.)))) » || NVL(SRC_COL_NM,'0')) AS ALLOFIT,
DIM_NM
from EXPL_CONFIG where SBJ_AREA_CD = 'PRMDTL. '
I want to use analytical to sort by DIM_NM but I do not want to show. I want to just just the ALLOFIT column to show in the output.
Something like "row_number() over (order by DIM_NM desc).
Thank you!
Hello
If you SELECT SEPARATE, then you can only ORDER OF things in the SELECT clause. (Do you really need to do SELECT DISTINCT? Often, it's just an inefficient way to remove the lines that do not need to be there at all).
Move making the SELECT DISTINCT in a subquery and make the ORDER BY (and nothing else) in the main query.
I hope that answers your question.
If this isn't the case, please post a small example data (CREATE TABLE and only relevant columns, INSERT statements) and also publish outcomes from these data.
Explain, using specific examples, how you get these results from these data.
Always say what version of Oracle you are using (for example, 11.2.0.2.0).
See the FAQ forum: https://forums.oracle.com/message/9362002#9362002
-
Re: How to change its use of the function keys?
The function keys on my laptop P870 are keys to double action. You must press the FN key at the same time as the key function F1, F2. In particular, I use the F2 key a lot but have to use 2 hands (unless you have extremely long fingers) to operate the function keys driving me crazy.
Any suggestions?
If you use open Office assist Win8 > system settings > keyboard and function keys mode change.
Change of cell function special mode reboot and test how it works. -
make changes to a file, I don't create and now used to the function key
I had to change the dimensions of the file so I am not changing the button actions. So, it worked before, and now im in CC host trying to get it to work again. Any ideas how to check where the url I need to go to url google.org
Here's the AS3
to import flash.external.ExternalInterface;
// =========================== CLICKTAG STUFF ==================================
// =============================================================================
ct.addEventListener (MouseEvent.CLICK, handleClick);
ct.addEventListener (MouseEvent.ROLL_OVER, handleMouseOver);
ct.addEventListener (MouseEvent.ROLL_OUT, handleMouseOut);
function handleClick(e:MouseEvent):void {}
ExternalInterface.call("saveglobalscore",score) ("ctaClick");
trace ("clicked... clicktag");
/ * var flashVars:Object = LoaderInfo (this.root.loaderInfo) .parameters;
If {(flashVars.clickTag)
navigateToURL (new URLRequest (flashVars.clickTag), "_blank");
}
trace ("clicked"); * /
}
function handleMouseOver(e:MouseEvent):void {}
cta.cta.gotoAndPlay ("OVER");
cta.gotoAndPlay ("over");
}
function handleMouseOut(e:MouseEvent):void {}
cta.gotoAndPlay ("out");
}
do not change the fla.
Use the original html code and change the object tag:
width = "550" height = "400" id = 'test' style = "float: none;" ' vertical-align: middle ">
-
Thanks in advance to anyone who might help
I am writing a storage report. Try to calculate the use of space over a period of time. I need for a db_nm, tblsp_nm find the collections of the first and the last, and then find the difference in the space_used
The structure of the table is like this
Expected result isdrop table tstg / create table tstg (db_nm varchar(10), tblsp_nm varchar(15), space_used number, collection_time date) / insert into tstg values ( 'EDW', 'SYSTEM',100,to_date('01/07/2011','DD/MM/YYYY')); insert into tstg values ( 'EDW', 'SYSTEM',120,to_date('05/07/2011','DD/MM/YYYY')); insert into tstg values ( 'EDW', 'SYSTEM',150,to_date('10/07/2011','DD/MM/YYYY')); insert into tstg values ( 'EDW', 'SYSAUX',10,to_date('01/07/2011','DD/MM/YYYY')); insert into tstg values ( 'EDW', 'SYSAUX',12,to_date('05/07/2011','DD/MM/YYYY')); insert into tstg values ( 'EDW', 'SYSAUX',15,to_date('10/07/2011','DD/MM/YYYY')); commit;
I useDB_NM TBLSP_NM SPACE_USED COLLECTIO DIFF ---------- --------------- ---------- --------- ---------- EDW SYSAUX 15 10-JUL-11 5 EDW SYSTEM 150 10-JUL-11 50
but gives more lines in the result I wantselect db_nm,tblsp_nm,space_used,collection_time, last_value(space_used) OVER (partition by DB_NM,Tblsp_nm order by collection_time ASC) - first_value(space_used) OVER (partition by DB_NM,Tblsp_nm order by collection_time ASC) diff from tstg
/DB_NM TBLSP_NM SPACE_USED COLLECTIO DIFF ---------- --------------- ---------- --------- ---------- EDW SYSAUX 10 01-JUL-11 0 EDW SYSAUX 12 05-JUL-11 2 EDW SYSAUX 15 10-JUL-11 5 EDW SYSTEM 100 01-JUL-11 0 EDW SYSTEM 120 05-JUL-11 20 EDW SYSTEM 150 10-JUL-11 50
Thank you
EduardoHello
Thanks for the sample data.
Here's a solution using the FIRST/LAST functions:
select db_nm , tblsp_nm , max(collection_time) as collection_time , max(space_used) keep (dense_rank last order by collection_time) as space_used , max(space_used) keep (dense_rank last order by collection_time) - max(space_used) keep (dense_rank first order by collection_time) as diff from tstg group by db_nm , tblsp_nm ;
-
Warning quota IMAP but I have cleaned already and its use is very low
I get a warning of Quota IMAP (101%) on one of my gmail email accounts.
I read the articles on cleaning and avoid Quota IMAP problems and have done what I can delete and move e-mails.
But my space usage report shows that I use only 36MB for this account which seems really small.
(my main Gmail account using 8.8 GB and I do not receive a warning.)what I missed or where can I turn?
Peter
Gmail will also save all e-mails, both sent and received in 'all mail '.
Deletion of an e-mail from a folder does not result removes the email in "all mail".
Basically, all letters, incoming and outgoing are saved in the folder "all messages".
Labels can be applied to create a visual effect to appear as in other folders, for example: Inbox or one that you create.So to delete an e-mail from for example: Inbox simply deletes the "Inbox" label and still leaves the email in "all messages".
Maybe you are looking for
-
How to get rid of the ask search toolbar?
I am troubled by the ask search toolbar, it is difficult to get rid of it, I also ask for this help, can someone give me a link removal guide But he needs to download sth, if anyone knows if it is reliable? and my OS is windows 8. Help, please! null
-
Portege M750/10J - touch screen problem
All, I have the Tablet above and the touch screen works intermittently, The used stylus is that magnetic with the right click feature Someone at - it met similar problems? Kind regards
-
How to clear a duplication?
I use a MacBook Pro OS X Retina El Capitan and I accidentally copied 'documents' on my desk and I put it in 'my files' so I have all my (many) duplicate photos in "my files". How can erase just replicas? Thank you Ory92140
-
Question about memory timmings on the Qosmio F50-10
Hello I need help here. I just bought this Qosmio F50 - 10A, which comes with 4 GB of ddr2 at 800 mhz. The problem is that the timmings memory are defined for CAs 6. But memories can do since reported 5 cases of cpu - z.I think a simple bios update s
-
I get this message every time I get an email with a link and then can not click on the link. This happens on any email I receive. Just got the message. I am running Vista. I don't understand any of this message. If anyone can help me in simple terms.