选择Progressive更大的记录

时间:2018-05-22 10:52:34

标签: sql sql-server tsql

使用T-SQL我有一个查询从一系列表中提取以下信息。根据这些信息,我需要选择运行逐渐变大的行。

Runs    Name         Opposition              Ground          Date       RN
4   Wickramanayake  Ellerslie          Devonport Domain 25-26/11/2016   1
51  Mahoney         Ellerslie          Devonport Domain 25-26/11/2016   2
157 Beghin          Ellerslie          Devonport Domain 25-26/11/2016   3
10  Olsen           Ellerslie          Devonport Domain 25-26/11/2016   4
9   Thomson         Ellerslie          Devonport Domain 25-26/11/2016   5
12  Chitongo        Ellerslie          Devonport Domain 25-26/11/2016   6
40  McKenzie        Ellerslie          Devonport Domain 25-26/11/2016   7
1   Wall            Ellerslie          Devonport Domain 25-26/11/2016   8
0   Duncraft        Ellerslie          Devonport Domain 25-26/11/2016   9
0   Beghin          Ellerslie          Devonport Domain 25-26/11/2016   10
0   Olsen           Ellerslie          Devonport Domain 25-26/11/2016   11
0   Thomson         Ellerslie          Devonport Domain 25-26/11/2016   12
0   Chitongo        Ellerslie          Devonport Domain 25-26/11/2016   13
0   McKenzie        Ellerslie          Devonport Domain 25-26/11/2016   14
0   Hinds           Ellerslie          Devonport Domain 25-26/11/2016   15
0   Wall            Ellerslie          Devonport Domain 25-26/11/2016   16
0   Brungar         Ellerslie          Devonport Domain 25-26/11/2016   17
0   Duncraft        Ellerslie          Devonport Domain 25-26/11/2016   18
2   Beghin          Suburbs New Lynn    Vauxhall Road   7/10/2017   19
0   Wickramanayake  Suburbs New Lynn    Vauxhall Road   7/10/2017   20
7   Olsen           Suburbs New Lynn    Vauxhall Road   7/10/2017   21
15  Thomson         Suburbs New Lynn    Vauxhall Road   7/10/2017   22
0   Hinds           Suburbs New Lynn    Vauxhall Road   7/10/2017   23
34  Hira            Suburbs New Lynn    Vauxhall Road   7/10/2017   24
0   Wall            Suburbs New Lynn    Vauxhall Road   7/10/2017   25
5   Brungar         Suburbs New Lynn    Vauxhall Road   7/10/2017   26
8   Duncraft        Suburbs New Lynn    Vauxhall Road   7/10/2017   27
0   Strain          Suburbs New Lynn    Vauxhall Road   7/10/2017   28
74  Beghin          Ellerslie           Vauxhall Road   14/10/2017  29

所以结果集(使用这个数据)只包含前三行,因为第一个最高的运行是4,然后是下一个最高的是51.那么下一个最高的是157.而且因为没有其他更高的运行不会有157个记录。

我希望这是有道理的。我确实喜欢SQL挑战,但有时我会挣扎.. lol

1 个答案:

答案 0 :(得分:2)

我将此解释为从第一行开始,获取值增加的后续行。事实上,这些是示例中的前三行是巧合。

如果我理解正确,您需要定义累积最大值的行:

select t.*
from (select t.*, max(runs) over (order by rn) as cumalative_runs
      from t
     ) t
where cumulative_runs = runs;