Pergunta

Here is a 2 column table called HIGHVALS. I am looking for TSQL to count the number of higher highs in column B. Row 1 starts the counting at 1. At row 2 the count is 2 because 1525.34 is > than 1520.08. Rows 3 and 4 do not contribute to the count because they are both < 1525.34 the last highest value. Row 5 contributes to the count because 1543.47 is > 1525.34 the last highest value. The end result of the counting should be 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  

Adding one last item to this. There are multiple instances of the above in the table. Column C provides the unique key to group by. Could I get a little more guidance based on the addition of Column C? Same requirements as before, count only the higher highs in column B, but now group by column 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

I appreciate everyone's suggestions.

Foi útil?

Solução 2

Try this:

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;

EDIT: addition of column C.

Results:

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

Also see fiddle example.

Outras dicas

If you are on SQL Server 2012 or higher, consider the max() over () window function:

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.

For previous editions of SQL Server, you could use a left join to calculate 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.

using CTE alternative:

;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

with the addition requirement of group by 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
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top