SQL analytic functions learning

First, the analysis function syntax:

function_name(<argument>,<argument>...) over(<partition_Clause><order by_Clause><windowing_Clause>);

function_name (): function name

argument: parameter

over (): windowing function

partition_Clause: partition clause, packet data records, group by ...

order by_Clause: Sort clause sets of data records to sort, order by ...

windowing_Clause: windowing clause, defining a set of analytic functions in the operation of row three windows in: rows, range, Specifying

Note: When using the window must be sorted clause clause! ! !

Second, the analysis function is as follows:

1, () over () count: the number of rows in each group statistics partitions, partition by alternative, order by optional

1, select name, sex, age , count (*) over () from emp; - total number of 
2, select name, sex, age , count (*) over (order by age) from emp; - incrementing count 
3, select name, sex, age , count (*) over (partition by sex) from emp; - packet count 
4, select name, sex, age , count (*) over (partition by sex order by age) from emp ; - packet count a sliding scale

2, sum () over (): the sum of the statistical records of the partition, partition by alternatively, order by the optional

1, select name, sex, age , sum (salary) over () from emp; - total cumulative sum 
2, select name, sex, age , sum (salary) over (order by age) from emp; - Delivery plus the cumulative sum 
3, select name, sex, age , sum (salary) over (partition by sex) from emp; - packet accumulated summation 
4, select name, sex, age , sum (salary) over (partition by sex order by age) from emp; - incrementing a packet accumulated sum

3, avg () over (): the statistical mean value recorded in the partition, partition by alternatively, order by the optional

1, select name, sex, age , avg (salary) over () from emp; - average total 
2, select name, sex, age , avg (salary) over (order by age) from emp; - incrementing averaging 
3, select name, sex, age , avg (salary) over (partition by sex) from emp; - averaged packet 
4, select name, sex, age , avg (salary) over (partition by sex order by age) from emp; - incrementing a packet averaging

4, min () over (): minimum statistical record partition, partition by alternatively, order by the optional

     max () over (): maximum value recorded in the statistical partition, partition by alternatively, order by the optional

1, select name, sex, age , salary, min (salary) over () from emp; - the minimum required Total 
2, select name, sex, age , salary, min (salary) over (order by age) from emp ; - incrementing minimization 
3, select name, sex, age , salary, min (salary) over (partition by sex) from emp; - for the minimum packet 
4, select name, sex, age , salary, min a sliding scale for the minimum packet -; (the salary) over (Order by Partition by Sex Age) from EMP 


from EMP. 5, SELECT name, Sex, Age, the salary, max (the salary) over (); - total maximum seek 
6, select name, sex, age , salary, max (salary) over (order by age) from emp; - incrementing selecting the maximum value 
7, select name, sex, age , salary, max (salary) over (partition by sex) from emp; - packet selecting the maximum value 
8, select name, sex, age , salary, max (salary) over (partition by sex order by age) from emp; - incrementing the packet selecting the maximum value

5, rank () over (): jump sorting, partition by alternatively, order by Required

1、select name, age, rank() over(partition by job order by age) from emp;
2、select name, age, rank() over(order by age) from emp;

6, dense_rank (): consecutively ordered, partition by alternatively, order by Required

1、select name, age, dense_rank() over(partition by job order by age) from emp;
2、select name, age, dense_rank() over(order by age) from emp;

7, row_number () over (): sorting, no duplicates, partition by alternatively, order by Required

1、select name, age, row_number() over(partition by job order by age) from emp;
2、select name, age, row_number() over(order by age) from emp;

8, ntile (n) over (): partition by alternatively, order by Required

    n represents the average recorded partition into n parts, a plurality of numbers in the sequence group allocated to the front

select name, salary, ntile(3) over(order by salary desc) from emp;
select name, salary, ntile(3) over(partition by job order by salary desc) from emp;

9, first_value () over (): Remove field value recorded in the first partition, partition by alternatively, order by the optional

     last_value () over (): remove the partition field value of the last record, partition by alternatively, order by the optional

1、select name, first_value(salary) over() from emp;
2、select name, first_value(salary) over(order by salary desc) from emp;
3、select name, first_value(salary) over(partition by job) from emp;                                                           
4、select name, first_value(salary) over(partition by job order by salary desc) from emp;


