search
HomeDatabaseSQLDetailed explanation of SQL window functions: the use of ranking window functions

This article brings you relevant knowledge about SQL server, which mainly introduces SQL Server primary key constraints (PRIMARY KEY). The primary key is a column or a group that uniquely identifies each row in the table. Column, the article expands on the topic in detail. Let’s take a look at it together. I hope it will be helpful to everyone.

Detailed explanation of SQL window functions: the use of ranking window functions

Recommended study: "SQL Tutorial"

For the basics of window functions, please see the article SQL Window Functions

The value window function can be used to return the data row at the specified position within the window. Common value window functions are as follows:

LAG function can return the Nth row of data before the current row in the window. The LEAD function can return the Nth row of data after the current row in the window. The FIRST_VALUE function can return the first row of data in the window. The LAST_VALUE function can return the last row of data in the window. The NTH_VALUE function can return the Nth row of data in the window.

Among them, the LAG function and LEAD function do not support dynamic window size. They use the entire partition as the analysis window.

Case analysis

Example table used in the case

The following query will use a table. The sales_monthly table stores product sales information, and product represents the product name. ym represents the year and month, and amount represents the sales amount (yuan).

The following is some data in the table:

The initialization script of this table can be obtained at the bottom of the article.

1. Period-on-month analysis

Period-on-month growth refers to the growth of the current period's data compared with the previous period's data. For example, the sales of a product in June 2019 and the sales in May 2019 Compared to the increased portion.

The following statement counts the month-on-month growth rate of various products:

SELECT s.product AS "产品", s.ym AS "年月", s.amount AS "销售额",
 ( 
    (s.amount - LAG(s.amount,1) OVER (PARTITION BY product ORDER BY s.ym))/
    LAG(s.amount,1) OVER (PARTITION BY product ORDER BY s.ym)
 ) * 100 AS "环比增长率(%)"
FROM sales_monthly s
ORDER BY s.product,s.ym

Among them, LAG (amount, 1) means to obtain the sales volume of the previous period, and the PARTITION BY option means according to For product partitioning, the ORDER BY option indicates sorting by month.

The sales amount of the current month minus the sales amount of the previous period, and then divided by the sales amount of the previous period, is the month-on-month growth rate.

The results returned by this query are as follows:

#January 2018 is the first period, so its month-on-month growth rate is empty.

The month-on-month growth rate of "Orange" in February 2018 was approximately 0.2856% ((10183-10154)/10154×100), and so on.

2. Year-on-year analysis

Year-over-year growth refers to the growth of the current period's data compared with the previous year or the same period in history. For example, the sales of a product in June 2019 were compared with those in June 2018. The increase compared to monthly sales.

The following statement counts the year-on-year growth rate of various products every month:

SELECT s.product AS "产品", s.ym AS "年月", s.amount AS "销售额",
 ( 
    (s.amount - LAG(s.amount,12) OVER (PARTITION BY product ORDER BY s.ym))/
    LAG(s.amount,12) OVER (PARTITION BY product ORDER BY s.ym)
 ) * 100 AS "同比增长率(%)"
FROM sales_monthly s
ORDER BY s.product,s.ym

Among them, LAG (amount, 12) represents the sales volume of the 12th period before the current month, that is, last year sales in the same month.

PARTITION BY option indicates partitioning by product, and ORDER BY option indicates sorting by month.

The sales amount of the current month minus the sales of the same period last year, divided by the sales of the same period last year, is the year-on-year growth rate.

The results returned by this query are as follows:

#There is no corresponding year-on-year growth rate for the 12 periods of data in 2018. "Orange" in January 2019 The year-on-year growth rate is approximately 9.3067% ((11099-10154)/10154×100), and so on.

Tip: The LEAD function is similar to the LAG function, but its return result is the Nth row of data after the current row.

3. Compound growth rate

The compound growth rate is the result of dividing the data of the Nth period by the benchmark data of the first period, then raising it to the power of N-1 and subtracting 1.

Suppose the product sales in 2018 are 10,000, the product sales in 2019 are 12,500, and the product sales in 2020 are 15,000. Then the compound growth rate of these two years is calculated as follows:

The compound growth rate calculated on an annual basis is called average annual compound growth rate, the compound growth rate calculated on a monthly basis is called average monthly compound growth rate.

