1. 程式人生 > >SQL Server資料庫partition by 與ROW_NUMBER()函式使用詳解

SQL Server資料庫partition by 與ROW_NUMBER()函式使用詳解

關於SQL的partition by 欄位的一些用法心得

先看例子:

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'

-- 所有的資訊

SELECT * FROM TESTDB


A    B

-------

A1  B1

A1  B2

A1  B3

A2  B4

A2  B5

A2  B6

A3  B7

A3  B3

A3  B4

-- 使用PARTITION BY 函式後

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


可以看到結果中多出一列NUM 這個NUM就是說明了相同行的個數,比如A1有3個,他就給每個A1標上是第幾個。

-- 僅僅使用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

可以看到它只是單純標出了行號。


-- 深入一點應用

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


接下來我們就通過幾個例項來一一介紹ROW_NUMBER()函式的使用。

例項如下:

1.使用row_number()函式進行編號,如

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

原理:先按psd進行排序,排序完後,給每條資料進行編號。

2.在訂單中按價格的升序進行排序,並給每條記錄進行排序程式碼如下:

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

3.統計出每一個各戶的所有訂單並按每一個客戶下的訂單的金額 升序排序,同時給每一個客戶的訂單進行編號。這樣就知道每個客戶下幾單了。

如圖:

SQL Server資料庫ROW_NUMBER()使用詳解

程式碼如下:

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

4.統計每一個客戶最近下的訂單是第幾次下的訂單。

SQL Server資料庫ROW_NUMBER()使用詳解

程式碼如下:

  1.  with tabs as  
  2. (  
  3. select ROW_NUMBER() over(partition by customerID  order by totalPrice) as rows,customerID,totalPrice, DID from OP_Order  
  4.  )  
  5. select MAX(rows) as '下單次數',customerID from tabs group by customerID 

5.統計每一個客戶所有的訂單中購買的金額最小,而且並統計改訂單中,客戶是第幾次購買的。

如圖:

SQL Server資料庫ROW_NUMBER()使用詳解

上圖:rows表示客戶是第幾次購買。 

思路:利用臨時表來執行這一操作。

1.先按客戶進行分組,然後按客戶的下單的時間進行排序,並進行編號。

2.然後利用子查詢查找出每一個客戶購買時的最小价格。

3.根據查找出每一個客戶的最小价格來查詢相應的記錄。

程式碼如下:

  1. with tabs as  
  2.  (  
  3. select ROW_NUMBER() over(partition by customerID  order by insDT) as rows,customerID,totalPrice, DID from OP_Order  
  4. )  
  5.  select * from tabs  
  6. where totalPrice in   
  7. (  
  8. select MIN(totalPrice)from tabs group by customerID  
  9.  ) 

6.篩選出客戶第一次下的訂單。 

SQL Server資料庫ROW_NUMBER()使用詳解

思路。利用rows=1來查詢客戶第一次下的訂單記錄。

程式碼如下:

  1. with tabs as  
  2. (  
  3. select ROW_NUMBER() over(partition by customerID  order by insDT) as rows,* from OP_Order  
  4. )  
  5. select * from tabs where rows = 1
  6. select * from OP_Order 

7.rows_number()可用於分頁

思路:先把所有的產品篩選出來,然後對這些產品進行編號。然後在where子句中進行過濾。 

8.注意:在使用over等開窗函式時,over裡頭的分組及排序的執行晚於“where,group by,order by”的執行。

如下程式碼:

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

以上程式碼是先執行where子句,執行完後,再給每一條記錄進行編號。