排名函数SQL Server 2014中的多种排序方式

时间:2017-02-22 17:20:56

标签: sql sql-server

LN_NUMBER       RUN_DATE    SALE_DATE   STATUS_CODE TYPE    PROCESSOR_ID    
------------------------------------------------------------------------
0201203909      02/21/17    09/30/15        R   1      TG1          
0201203909      02/21/17                    R   2      TG1      
0201203909      02/21/17                    A   1      MW1  -------choose this row for LN_number 0201203909   
0201105919      02/21/17    08/24/16        S   2      MW1  ---choose this row for LN_number 0201105919
0201105919      02/21/17    08/24/16        S   2      MW1  
0201105919      02/21/17    07/01/15        R   2      TG1  
1400000138      02/21/17-----------------------------------------------------------Choose this for LN_NUMBER 1400000138  

我有一个名为closure的表,如上所示。我需要从闭包表中选择记录。

条件是:

  • 如果状态代码= A,则选择该行(将其设为RowNum = 1)。
  • 如果状态代码不是A,则选择最近SALE_DATE的记录(将其设为RowNum = 1)。

我试过这段代码

SELECT
    [LN_NUMBER],
    [RUN_DATE] , 
    [SALE_DATE], 
    [STATUS_CODE], 
    [TYPE],
    [PROCESSOR_ID],
    ROW_NUMBER() OVER (partition by [LN_NUMBER]  order by 
                     case when [STATUS_CODE]='A' then [STATUS_CODE] end asc,
                     case when [STATUS_CODE]!='A' then CONVERT(DATE,[SALE_DATE]) end desc)               
              as 'RowNum' 
FROM [dbo].[Closure ] 

此代码适用于LN_NUMBER = 0201105919。但对于LN_NUMBER = 0201203909,我没有按要求的方式获取rowNum。

这就是我得到的结果:

LN_NUMBER   RUN_DATE    SALE_DATE   STATUS_CODE TYPE PROCESSOR_ID   RowNum
0201203909   02/21/17   09/30/15     R          1     TG1           1
0201203909   02/21/17                R          2     TG1           2
0201203909   02/21/17                A          1     MW1           3
0201105919   02/21/17   08/24/16     S         2      MW1           1
0201105919   02/21/17   08/24/16     S         2      MW1           2
0201105919   02/21/17   07/01/15     R         2      TG1           3

有谁可以请帮助我。我正在使用SQL Server 2014.非常感谢你的时间和帮助

1 个答案:

答案 0 :(得分:2)

select  
  [LN_NUMBER],
  [RUN_DATE]  ,  
  [SALE_DATE],  
  [STATUS_CODE],  
  [TYPE],
  [PROCESSOR_ID],
  ROW_NUMBER () over (
    partition by [LN_NUMBER] 
    order by case when [STATUS_CODE]='A' then 0 else 1 end asc
      , CONVERT(DATE,[SALE_DATE]) desc
  )  as  'RowNum'  
FROM  [dbo].[Closure]  

测试设置:http://rextester.com/FNRE71956

create table Closure ( 
    LN_NUMBER varchar(10)
  , RUN_DATE  varchar(10)
  , SALE_DATE varchar(10)
  , STATUS_CODE char(1)
  , TYPE int
  , PROCESSOR_ID char(3)
)
insert into t values
 ('0201203909','02/21/17','09/30/15','R',1,'TG1')
,('0201203909','02/21/17','        ','R',2,'TG1')
,('0201203909','02/21/17','        ','A',1,'MW1')
,('0201105919','02/21/17','08/24/16','S',2,'MW1')
,('0201105919','02/21/17','08/24/16','S',2,'MW1')
,('0201105919','02/21/17','07/01/15','R',2,'TG1')

查询:

select  
  [LN_NUMBER],
  [RUN_DATE]  ,  
  [SALE_DATE],  
  [STATUS_CODE],  
  [TYPE],
  [PROCESSOR_ID],
  ROW_NUMBER () over (
    partition by [LN_NUMBER] 
    order by case when [STATUS_CODE]='A' then 0 else 1 end asc
      , CONVERT(DATE,[SALE_DATE]) desc
  )  as  'RowNum'  
FROM  [dbo].[Closure]  

结果:

+------------+----------+-----------+-------------+------+--------------+--------+
| LN_NUMBER  | RUN_DATE | SALE_DATE | STATUS_CODE | TYPE | PROCESSOR_ID | RowNum |
+------------+----------+-----------+-------------+------+--------------+--------+
| 0201105919 | 02/21/17 | 08/24/16  | S           |    2 | MW1          |      1 |
| 0201105919 | 02/21/17 | 08/24/16  | S           |    2 | MW1          |      2 |
| 0201105919 | 02/21/17 | 07/01/15  | R           |    2 | TG1          |      3 |
| 0201203909 | 02/21/17 |           | A           |    1 | MW1          |      1 |
| 0201203909 | 02/21/17 | 09/30/15  | R           |    1 | TG1          |      2 |
| 0201203909 | 02/21/17 |           | R           |    2 | TG1          |      3 |
+------------+----------+-----------+-------------+------+--------------+--------+