当前位置:网站首页>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


边栏推荐
- Network connection (II) three handshakes, four waves, socket essence, packaging of network packets, TCP header, IP header, ACK confirmation, sliding window, results of network packets, working mode of
- Jemeter script recording
- The most ideal automated testing model, how to achieve layering of automated testing
- Open the neural network "black box"! Unveil the mystery of machine learning system with natural language
- Interview and lecture summary 1
- On binary tree (C language)
- Jianzhi offer 04 (implemented in C language)
- Oracle11g | getting started with database. It's enough to read this 10000 word analysis
- Huge number (C language)
- Canoe - description of common database attributes
猜你喜欢

Dichotomy search (C language)

Appscan installation steps

Canoe test: two ways to create CAPL test module
![[Galaxy Kirin V10] [desktop] can't be started or the screen is black](/img/68/735d80c648f4a8635513894c473860.jpg)
[Galaxy Kirin V10] [desktop] can't be started or the screen is black

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

Canoe - the third simulation project - bus simulation - 3-2 project implementation

Network connection (II) three handshakes, four waves, socket essence, packaging of network packets, TCP header, IP header, ACK confirmation, sliding window, results of network packets, working mode of

Basic data types of MySQL
![[Galaxy Kirin V10] [desktop] printer](/img/ab/066923f1aa1e8dd8dcc572cb60a25d.jpg)
[Galaxy Kirin V10] [desktop] printer

F12 clear the cookies of the corresponding web address
随机推荐
Software testing related resources
Write a program that uses pointers to set all elements of an int array to 4.18: 0.
Using SA token to solve websocket handshake authentication
Add t more space to your computer (no need to add hard disk)
Knapsack problem and 0-1 knapsack problem
[test theory] test phase analysis (unit, integration, system test)
Performance test method
本地Mysql忘记密码的修改方法(windows)
Software sharing: the best PDF document conversion tool and PDF Suite Enterprise version sharing | with sharing
VI text editor and user rights management, group management and time management
JMeter Foundation
From programmers to large-scale distributed architects, where are you (2)
2022 ape circle recruitment project (software development)
How to quickly parse XML documents through C (in fact, other languages also have corresponding interfaces or libraries to call)
Swagger and OpenAPI
F12 clear the cookies of the corresponding web address
Application and Optimization Practice of redis in vivo push platform
Design and common methods of test case documents
Dichotomy search (C language)
Performance test overview

