zoukankan      html  css  js  c++  java
  • 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 = 
    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子句,执行完后,再给每一条记录进行编号。

  • 相关阅读:
    20155339 Exp9 Web安全基础
    20155339 Exp8 Web基础
    20155339 Exp7 网络欺诈防范
    20155339 Exp6 信息搜集与漏洞扫描
    20155339 Exp5 MSF基础应用
    20155339 Exp4 恶意代码分析
    20155339 Exp3 免杀原理与实践
    20155339平措卓玛 Exp2 后门原理与实践
    20155339平措卓玛 Exp1 PC平台逆向破解(5)M
    20155339 第16周课堂实践加分作业
  • 原文地址:https://www.cnblogs.com/wwzhang/p/7827546.html
Copyright © 2011-2022 走看看