5、select name, last_value(ename) over() from emp;
6、select name, last_value(ename) over(order by salary desc) from emp;
7、select name, last_value(ename) over(partition by job) from emp;
8、select name, last_value(ename) over(partition by job order by salary desc) from emp;

10, first: a row in front of the value returned from the set of rows removed DENSE_RANK

      last: DENSE_RANK returned from the set of rows in the row a value taken rearmost

select job, max(salary) keep(dense_rank first order by salary desc),
max(salary) keep(dense_rank last order by salary desc) from emp
group by job;

11, lag () over (): remove the first n rows of data, partition by alternatively, order by Required

      lead () over (): After removing the n rows of data, partition by alternatively, order by Required

1、select name, age, lag(age,1,0) over(order by salary), 
lead(age,1,0) over(order by salary) from emp;
 
2、select name, age, lag(age,1) over(partition by sex order by salary),
lead(age,1) over(partition by sex order by salary) from emp;

12, ratio_to_report (a) over (partition by b): b request packet according to the value of a proportion of the total in the packet belongs, it must be a numeric value or numeric field

      partition by alternative, order by not optional

1, select name, job, salary , ratio_to_report (1) over () from emp; - assigning to each of a row, the total value of required proportion of the current row, always 0.1 
2, SELECT name, Job, the salary, RATIO_TO_REPORT (salary) over () from emp ; - proportion of the current value of all rows in the data 
3, select name, job, salary , ratio_to_report (1) over (partition by job) from emp; - assigning to each row 1, after seeking the total group current row share packet 
from emp 4, select name, job , salary, ratio_to_report (salary) over (partition by job); - the current value of the row after the packet group total value accounting

13, percent_rank () over (): partition by alternatively, order by Required

     Where the group ranked No. -1 divided by the number of rows in the group all -1, ranked sort jump

1、select name, job, salary, percent_rank() over(order by salary) from emp;
2、select name, job, salary, percent_rank() over(partition by job order by salary) from emp;

14, cume_dist () over (): partition by alternatively, order by Required

Rank's group number divided by the number of rows of the set, note the location for the duplicate rows, the rows take repeat the calculation of the last row

1、select name, job, salary, cume_dist() over(order by salary) from emp;
2、select name, job, salary, cume_dist() over(partition by job order by salary) from emp;

15, precentile_cont (x) within group (order by ...) over (): over () in the partition by an optional, order by no optional

x is the percentage of the input, is a decimal between 0 and 1, the percentage of the position data is returned, if not the calculated value (r) is returned:

a = 1 + (x * (N-1)) x is the percentage of the number of input lines, N being recorded within a partition

b = ceil (a) rounded up

c = floor (a) rounded down

a data r = a * b at a position Percentage Percentage position data + *

1、select name, job, salary, percentile_cont(0.5) within group(order by salary) over() from emp;
2、select name, job, salary, percentile_cont(0.5) within group(order by salary) over(partition by job) from emp;

16, precentile_disc (x) within group (order by ...) over (): over () in the partition by an optional, order by no optional

x is the percentage of the input, is a decimal between 0 and 1, the position data of the percentage return on the value corresponding to the position, if there is no corresponding data value, the distribution of values ​​taken at greater than a value

1、select name, job, salary, percentile_disc(0.5) within group(order by salary) over()from emp;
2、select name, job, salary, percentile_disc(0.5) within group(order by salary) over(partition by job) from emp;

17, stddev () over (): the sample standard deviation is calculated, 0 if only one row of data, partition by alternatively, order by the optional

      stddev_samp () over (): the sample standard deviation is calculated, and only returns null row of data, partition by alternatively, order by the optional

      stddev_pop () over (): standard deviation calculated overall, partition by alternatively, order by the optional

