Directory
=========================================
1. Introduction to Window function
2. Window Function Example-all statistics
3. Window function Advanced-scrolling statistics (cumulative/mean value)
4. Window function Advanced-statistics based on time range
5. Window function Advanced-first_value/last_value
6. Window function Advanced-compare adjacent records
Introduction of Window Function:
So far, the analytic functions we've learned are particularly useful for calculating/counting data over time, but if the calculations/statistics need to go through each record of the recordset. For some examples:
① lists the total monthly orders and total orders for the whole year
② lists the total monthly orders and the total orders up to the current month
③ lists total orders for last month, month, next January, and total orders for the year
④ lists the daily turnover and total turnover in a week.
⑤ lists the daily turnover and average turnover per day in a week.
Looking back at the analytic function we introduced earlier, we will find that these requirements are different from the previous ones: the analysis function we described earlier is used to compute/count an explicit stage/Recordset, and there are some requirements, such as 2, that need to be counted as each record of the recordset is traversed.
That is to say: statistics not only happen once, but happen many times. Statistics do not occur after a recordset is formed, but rather occur during the formation of a recordset.
This is the application of the window function we are going to introduce this time. It applies to the following situations:
① by specifying a batch of records: For example, from the current record to the end of a part of the last record
② by specifying a time interval: for example, the first 30 days before the trading day
③ by specifying a range value: For example, all records that account for 5% of the current total volume
Second, window function examples-all statistics:
Here's what we need: Take a look at the application of the window function by listing the total orders for each month and the total order totals for the whole year.
"1" Test environment: SQL > DESC orders;
is the name empty? Type
-- --------------------- -------- ----------------
MONTH Number (2)
Tot_sales number
SQL >
"2" test data: SQL > select * from Orders;
MONTH Tot_sales
-- -------- ----------
1 610697
2 428676
3 637031
4 541146
5 592935
6 501485
7 606914
8 460520
9 392898
10 510117
11 532889
12 492458
12 rows have been selected.
"3" test statement:
Recalling the previous article on Oracle development topics: Analytic functions (over), we used sum (tot_sales) over (partition by region_id) to count the total orders for each partition. Now we're going to count not just every partition, but all partitions, partition by region_id doesn't work here.
Oracle provides a clause for this situation: rows between ... preceding and ... following. Literally guessing what it means: all the records before XXX and after xxx, how the actual situation lets us verify by example: SQL > select Month,
2 sum (tot_sales) Month_sales,
3 sum (SUM (tot_sales)) over (order by month
4 rows between unbounded preceding and unbounded following) Total_sales
5 from Orders
6 Group BY Month;
MONTH Month_sales Total_sales
-- -------- ----------- -----------
1 610697 6307766
2 428676 6307766
3 637031 6307766
4 541146 6307766
5 592935 6307766
6 501485 6307766
7 606914 6307766
8 460520 6307766
9 392898 6307766
10 510117 6307766
11 532889 6307766
12 492458 6307766
12 rows have been selected.
The green highlighting code plays a key role here, telling Oracle to count the monthly sales from the first record to the last record. This statistic was performed 12 times during the formation of the recordset, which is quite time-consuming. But at least we solved the problem.
The meaning of unbounded preceding and unbouned following is for the previous and last records of all current records, that is, all records in the table. So if we just specify from the first record to the end of the beginning. Look at the following results: SQL > select Month,
2 sum (tot_sales) Month_sales,
3 sum (SUM (tot_sales)) over (order by month
4 rows between 1 preceding and unbounded following) All_sales
5 from Orders
6 Group BY Month;
MONTH Month_sales All_sales
-- -------- ----------- ----------
1 610697 6307766
2 428676 6307766
3 637031 5697069
4 541146 5268393
5 592935 4631362
6 501485 4090216
7 606914 3497281
8 460520 2995796
9 392898 2388882
10 510117 1928362
11 532889 1535464
12 492458 1025347
12 rows have been selected.
(It is obvious that his sum (total_sales) is the total value from the previous record to the bottom of all records)
It is obvious that the statement was wrong. The actual 1 here is not the beginning of the 1th record, but the previous record of the current record. The modifier before the preceding is the number of records to tell the window function to execute, as unbounded tells Oracle that regardless of the current record is the first few, as long as the number of previous records, are included in the scope of the statistics.
Third, the window function advanced-scrolling statistics (cumulative/mean):
Consider the 2nd requirement mentioned earlier: list the total monthly orders and the total orders up to the current month. That means the February record will show the sum of the total orders for the month and the sum of 1, February orders. March to show the sum of the total orders for the month and the sum of 1, 2, March orders, and so on.
It is clear that this requirement needs to be counted on the sum of the total orders for the nth month. Think of the above statement if we can replace and unbounded following with the logic representing the current month. Luckily Oracle took our needs into account, so we just need to change the statement a little: curreent row is ok. SQL > select Month,
2 sum (tot_sales) Month_sales,
3 sum (SUM (tot_sales)) over (order by month
4 rows between unbounded preceding and current row) Current_total_sales
5 from Orders
6 Group BY Month;
MONTH Month_sales Current_total_sales
-- -------- ----------- -------------------
1 610697 610697
2 428676 1039373
3 637031 1676404
4 541146 2217550
5 592935 2810485
&nbs