


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:
ps: Detailed explanation of the use of SQL Server database partition by and ROW_NUMBER() functions
Some usage experience of SQL partition by fieldRead 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 9You 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_CustomerPrinciple: 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 customerID5. 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.根据查找出每一个客户的最小价格来查找相应的记录。 代码如下: 6.筛选出客户第一次下的订单。 思路。利用rows=1来查询客户第一次下的订单记录。 代码如下: 7.rows_number()可用于分页 思路:先把所有的产品筛选出来,然后对这些产品进行编号。然后在where子句中进行过滤。 8.注意:在使用over等开窗函数时,over里头的分组及排序的执行晚于“where,group by,order by”的执行。 如下代码: 以上代码是先执行where子句,执行完后,再给每一条记录进行编号。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
)
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
select
ROW_NUMBER() over(partition by customerID order by insDT) as rows,
customerID,totalPrice, DID
from OP_Order where insDT>'2011-07-22'
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!

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

SublimeText3 Linux new version
SublimeText3 Linux latest version

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),

Zend Studio 13.0.1
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.