1, select stddev (stu_age) over () from student; - calculating the sample standard deviation for all records 
2, select stddev (stu_age) over (order by stu_age) from student; - calculating the sample standard deviation incrementing 
3, select stddev (stu_age) over (partition by stu_major) from student; - sample standard deviation calculated packet 
4, select stddev (stu_age) over (partition by stu_major order by stu_age) from student; - calculating the sample standard deviation incrementing packet 
 
 
5, select stddev_samp (stu_age) over () from student; - calculating the sample standard deviation for all records 
6, select stddev_samp (stu_age) over (order by stu_age) from student; - sample standard deviation calculated sliding scale 
7, select stddev_samp (stu_age) over (partition by stu_major) from student; - sample standard deviation calculated packet 
8, select stddev_samp (stu_age) over from student (partition by stu_major order by stu_age); - calculating the sample standard deviation incrementing packet 
 
 
9, select stddev_pop (stu_age) over () from student; - Computes the difference between the standard for all records
10, select stddev_pop (stu_age) over (order by stu_age) from student; - the overall standard deviation of the sliding scale 
11, select stddev_pop (stu_age) over (partition by stu_major) from student; - the overall standard deviation calculated packet 
12 , select stddev_pop (stu_age) over from student (partition by stu_major order by stu_age); - Computes the difference between the standard packet incrementing

18, variance () over (): calculated sample variance, when only one row of data to return 0, partition by alternatively, order by the optional

       var_samp () over (): calculated sample variance, or null when only one row of data, partition by alternatively, order by the optional

       var_pop () over (): calculate the population variance, partition by alternative, order by optional

1, select variance (stu_age) over () from student; - calculation of all records in the sample variance 
2, select variance (stu_age) over (order by stu_age) from student; - sample variance calculation sliding scale 
3, select variance ( stu_age) over (partition by stu_major) from student; - sample variance calculation packet 
4, select variance (stu_age) over (partition by stu_major order by stu_age) from student; - incrementing the calculated sample variance packet 
 
 
5, select var_samp (stu_age) over () from student ; - computing the sample variance of all records 
6, select var_samp (stu_age) over (order by stu_age) from student; - sample variance calculation sliding scale 
7, select var_samp (stu_age) over ( partition by stu_major) from student; - calculated sample variance packet 
8, select var_samp (stu_age) over (partition by stu_major order by from student stu_age); - sample variance calculation packets sliding scale 
 
 
9, select var_pop (stu_age) over () from student; - records on all of the population variance
10, select var_pop (stu_age) over (order by stu_age) from student; - Overall variance calculation sliding scale 
11, select var_pop (stu_age) over (partition by stu_major) from student; - Overall variance calculation packet 
12, select var_pop (stu_age) over from student ( partition by stu_major order by stu_age); - incrementing the sample variance calculated packet

  stddev () = sqrt (variance ()) sqrt () - seeking prescribing

       stddev_samp () = sqrt (var_samp ())

       stddec_pop=sqrt( var_pop() )

19, covar_samp over (): Returns a pair expressions sample covariance, partition by alternatively, order by the optional

       covar_pop over (): Returns the population covariance of a bunch of expressions, partition by alternative, order by optional

1, select covar_samp (stu_age, line ) over (from student); - calculation of all records in the sample covariance 
2, select covar_samp (stu_age, line ) over (order by stu_age) from student; - incrementing the sample covariance calculation variance 
3, select covar_samp (stu_age, line ) over (partition by stu_major) from student; - sample covariance calculation packet 
4, select covar_samp (stu_age, line ) over (partition by stu_major order by stu_age) from student; - incrementing the calculated sample covariance packet 
 
 
5, select covar_pop (stu_age, line ) over () from student; - calculation of all records in the population covariance 
6, select covar_pop (stu_age, line ) over (order by stu_age) from student; - calculation of the overall covariance incrementing 
7, select covar_pop (stu_age, line ) over (partition by stu_major) from student; - overall covariance calculation packet 
8, select covar_pop (stu_age, line ) over (partition by stu_major order by stu_age) from student; - calculation of the overall covariance packet incrementing

20, corr () over (): Returns a pair expression correlation coefficient, partition by alternatively, order by the optional

1, select corr (stu_age, line ) over () from student; - calculating the correlation coefficients of all records 
2, select corr (stu_age, line ) over (order by stu_age) from student; - sliding scale correlation coefficient calculation 
3 , select corr (stu_age, line) over (partition by stu_major) from student; - correlation coefficient calculation packet 
from student 4, select corr (stu_age , line) over (partition by stu_major order by stu_age); - calculation packet delivery plus correlation coefficient

21, REGR_ (Linear Regression) Functions: The least squares linear regression functions for regression line, there are nine different regression function may be used

  

  

Guess you like

Origin www.cnblogs.com/Java-125/p/12034985.html
Recommended