在分组结果集中,select 最新行。如果最新行中的特定字段为 0,则下一行 select

In grouped result set, select latest row. If particular field in the latest row is 0 then select next row

CREATE TABLE TableOne(Name VARCHAR(10), Val1 INT, Val2 INT, Val3 INT, RDate DATE)

INSERT TableOne VALUES ('Rec01', 0, 0, 0, '11-Nov-2017'), ('Rec02', 0, 20, 30, '10-Nov-2017'), ('Rec03', 0, 0, 0, '09-Nov-2017'), ('Rec01', 10, 20, 30, '11-Nov-2017'), ('Rec02', 10, 20 ,30, '10-Nov-2017'), ('Rec03', 0, 0, 30, '08-Nov-2017'), ('Rec01', 0, 20, 0, '10-Nov-2017'), ('Rec02', 10, 20, 0, '09-Nov-2017'), ('Rec03', 10, 0, 30, '08-Nov-2017')

TableOne

 -------------------------------------------
|  Name  | Val1 | Val2 | Val3 |    RDate    |
|-------------------------------------------|
| Rec01  |   0  |   0  |   0  | 11-Nov-2017 |
| Rec02  |   0  |  20  |  30  | 10-Nov-2017 |
| Rec03  |   0  |   0  |   0  | 09-Nov-2017 |
| Rec01  |  10  |  20  |  30  | 11-Nov-2017 |
| Rec02  |  10  |  20  |  30  | 10-Nov-2017 |
| Rec03  |   0  |   0  |  30  | 08-Nov-2017 |
| Rec01  |   0  |  20  |   0  | 10-Nov-2017 |
| Rec02  |  10  |  20  |   0  | 09-Nov-2017 |
| Rec03  |  10  |   0  |  30  | 08-Nov-2017 |
 ------------------------------------------- 

这些记录必须按 'Name' 分组,每组的最新记录需要 select 符合以下条件

1. If a record has two rows with same date, select first row. 
2. If a record has two rows with same date but Val1, Val2, Val3 are 0 then select next row with same date

预期结果是

 -------------------------------------------
|  Name  | Val1 | Val2 | Val3 |    RDate    |
|-------------------------------------------|
| Rec01  |  10  |  20  |  30  | 11-Nov-2017 |
| Rec02  |   0  |  20  |  30  | 10-Nov-2017 |
| Rec03  |   0  |   0  |   0  | 09-Nov-2017 |
 ------------------------------------------- 

我试过 ROW_NUMBER() OVER PARTITION BY

SELECT Name, Val1, Val2, Val3, RDate FROM (
SELECT Name, Val1, Val2, Val3, Max(RDate) AS RDate, ROW_NUMBER() OVER (PARTITION BY Name ORDER BY RDate DESC) AS Rno FROM TableOne GROUP BY Name, Val1, Val2, Val3, RDate) TempTable where TempTable.rno = 1

那个returns

 -------------------------------------------
|  Name  | Val1 | Val2 | Val3 |    RDate    |
|-------------------------------------------|
| Rec01  |   0  |   0  |   0  | 11-Nov-2017 |
| Rec02  |   0  |  20  |  30  | 10-Nov-2017 |
| Rec03  |   0  |   0  |   0  | 09-Nov-2017 |
 ------------------------------------------- 

对于 Rec01,我如何才能 select 与 Val1、Val2 和 Val3 具有相同日期的下一行是 0,而不是第一行?

谢谢

如果将 sum of vals 添加为 case 的第二个子句会怎样:

SELECT Name, Val1, Val2, Val3, RDate FROM (
SELECT Name, Val1, Val2, Val3, Max(RDate) AS RDate, ROW_NUMBER() OVER (PARTITION BY Name ORDER BY RDate DESC,val1+val2+val3 desc) AS Rno FROM TableOne GROUP BY Name, Val1, Val2, Val3, RDate) TempTable where TempTable.rno = 1

试试下面的查询

SELECT  Name,Val1,Val2,Val3,RDate
FROM    (
    SELECT  *   
        ,ROW_NUMBER() OVER(PARTITION BY NAME ORDER BY RDATE DESC
            ,(CASE WHEN Val1 +Val2 +Val3 > 0 THEN 1 ELSE 0 END) DESC) AS SlNo
    FROM    TableOne
)   AS  T
WHERE   T.SlNo = 1

输出:-

Name    Val1    Val2    Val3    RDate
Rec01   10      20      30      2017-11-11
Rec02   0       20      30      2017-11-10
Rec03   0       0       0       2017-11-09