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


边栏推荐
- Ten key performance indicators of software applications
- Oracle11g | getting started with database. It's enough to read this 10000 word analysis
- [test theory] test phase analysis (unit, integration, system test)
- Canoe: the fourth simulation project -- bug debugging experience
- Using SA token to solve websocket handshake authentication
- 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 test: two ways to create CAPL test module
- Application and Optimization Practice of redis in vivo push platform
- [machine] [server] Taishan 200
- Performance test method
猜你喜欢

Canoe: what is vtsystem

On binary tree (C language)

Discussion | has large AI become autonomous? Lecun, chief scientist of openai

Elevator dispatching (pairing project) ②

Introduction to tree and binary tree

Failed to configure a DataSource: ‘url‘ attribute is not specified... Bug solution
![[Galaxy Kirin V10] [server] NFS setup](/img/ed/bd7f1a1e4924a615cb143a680a2ac7.jpg)
[Galaxy Kirin V10] [server] NFS setup
![[Galaxy Kirin V10] [server] failed to start the network](/img/0f/6d2f321da85bd7437d2b86547bd8b4.jpg)
[Galaxy Kirin V10] [server] failed to start the network

Network connection (III) functions and similarities and differences of hubs, switches and routers, routing tables and tables in switches, why do you need address translation and packet filtering?

Function introduction of canbedded component
随机推荐
[test theory] test the dimension of professional ability
Performance test method
[Galaxy Kirin V10] [server] FTP introduction and common scenario construction
[Galaxy Kirin V10] [desktop] cannot add printer
Student achievement management system (C language)
Performance features focus & JMeter & LoadRunner advantages and disadvantages
R built in data set
2022 AAAI fellow release! Yan Shuicheng, chief scientist of sail, and Feng Yan, Professor of Hong Kong University of science and technology, were selected
[test theory] test phase analysis (unit, integration, system test)
Day06 list job
2、 Operators and branches
I What is security testing
[Galaxy Kirin V10] [server] NUMA Technology
Canoe - the second simulation engineering - xvehicle - 2 panel design (operation)
shell awk
Communication layer of csframework
DNS hijacking
regular expression
Swagger and OpenAPI
[Galaxy Kirin V10] [desktop] login system flash back

