当前位置:网站首页>Analysis function in SQL
Analysis function in SQL
2022-07-04 10:57:00 【ths512】
One , Analyze the difference between functions and aggregate functions
Aggregate functions :
1 Aggregate functions can be used directly . max() ,min(),avg(),count()…
2 The calculation result of the aggregate function returns a value .
3 GROUP BY After grouping , The aggregation function will group fields according to , Each group returns a calculation result .
4 If the aggregate function is to be queried together with other fields , Then the remaining fields must be grouped fields .
5 GROUP BY after , stay SELECT Back Can only query Grouped fields , And aggregate functions , The remaining ungrouped fields are not allowed to be queried directly .
6 If the data does not exist in the table , Not subject to the above conditions .
Analysis function
1 Analysis functions have almost no restrictions .
2 The way of grouping is different , Analysis function Use PARTITION BY Grouping .
3 The number of return values is different : The analysis function will be based on the number of rows in the table , Each row returns a result of the calculation .
4 ORDER BY In aggregate function , Just a simple sort , In addition to being able to sort in the analysis function , It can also be evaluated cumulatively .
Two , What is the analytic function ?
The analytic function is Oracle Powerful functions for complex report statistics , It can group in the data and then calculate some statistics based on the group , And each row of each group can return a statistical value .
3、 ... and , What is the difference between analysis function and aggregate function ?
Common aggregate functions use group by grouping , Each group returns a statistic , And the analysis function uses partition by grouping , And each row of each group can return a statistical value .
Four , Analyze the form of the function
The analysis function has a windowing function :over()
Contains three analysis clauses : grouping (partition by), Sort (order by), window (rows)
They are used in the following forms :
over(partition by xxx order by yyy)
over(partition by xxx order by yyy rows between unbounded preceding and current row) notes : Marked in red is the window scope clause
5、 ... and , Common function analysis
1, Sort analysis function
Seek the overall situation TopN Can be used when order by limit Realization , Just ask each group TopN You have to use the sorting analysis function to quickly realize
- row_number In the same group , The serial number is not repeated , The serial number is absolutely continuous
- rand In the same group , The serial number is repeated , The next serial number of the repeated serial number will skip
- dense_rank In the same group , The serial number is repeated , The serial number is absolutely continuous
1) Comprehensive case
Table data create table TEST_ROW_NUMBER_OVER( id varchar(10) not null, name varchar(10) null, age varchar(10) null, salary int null ); select * from TEST_ROW_NUMBER_OVER t; insert into TEST_ROW_NUMBER_OVER(id,name,age,salary) values(1,'a',10,8000); insert into TEST_ROW_NUMBER_OVER(id,name,age,salary) values(1,'a2',11,6500); insert into TEST_ROW_NUMBER_OVER(id,name,age,salary) values(2,'b',12,13000); insert into TEST_ROW_NUMBER_OVER(id,name,age,salary) values(2,'b2',13,4500); insert into TEST_ROW_NUMBER_OVER(id,name,age,salary) values(3,'c',14,3000); insert into TEST_ROW_NUMBER_OVER(id,name,age,salary) values(3,'c2',15,20000); insert into TEST_ROW_NUMBER_OVER(id,name,age,salary) values(4,'d',16,30000); insert into TEST_ROW_NUMBER_OVER(id,name,age,salary) values(5,'d2',17,1800);
2) Sort query results ( No grouping )
select id,name,age,salary,row_number()over(order by salary desc) rank
from TEST_ROW_NUMBER_OVER t
3) according to id Grouping sorting
select id,name,age,salary,row_number()over(partition by id order by salary desc) rank
from TEST_ROW_NUMBER_OVER t
4) Find out the number one data in each group
select * from(select id,name,age,salary,row_number()over(partition by id order by salary desc) rank
from TEST_ROW_NUMBER_OVER t)
where rank <2
2, Aggregate analysis functions
3, Shift analysis function
1) WITH AS The phrase , Also called subquery part (subquery factoring), You can define a SQL fragment , The SQL The fragment will be replaced by the whole SQL Statement use .
More with as See the blog below :
https://blog.csdn.net/jia718/article/details/88253918
with
cr as
(
select CountryRegionCode from person.CountryRegion where Name like 'C%'
)
select * from person.StateProvince where CountryRegionCode in (select * from cr)
The query results are the same as the following :
select * from person.StateProvince where CountryRegionCode in
(select CountryRegionCode from person.CountryRegion where Name like 'C%')
2) CTE
3) lead ()
https://blog.csdn.net/a5276957/article/details/103808997
4) ·lag()
https://blog.csdn.net/a5276957/article/details/103808997
4,presto array_agg Analysis function
array_agg(expression): Turn an expression into an array
5, Window scope clause
https://www.cnblogs.com/Brambling/p/6721078.html
边栏推荐
- Performance features focus & JMeter & LoadRunner advantages and disadvantages
- Fundamentals of database operation
- The bamboo shadow sweeps the steps, the dust does not move, and the moon passes through the marsh without trace -- in-depth understanding of the pointer
- [machine] [server] Taishan 200
- Aike AI frontier promotion (2.14)
- How do microservices aggregate API documents? This wave of show~
- Ten key performance indicators of software applications
- [Galaxy Kirin V10] [server] NFS setup
- Heartbeat报错 attempted replay attack
- [Galaxy Kirin V10] [desktop] printer
猜你喜欢
[Galaxy Kirin V10] [desktop] can't be started or the screen is black
JMeter assembly point technology and logic controller
DNS hijacking
The most detailed teaching -- realize win10 multi-user remote login to intranet machine at the same time -- win10+frp+rdpwrap+ Alibaba cloud server
Recursion and divide and conquer strategy
Dynamic memory management
F12 clear the cookies of the corresponding web address
Canoe - the third simulation project - bus simulation - 3-2 project implementation
Article publishing experiment
[Galaxy Kirin V10] [server] KVM create Bridge
随机推荐
Terms related to hacker technology
Linked list operation can never change without its roots
Canoe - the second simulation engineering - xvehicle - 2 panel design (operation)
[Galaxy Kirin V10] [desktop] FTP common scene setup
unit testing
[machine] [server] Taishan 200
Seven examples to understand the storage rules of shaped data on each bit
Performance test process
[Galaxy Kirin V10] [server] set time synchronization of intranet server
From programmers to large-scale distributed architects, where are you (2)
Elevator dispatching (pairing project) ③
Read a piece of text into the vector object, and each word is stored as an element in the vector. Convert each word in the vector object to uppercase letters. Output the converted elements in the vect
Day06 list job
/*Rewrite the program, find the value of the element, and return the iterator 9.13: pointing to the found element. Make sure that the program works correctly when the element you are looking for does
Performance test method
Common system modules and file operations
DDL statement of MySQL Foundation
Quick sort (C language)
MBG combat zero basis
The last month before a game goes online