Tags: des io ar data Art sp on C SQLOracle Analytic functions --row_number (): 1234567 consecutive repetitions --rank (): Jump repeatable 12333678 --dense_rank (): Continuous repeatable 12333456 Row_number () over (partition by deptno ORDER BY Sal Desc) Over (partition by [partition field, can have multiple] order by [one or more fields]) The written questions are as follows: The topics are as follows: Use
function when using the subquery method, but more trouble, ashamed, I use a subquery some still can not find out.The syntax is as follows:Lag (value_expression [, offset] [, default]) over ([query_partition_clase] order_by_clause);Leads (value_expression [, offset] [, default]) over ([query_partition_clase] order_by_clause);whichValue_expression: Can be a field or a built-in function.Offset is a positive integer, which defaults to 1, indicating the number of records forward or backward. Because
7902 FORD 20 108759 7499 ALLEN 30 16007521 WARD 30 28507654 MARTIN 30 41007698 BLAKE 30 69507844 TURNER 30 84507900 JAMES 30 9400 Rank shows for example the next SELECT empno, ename, Deptno, Sal,rank () over (PARTITION by Deptno ORDER by Sal) as Deptpn_sal from EMP Results 1 7934 MILLER 10 1300.00 12 7782 CLARK 10 2450.00 23 7839 KING 10 5000.00 34 7369 SMITH 20 800.00 15 7876 ADAMS 20 1100.00 26 7566 JONES 20 2975.00 37 7788 SCOTT 20 3000.00 48 7902 FORD 20 3000.00 49 7900 JAMES 30 950.00 176
analytic functions provide a much quicker way to check gaps. They enable you to still see the next line (lead) or the previous line (LAG) values while using full, set-oriented SQL processing.
The following are the formats for these functions:
{lead | LAG} (value_expression, offset, default)
Over ([PARTITION by expr] order by expr)
Typically, a value_expression
Often have friends ask the row and column conversion problem, the message board also has such a question.
In fact, the use of analytic functions to deal with is a good way to turn over Tom's book, one of the examples included here. For example, to query the Scott.emp table of users Sal sort information, you can use the following query:
Sql> SELECT Deptno, ename,
2 row_number () over (PARTITION by deptno
3P088888888888 2016-02-11 3 4P088888888888 2016-02-14 3 5P088888888888 2016-02-12 1 6P088888888888 2016-02-10 1 7RANK and Dense_rank-rank () The rank of the data item in the grouping, the rank equal will leave the vacancy in the position-dense_rank () The rank of the data item in the grouping, the rank equal will not leave the vacancy in the rankSELECT Polno,Createtime,Pnum,RANK () over (PARTITION by Polno ORDER by pnum Desc) as RN1,Dense_rank () over (PARTITION by Polno ORDER by pnum Desc) as
Tags: src return inf same IMA body height img Share1. RANKThe--rank function returns a unique value that, when the same data is encountered, ranks the same as the last and next rank between the same data.Column:2, Dense_rank--Returns a unique value that, when the same data is encountered, ranks the same, encounters different data, does not empty the rank, but increments.Column:3, Row_number--return unique values, same data, rank different, increment, seeColumn:The role and difference of Oracle t
) rangebetweenInterval'2' DayPreceding andInterval'2' Dayfollowing) Five_day_avg fromCust_orderwhereSale_price is not NULL andOrder_dtbetweenTo_date ('01-jul-2001','dd-mon-yyyy') andTo_date ('31-jul-2001','dd-mon-yyyy')--displays sales for the current month, one months, one months, and average sales for every 3 monthsSelect Month, First_value (sum(Tot_sales)) Over (Order by MonthRowsbetween 1Preceding and 1following) Prev_month,sum(tot_sales) Monthly_sales,last_value (sum(Tot_sales)) Over (
(Rank, Dense_rank, Row_number) ①row_number:12345The row_number function returns a unique value that, when the same data is encountered, is incremented sequentially in the order in which the records are recorded in the recordset. ②dense_rank:12223The Dense_rank function returns a unique value unless all the same data is ranked the same at the time when the same data is encountered. ③rank:12225The rank function returns a unique value, except that when the same data is encountered, all the same dat
first line is unbounded preceding, and the current row is Currentrow, the last line is unbounded following. The window clause appears, and the ORDER BY clause must be specified, such as: Last_value (SAL) Over(Partition byDeptnoOrder bySal rowsbetweenunbounded preceding andunbounded following) The above example specifies that the window is the entire grouping. When the window clause is omitted: a) If an order by is present, the default window is unbounded preceding and CurrentROWB) If an order
t.dept_id, t.sale_date, t.goods_type, t.sale_cnt, SUMover ( ORDERbybetween and current ROW) all_cmp_cntfrom criss_sales TThe results are as follows:With this example, you can see that using analytic functions to solve problems in some scenarios is very intuitive and straightforward.What is an analytic function?
Label: analytic functions are a new concept introduced in Oracle 8.1.6, providing a simple and efficient way to analyze data. in the analysis function previously, we had to use a self-query, subquery, or inline view, or even a complex stored procedure implementation statement, now as long as a simple SQL statement can be achieved, and there is considerable improvement in the efficiency of implementatio
Oracle's analytic Functions row_number (), rank (), Dense_rank () usage and differencesFor example, to query the payroll 7th Employee information, you can use the analysis function to do.--Query Employee information for payroll 7thSELECT *From (select emp.*, rank () + (ORDER by sal Desc) rank from EMP)where rank = 7;SELECT *From (select emp.*, Dense_rank () Up (order by Sal Desc) Dr from EMP)where Dr = 7;SE
. Assume that the data is 10,9,9,8,8; rank from highest to lowest: 1,2,2,4,4--use as an aggregate function--Suppose a person has a salary of 15500, the following SQL can query 15500 of the payroll rankingsSelectRank15500) withinGroup(Order bysalary) fromemployees E;--use as an analysis function--check everyone's salary and salary rankingsSelectE.last_name, E.salary, e.manager_id, rank () Over(Order bye.salary) fromEmployees E;3.8 Row_number can only be used as an
There are two main analytic functions used in the work, one is sum () over (partition by ... order by ...) The other one is lead (lag) over (|partition By|order by)Sum does not need to say much, mainly by the partition by the field, the total, the return of a number of rows, rather than as a group by just return a row.The lead (lag) is a sort of order by, in the range within partition by, and down (up) to t
analytic functions provide a much quicker way to check gaps. They enable you to still see the next line (lead) or the previous line (LAG) values while using full, set-oriented SQL processing.
The following are the formats for these functions:
{lead | LAG} (value_expression, offset, default) over
([PARTITION to expr] order by expr)
Typically, a value_express
Tags: ACL tween pre from GES. com Understanding First SeleThe literal meaning of First_value () and Last_value () is intuitive, taking the values of the records.Example: Query department The earliest occurrence of sales record date and recent sales record date Select
dept_id
, Sale_date
, Goods_type
, sale_cnt
over byorder by sale_date) First_value
throughorderbydesc ) Last_value
from Criss_sales; Look at the results first_value () very intuitive, no more
Ratio_to_report--List Sales totals per month of the previous year, year-end sales, and monthly sales as a percentage of total annual sales--method ①:SelectAll_sales.*, - * round(Cust_sales/Region_sales,2)|| '%' Percent from (SelectO.CUST_NBR customer,o.region_id Region,sum(o.tot_sales) cust_sales,sum(sum(O.tot_sales)) Over(Partition byo.region_id) Region_sales fromorders_tmp owhereO. Year = 2001 Group byo.region_id, O.CUST_NBR) all_saleswhereAll_sales.cust_sales>All_sales.region_sales* 0.2;--m
--Check the salary rankings of all teachers--rank (): Same row with equal values, followed by rank jumpsSelect Tname,sal,Rank () Over (order by Sal) as salary rankFrom teacher--dense_rank (): The same row with equal values, followed by successive rankingsSelect Tname,sal,Dense_rank () Over (order by Sal) as salary rankingFrom teacher--row_number: The returned rank is contiguous regardless of the value equalitySelect Tname,sal,deptno,Row_number () Over (order by Sal) as salary rankingFrom teacher
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.