自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

SQL Server數(shù)據(jù)庫ROW_NUMBER()函數(shù)使用詳解

數(shù)據(jù)庫 SQL Server
本文我們主要介紹了SQL Server數(shù)據(jù)庫中ROW_NUMBER()函數(shù)的使用,并給出了大量的例子進(jìn)行詳細(xì)的說明,希望能夠?qū)δ兴鶐椭?/div>

SQL Server數(shù)據(jù)庫ROW_NUMBER()函數(shù)的使用是本文我們要介紹的內(nèi)容,接下來我們就通過幾個實例來一一介紹ROW_NUMBER()函數(shù)的使用。

實例如下:

1.使用row_number()函數(shù)進(jìn)行編號,如

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

原理:先按psd進(jìn)行排序,排序完后,給每條數(shù)據(jù)進(jìn)行編號。

2.在訂單中按價格的升序進(jìn)行排序,并給每條記錄進(jìn)行排序代碼如下:

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

3.統(tǒng)計出每一個各戶的所有訂單并按每一個客戶下的訂單的金額 升序排序,同時給每一個客戶的訂單進(jìn)行編號。這樣就知道每個客戶下幾單了。

如圖:

SQL Server數(shù)據(jù)庫ROW_NUMBER()使用詳解

代碼如下:

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

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

SQL Server數(shù)據(jù)庫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 '下單次數(shù)',customerID from tabs group by customerID 

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

如圖:

SQL Server數(shù)據(jù)庫ROW_NUMBER()使用詳解

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

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

1.先按客戶進(jìn)行分組,然后按客戶的下單的時間進(jìn)行排序,并進(jìn)行編號。

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

3.根據(jù)查找出每一個客戶的最小價格來查找相應(yīng)的記錄。

代碼如下:

  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數(shù)據(jù)庫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()可用于分頁

思路:先把所有的產(chǎn)品篩選出來,然后對這些產(chǎn)品進(jìn)行編號。然后在where子句中進(jìn)行過濾。 

8.注意:在使用over等開窗函數(shù)時,over里頭的分組及排序的執(zhí)行晚于“where,group by,order by”的執(zhí)行。

如下代碼:

  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' 

以上代碼是先執(zhí)行where子句,執(zhí)行完后,再給每一條記錄進(jìn)行編號。

關(guān)于SQL Server數(shù)據(jù)庫ROW_NUMBER()函數(shù)的使用就介紹到這里了,希望本次的介紹能夠?qū)δ兴鶐椭?/p>

【編輯推薦】

  1. SQL Server 2008數(shù)據(jù)庫學(xué)習(xí)筆記
  2. SQL Server 2005數(shù)據(jù)庫nolock使用詳解
  3. SQL Server如何啟用Ad Hoc Distributed Queries?
  4. SQL Server 2008用存儲過程實現(xiàn)插入更新數(shù)據(jù)的實例
  5. 含有GROUP BY子句的查詢中如何顯示COUNT()為0的結(jié)果

 

責(zé)任編輯:趙鵬 來源: 博客園
相關(guān)推薦

2011-08-30 10:53:05

SQL Serverrow_number(自動產(chǎn)生行號

2011-08-22 11:39:53

SQL Server數(shù)PIVOT

2011-08-11 09:12:31

SQL Server nolock

2021-03-18 08:20:19

SQLServer數(shù)據(jù)庫SQL

2011-08-24 12:49:56

SQL Server托管代碼

2011-08-22 13:04:47

SQL Server數(shù)函數(shù)

2011-08-15 14:29:52

SQL Server數(shù)事務(wù)

2011-08-22 13:28:56

FOR XMLSQL Server

2010-09-10 14:44:27

SQLROW_NUMBER(循環(huán)

2011-08-02 14:29:06

SQL Server數(shù)Substring函數(shù)

2010-07-15 17:28:50

SQL Server

2011-08-25 13:41:50

SQL Server 變更跟蹤

2011-08-24 09:15:36

SQL Server數(shù)FOR XML AUT

2010-03-16 10:12:40

SQL Server

2011-04-13 15:44:12

SQL Server數(shù)函數(shù)

2011-08-09 09:31:39

SQL Server數(shù)connectionS

2009-07-07 17:42:28

2011-08-22 10:47:09

SQL Server流水號

2011-08-30 11:04:30

鏈接查詢內(nèi)連接外連接

2010-09-09 14:31:31

SQL函數(shù)數(shù)據(jù)庫
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號