当前位置:网站首页>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
边栏推荐
- unit testing
- Canoe - the third simulation project - bus simulation - 3-1 project implementation
- Canoe: the difference between environment variables and system variables
- Day06 list job
- Fundamentals of database operation
- Performance features focus & JMeter & LoadRunner advantages and disadvantages
- Introduction to canoe automatic test system
- Summary of several job scheduling problems
- From programmers to large-scale distributed architects, where are you (2)
- VI text editor and user rights management, group management and time management
猜你喜欢
DML statement of MySQL Foundation
How to use diff and patch to update the source code
Hidden C2 tunnel -- use of icmpsh of ICMP
Time complexity and space complexity
[Galaxy Kirin V10] [server] KVM create Bridge
[Galaxy Kirin V10] [desktop] build NFS to realize disk sharing
Send a request using paste raw text
JMeter assembly point technology and logic controller
[test theory] test process management
[Galaxy Kirin V10] [desktop] cannot add printer
随机推荐
Interview and lecture summary 1
Locust learning record I
Jemeter script recording
JMeter assembly point technology and logic controller
Basic data types of MySQL
The most detailed teaching -- realize win10 multi-user remote login to intranet machine at the same time -- win10+frp+rdpwrap+ Alibaba cloud server
Using Lua to realize 99 multiplication table
183 sets of free resume templates to help everyone find a good job
Canoe - description of common database attributes
[Galaxy Kirin V10] [server] FTP introduction and common scenario construction
本地Mysql忘记密码的修改方法(windows)
Solaris 10网络服务
本地Mysql忘记密码的修改方法(windows)[通俗易懂]
Appscan installation error: unable to install from Net runtime security policy logout appscan solution
[Galaxy Kirin V10] [desktop] cannot add printer
What if the book written is too popular? Author of "deep reinforcement learning" at Peking University: then open the download
From programmers to large-scale distributed architects, where are you (2)
Hidden C2 tunnel -- use of icmpsh of ICMP
Software sharing: the best PDF document conversion tool and PDF Suite Enterprise version sharing | with sharing
Failed to configure a DataSource: ‘url‘ attribute is not specified... Bug solution