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


边栏推荐
- Dynamic memory management
- When I forget how to write SQL, I
- Capl: timer event
- Ten key performance indicators of software applications
- shell awk
- Canoe - the second simulation engineering - xvehicle - 2 panel design (operation)
- [untitled]
- Canoe - the third simulation project - bus simulation - 2 function introduction, network topology
- Summary of several job scheduling problems
- Write a program to judge whether the elements contained in a vector < int> container are 9.20: exactly the same as those in a list < int> container.
猜你喜欢

Jemeter script recording

Collection of practical string functions

JMeter Foundation

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
![[Galaxy Kirin V10] [desktop] build NFS to realize disk sharing](/img/72/5e725a44a50f152b477a4b2907a2d0.jpg)
[Galaxy Kirin V10] [desktop] build NFS to realize disk sharing

What is an excellent architect in my heart?

Elevator dispatching (pairing project) ④

Recursion and divide and conquer strategy

Send a request using paste raw text

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
随机推荐
JMeter common configuration components and parameterization
TS type gymnastics: illustrating a complex advanced type
[Galaxy Kirin V10] [server] failed to start the network
Performance test method
Sword finger offer 05 (implemented in C language)
Canoe - the third simulation project - bus simulation - 3-2 project implementation
[Galaxy Kirin V10] [desktop] cannot add printer
[Galaxy Kirin V10] [desktop] login system flash back
Ten key performance indicators of software applications
[Galaxy Kirin V10] [desktop] printer
Huge number (C language)
[test theory] test phase analysis (unit, integration, system test)
JMeter assembly point technology and logic controller
regular expression
Postman interface test
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
Software testing related resources
Send a request using paste raw text
iptables导致Heartbeat脑裂
Elevator dispatching (pairing project) ④