The following query counts the compound growth rate of average monthly sales of different products since January 2018:

WITH s (product,ym,amount,first_amount,num) AS (
  SELECT m.product, m.ym, m.amount,
  FIRST_VALUE(m.amount) OVER (PARTITION BY m.product ORDER BY m.ym),
  ROW_NUMBER() OVER (PARTITION BY m.product ORDER BY m.ym)
  FROM sales_monthly m
)
 
SELECT product AS "产品", ym AS "年月",amount AS "销售额",
       (POWER( amount/first_amount, 1.0/NULLIF(num-1,0)) -1)*100 AS "月均复合增长率(%)"
FROM s
ORDER BY product, ym

First defines a general table expression, in which FIRST_VALUE (amount) returns For the sales volume of the first period (201801), the ROW_NUMBER function returns the number of each period.

The POWER function in the main query is used to perform square root operations, the NULLIF function is used to handle division-by-zero errors in the first period of data, and the constant 1.0 is used to avoid precision loss caused by integer division.

The results returned by this query are as follows:

January 2018 is the first period, so the compound growth rate of its product monthly average sales is empty.

“桔子”2018年2月的月均销售额复合增长率等于它的环比增长率,2018年3月的月均销售额复合增长率等于0.4471%,依此类推。 

4.不同产品最高和最低销售额

以下语句统计了不同产品最低销售额、最高销售额以及第三高销售额所在的月份:

  SELECT product AS "产品", ym AS "年月",amount AS "销售额",
  
         FIRST_VALUE(m.ym) OVER (
           PARTITION BY m.product ORDER BY m.amount DESC
           ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
         ) AS "最高销售额月份",
         
         LAST_VALUE(m.ym) OVER (
           PARTITION BY m.product ORDER BY m.amount DESC
           ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
         ) AS "最低销售额月份",
         
         NTH_VALUE(m.ym,3) OVER (
           PARTITION BY m.product ORDER BY m.amount DESC
           ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
         ) AS "第三高销售额月份"
 
  FROM sales_monthly m
  ORDER BY product, ym;

三个窗口函数的OVER子句相同,PARTITION BY选项表示按照产品进行分区,ORDER BY选项表示按照销售额从高到低排序。

以上三个函数的默认窗口都是从分区的第一行到当前行,因此我们将窗口扩展到了整个分区。

该查询返回的结果如下: 

“桔子”的最高销售额出现在2019年6月,最低销售额出现在2018年1月,第三高销售额出现在2019年4月。

示例表和脚本

-- 创建销量表sales_monthly
-- product表示产品名称,ym表示年月,amount表示销售金额(元)
CREATE TABLE sales_monthly(product VARCHAR(20), ym VARCHAR(10), amount NUMERIC(10, 2));
 
-- 生成测试数据
INSERT INTO sales_monthly (product,ym,amount) VALUES ('苹果','201801',10159.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('苹果','201802',10211.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('苹果','201803',10247.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('苹果','201804',10376.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('苹果','201805',10400.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('苹果','201806',10565.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('苹果','201807',10613.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('苹果','201808',10696.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('苹果','201809',10751.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('苹果','201810',10842.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('苹果','201811',10900.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('苹果','201812',10972.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('苹果','201901',11155.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('苹果','201902',11202.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('苹果','201903',11260.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('苹果','201904',11341.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('苹果','201905',11459.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('苹果','201906',11560.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('香蕉','201801',10138.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('香蕉','201802',10194.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('香蕉','201803',10328.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('香蕉','201804',10322.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('香蕉','201805',10481.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('香蕉','201806',10502.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('香蕉','201807',10589.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('香蕉','201808',10681.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('香蕉','201809',10798.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('香蕉','201810',10829.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('香蕉','201811',10913.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('香蕉','201812',11056.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('香蕉','201901',11161.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('香蕉','201902',11173.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('香蕉','201903',11288.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('香蕉','201904',11408.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('香蕉','201905',11469.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('香蕉','201906',11528.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('桔子','201801',10154.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('桔子','201802',10183.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('桔子','201803',10245.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('桔子','201804',10325.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('桔子','201805',10465.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('桔子','201806',10505.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('桔子','201807',10578.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('桔子','201808',10680.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('桔子','201809',10788.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('桔子','201810',10838.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('桔子','201811',10942.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('桔子','201812',10988.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('桔子','201901',11099.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('桔子','201902',11181.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('桔子','201903',11302.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('桔子','201904',11327.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('桔子','201905',11423.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('桔子','201906',11524.00);

推荐学习:《SQL教程

The above is the detailed content of Detailed explanation of SQL window functions: the use of ranking window functions. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:脚本之家. If there is any infringement, please contact admin@php.cn delete
OLTP vs OLAP: What about big data?OLTP vs OLAP: What about big data?May 14, 2025 am 12:06 AM

OLTPandOLAParebothessentialforbigdata:OLTPhandlesreal-timetransactions,whileOLAPanalyzeslargedatasets.1)OLTPrequiresscalingwithtechnologieslikeNoSQLforbigdata,facingchallengesinconsistencyandsharding.2)OLAPusesHadoopandSparktoprocessbigdata,withsetup

What is Pattern Matching in SQL and How Does It Work?What is Pattern Matching in SQL and How Does It Work?May 13, 2025 pm 04:09 PM

PatternmatchinginSQLusestheLIKEoperatorandregularexpressionstosearchfortextpatterns.Itenablesflexibledataqueryingwithwildcardslike%and_,andregexforcomplexmatches.It'sversatilebutrequirescarefulusetoavoidperformanceissuesandoveruse.

Learning SQL: Understanding the Challenges and RewardsLearning SQL: Understanding the Challenges and RewardsMay 11, 2025 am 12:16 AM

Learning SQL requires mastering basic knowledge, core queries, complex JOIN operations and performance optimization. 1. Understand basic concepts such as tables, rows, and columns and different SQL dialects. 2. Proficient in using SELECT statements for querying. 3. Master the JOIN operation to obtain data from multiple tables. 4. Optimize query performance, avoid common errors, and use index and EXPLAIN commands.

SQL: Unveiling Its Purpose and FunctionalitySQL: Unveiling Its Purpose and FunctionalityMay 10, 2025 am 12:20 AM

The core concepts of SQL include CRUD operations, query optimization and performance improvement. 1) SQL is used to manage and operate relational databases and supports CRUD operations. 2) Query optimization involves the parsing, optimization and execution stages. 3) Performance improvement can be achieved through the use of indexes, avoiding SELECT*, selecting the appropriate JOIN type and pagination query.

SQL Security Best Practices: Protecting Your Database from VulnerabilitiesSQL Security Best Practices: Protecting Your Database from VulnerabilitiesMay 09, 2025 am 12:23 AM

Best practices to prevent SQL injection include: 1) using parameterized queries, 2) input validation, 3) minimum permission principle, and 4) using ORM framework. Through these methods, the database can be effectively protected from SQL injection and other security threats.

MySQL: A Practical Application of SQLMySQL: A Practical Application of SQLMay 08, 2025 am 12:12 AM

MySQL is popular because of its excellent performance and ease of use and maintenance. 1. Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2. Insert and query data: operate data through INSERTINTO and SELECT statements. 3. Optimize query: Use indexes and EXPLAIN statements to improve performance.

Comparing SQL and MySQL: Syntax and FeaturesComparing SQL and MySQL: Syntax and FeaturesMay 07, 2025 am 12:11 AM

The difference and connection between SQL and MySQL are as follows: 1.SQL is a standard language used to manage relational databases, and MySQL is a database management system based on SQL. 2.SQL provides basic CRUD operations, and MySQL adds stored procedures, triggers and other functions on this basis. 3. SQL syntax standardization, MySQL has been improved in some places, such as LIMIT used to limit the number of returned rows. 4. In the usage example, the query syntax of SQL and MySQL is slightly different, and the JOIN and GROUPBY of MySQL are more intuitive. 5. Common errors include syntax errors and performance issues. MySQL's EXPLAIN command can be used for debugging and optimizing queries.

SQL: A Guide for Beginners - Is It Easy to Learn?SQL: A Guide for Beginners - Is It Easy to Learn?May 06, 2025 am 12:06 AM

SQLiseasytolearnforbeginnersduetoitsstraightforwardsyntaxandbasicoperations,butmasteringitinvolvescomplexconcepts.1)StartwithsimplequerieslikeSELECT,INSERT,UPDATE,DELETE.2)PracticeregularlyusingplatformslikeLeetCodeorSQLFiddle.3)Understanddatabasedes

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools