search
HomeDatabaseMysql TutorialDetailed explanation of the use of Partition By and row_number functions in SQL Server

Detailed explanation of the use of Partition By and row_number functions in SQL Server

partition The by keyword is part of the analytic function. It is different from the aggregate function in that it can return multiple records in a group, while the aggregate function generally only has one record that reflects statistics. Records of values, partition by is used to group the result set. If not specified, it treats the entire result set as a group.

I saw a question in the group today, and I’ll summarize it here: Query the latest records under different categories. Isn't it very simple at first glance? If you want to classify, use Group By; if you want the latest record, use Order By. Then try to make it in your own table:

Related learning recommendations: mysql video tutorial

First of all, I put the data in the table Listed in reverse order of submission time:

"corp_name" is the GUID of the category (please forgive the arbitrariness of my naming). OK, here we add Group By according to the original idea to see the display effect:

Uh, um. This result is different from what I imagined. It seems that when writing code, you still need to analyze the problem rationally. Your mind cannot control the result!

Since the requirements are different categories of data, besides using Group By, are there any other functions that can be used? I did some research and found out that there is indeed an over (partition by) function. So what is the difference between it and the usually used Group By? In addition to simply grouping results, Group By is generally used together with aggregate functions. Partition By also has a grouping function and is an Oracle analysis function. I won’t go into details here.

Look at the code:

##over(partition by corp_name order by submit_time desc) as t. It is classified according to corp_name and sorted out in reverse order of time. The column "t" here is the number of occurrences of different corp_name classes. The requirement is to only query the latest submission data of different categories, then we only need to filter again for "t". :

Okay, the results are out. I don’t ask you to like them, but I just want you to give me a thumbs up if you look at the breasts in my avatar. May this good man have a safe life! ! !

ps: Detailed explanation of the use of SQL Server database partition by and ROW_NUMBER() functions

Some usage experience of SQL partition by field

Read first Example:

if object_id('TESTDB') is not null drop table TESTDB
create table TESTDB(A varchar(8), B varchar(8))
insert into TESTDB
select 'A1', 'B1' union all
select 'A1', 'B2' union all
select 'A1', 'B3' union all
select 'A2', 'B4' union all
select 'A2', 'B5' union all
select 'A2', 'B6' union all
select 'A3', 'B7' union all
select 'A3', 'B3' union all
select 'A3', 'B4'

-- All information

SELECT * FROM TESTDB
A  B
-------
A1 B1
A1 B2
A1 B3
A2 B4
A2 B5
A2 B6
A3 B7
A3 B3
A3 B4

-- After using the PARTITION BY function

SELECT *,ROW_NUMBER() OVER(PARTITION BY A ORDER BY A DESC) NUM FROM TESTDB
A  B  NUM
-------------
A1 B1 1
A1 B2 2
A1 B3 3
A2 B4 1
A2 B5 2
A2 B6 3
A3 B7 1
A3 B3 2
A3 B4 3

, you can see that there is an extra column of NUM in the result. This NUM shows the same The number of rows, for example, if there are 3 A1s, he will mark the number of each A1.

-- Just use the result of ROW_NUMBER() OVER

SELECT *,ROW_NUMBER() OVER(ORDER BY A DESC)NUM FROM TESTDB
 A  B   NUM
------------------------
A3 B7  1
A3 B3  2
A3 B4  3
A2 B4  4
A2 B5  5
A2 B6  6
A1 B1  7
A1 B2  8
A1 B3  9

You can see that it just marks the line number.

-- A deeper application

SELECT A = CASE WHEN NUM = 1 THEN A ELSE '' END,B
FROM (SELECT A,NUM = ROW_NUMBER() OVER(PARTITION BY A ORDER BY A DESC) FROM TESTDB) T
A  B
---------
A1 B1
  B2
  B3
A2 B4
  B5
  B6
A3 B7
  B3
  B4

Next, we will introduce the use of ROW_NUMBER() function one by one through several examples.

The examples are as follows:

1. Use the row_number() function for numbering, such as

select email,customerID, ROW_NUMBER() over(order by psd) as rows from QT_Customer

Principle: Sort by psd first, After sorting, number each piece of data.

2. Sort the order in ascending order of price, and sort each record with the following code:

select DID,customerID,totalPrice,ROW_NUMBER() over(order by totalPrice) as rows from OP_Order

3. Count each record All orders from each household are sorted in ascending order by the amount of each customer's order, and each customer's order is numbered. In this way, you will know how many orders each customer has placed. As shown in the picture:

The code is as follows:

select ROW_NUMBER() over(partition by customerID order by totalPrice) as rows,customerID,totalPrice, DID from OP_Order

