TSQL - 需要计算'更高的高点'

时间:2013-08-18 13:48:47

标签: sql-server tsql counting

这是一个名为HIGHVALS的2列表。我正在寻找TSQL来计算B列中较高的高数。第1行开始计数为1.在第2行,计数为2,因为1525.34是>比1520.08。第3行和第4行对计数没有贡献,因为它们都是< 1525.34最后一个最高值。第5行对计数有贡献,因为1543.47> 1525.34最后一个最高值。计数的最终结果应为14.

A,  B  
1,  1520.08  
2,  1525.34  
3,  1519.99  
4,  1525.27  
5,  1543.47  
6,  1545.25  
7,  1545.78  
8,  1552.48  
9,  1556.27  
10, 1556.77  
11, 1556.39  
12, 1563.32  
13, 1563.62  
14, 1560.7  
15, 1557.25  
16, 1561.56  
17, 1558.71  
18, 1557.74  
19, 1564.91  
20, 1563.95  
21, 1564.07  
22, 1570.28  
23, 1570.57  
24, 1573.66  

向此添加最后一项。表中有上述的多个实例。 C列提供了分组依据的唯一键。我可以根据添加C列获得更多指导吗?与以前相同的要求,仅计算B列中较高的高值,但现在按C列分组。

COL_A,COL_B,COL_C
21037,1345.00,21037
21038,1341.29,21037
21039,1357.7,21039
21040,1357.26,21039
21041,1365.36,21039
21042,1375.26,21039
21043,1380.39,21039
21044,1376.51,21039
21045,1362.34,21045
21046,1351.53,21045
21047,1343.98,21045
21048,1363.13,21048
21049,1389.18,21048
21050,1391.74,21048
21051,1387.16,21048
21052,1385.03,21048
21053,1375.13,21048
21054,1394.16,21048
21055,1399.63,21048
21056,1407.14,21048
21057,1404.14,21048
21058,1405.95,21048
21059,1405.98,21048
21060,1405.87,21048
21061,1410.03,21048
21062,1407.73,21048
21063,1417.43,21048
21064,1418.71,21048
21065,1418.13,21048
21066,1426.68,21048
21067,1416.12,21048
21068,1413.49,21048
21069,1413.46,21048
21070,1416.17,21048
21071,1413.63,21048
21072,1413.95,21048
21073,1410.08,21073

我感谢大家的建议。

4 个答案:

答案 0 :(得分:4)

如果您使用的是SQL Server 2012或更高版本,请考虑max() over ()窗口函数:

select  count(*)
from    (
        select  max(B) over (order by A rows between
                             unbounded preceding and 1 preceding) as PrevMax
        ,       B
        from    Table1
        ) as SubQueryAlias
where   B > PrevMax -- Larger than previous maximum
        or PrevMax is null -- Or first row

Live example at SQL Fiddle.

对于以前版本的SQL Server,您可以使用left join来计算PrevMax

select  count(*)
from    (
        select  max(prev.B) as PrevMax
        ,       min(cur.B) as B
        from    Table1 cur
        left join
                Table1 prev
        on      prev.A < cur.A
        group by
                cur.A
        ) as SubQueryAlias
where   B > PrevMax
        or PrevMax is null

Live example at SQL Fiddle.

答案 1 :(得分:3)

试试这个:

SELECT t.C, HigherHighCount = COUNT(*)
FROM Table1 t
WHERE NOT EXISTS(SELECT * FROM Table1 WHERE [A] < t.[A] AND [B] > t.[B] AND [C] = t.[C])
GROUP BY t.C;

编辑:添加C列。

结果:

C           HigherHighCount
----------- ---------------
21037       1
21039       4
21045       1
21048       10
21073       1

另见fiddle example

答案 2 :(得分:0)

使用CTE替代方案:

;WITH Table1WithMaxPrevEntries AS
(   
    SELECT *, 
        ISNULL((select MAX(prev.b) FROM Table1 previous WHERE previous.a < [current].a ), 0) AS PreviousB_Max
    FROM Table1 AS [current]
)
SELECT COUNT(*)
FROM Table1WithMaxPrevEntries 
WHERE b > PreviousB_Max

答案 3 :(得分:0)

通过col_c

添加组的要求
select col_c, count(*) 
from 
(
    select s2.A
      from table s1 with (nolock) 
      join table s2 with (nolock) 
        on s2.A > s1.A
           and s2.col_c = s1.col_c 
     group by s1.col_c, s2.A, s2.B
    having s2.B > max(s1.B)
) ccount