4. Count the number of times each customer’s recent order was placed. Order.

The code is as follows:

 with tabs as 
( 
select ROW_NUMBER() over(partition by customerID order by totalPrice) as rows,customerID,totalPrice, DID from OP_Order 
 ) 
select MAX(rows) as '下单次数',customerID from tabs group by customerID

5. Count the minimum purchase amount among all orders of each customer, and also count the changes in the orders. How many times did the customer purchase?

如图:

上图:rows表示客户是第几次购买。

思路:利用临时表来执行这一操作。

1.先按客户进行分组,然后按客户的下单的时间进行排序,并进行编号。

2.然后利用子查询查找出每一个客户购买时的最小价格。

3.根据查找出每一个客户的最小价格来查找相应的记录。

代码如下:

with tabs as 
 ( 
select ROW_NUMBER() over(partition by customerID order by insDT) as rows,customerID,totalPrice, DID from OP_Order 
) 
 select * from tabs 
where totalPrice in  
( 
select MIN(totalPrice)from tabs group by customerID 
 )

6.筛选出客户第一次下的订单。

思路。利用rows=1来查询客户第一次下的订单记录。

代码如下:

with tabs as 
( 
select ROW_NUMBER() over(partition by customerID order by insDT) as rows,* from OP_Order 
) 
select * from tabs where rows = 1 
select * from OP_Order

7.rows_number()可用于分页

思路:先把所有的产品筛选出来,然后对这些产品进行编号。然后在where子句中进行过滤。

8.注意:在使用over等开窗函数时,over里头的分组及排序的执行晚于“where,group by,order by”的执行。

如下代码:

select  
ROW_NUMBER() over(partition by customerID order by insDT) as rows, 
customerID,totalPrice, DID 
from OP_Order where insDT>'2011-07-22'

以上代码是先执行where子句,执行完后,再给每一条记录进行编号。

The above is the detailed content of Detailed explanation of the use of Partition By and row_number functions in SQL Server. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:jb51. If there is any infringement, please contact admin@php.cn delete
Reduce the use of MySQL memory in DockerReduce the use of MySQL memory in DockerMar 04, 2025 pm 03:52 PM

This article explores optimizing MySQL memory usage in Docker. It discusses monitoring techniques (Docker stats, Performance Schema, external tools) and configuration strategies. These include Docker memory limits, swapping, and cgroups, alongside

How to solve the problem of mysql cannot open shared libraryHow to solve the problem of mysql cannot open shared libraryMar 04, 2025 pm 04:01 PM

This article addresses MySQL's "unable to open shared library" error. The issue stems from MySQL's inability to locate necessary shared libraries (.so/.dll files). Solutions involve verifying library installation via the system's package m

How do you alter a table in MySQL using the ALTER TABLE statement?How do you alter a table in MySQL using the ALTER TABLE statement?Mar 19, 2025 pm 03:51 PM

The article discusses using MySQL's ALTER TABLE statement to modify tables, including adding/dropping columns, renaming tables/columns, and changing column data types.

Run MySQl in Linux (with/without podman container with phpmyadmin)Run MySQl in Linux (with/without podman container with phpmyadmin)Mar 04, 2025 pm 03:54 PM

This article compares installing MySQL on Linux directly versus using Podman containers, with/without phpMyAdmin. It details installation steps for each method, emphasizing Podman's advantages in isolation, portability, and reproducibility, but also

What is SQLite? Comprehensive overviewWhat is SQLite? Comprehensive overviewMar 04, 2025 pm 03:55 PM

This article provides a comprehensive overview of SQLite, a self-contained, serverless relational database. It details SQLite's advantages (simplicity, portability, ease of use) and disadvantages (concurrency limitations, scalability challenges). C

How do I configure SSL/TLS encryption for MySQL connections?How do I configure SSL/TLS encryption for MySQL connections?Mar 18, 2025 pm 12:01 PM

Article discusses configuring SSL/TLS encryption for MySQL, including certificate generation and verification. Main issue is using self-signed certificates' security implications.[Character count: 159]

Running multiple MySQL versions on MacOS: A step-by-step guideRunning multiple MySQL versions on MacOS: A step-by-step guideMar 04, 2025 pm 03:49 PM

This guide demonstrates installing and managing multiple MySQL versions on macOS using Homebrew. It emphasizes using Homebrew to isolate installations, preventing conflicts. The article details installation, starting/stopping services, and best pra

What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)?What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)?Mar 21, 2025 pm 06:28 PM

Article discusses popular MySQL GUI tools like MySQL Workbench and phpMyAdmin, comparing their features and suitability for beginners and advanced users.[159 characters]

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft