简体   繁体   English

选择范围内具有最大值的条目数

[英]Select the count of entries that have Max value in a range

I need to get the count of myIndex which it has the max Value per hour, for example:我需要获取每小时具有最大值的 myIndex 的计数,例如:

MyIndex f_hour  maxi
8187    10  70**  ++1 for 10h and 8187
8178    10  50
8184    10  46
8190    10  46
8180    10  40
8179    10  33
8185    10  30
8183    10  26
8181    10  23
8182    10  20
8186    10  20
8177    10  13
8189    10  6
8188    10  3
**8188  11  80** ++1 for 11h and index 8188
8187    11  60
8180    11  53
8186    11  50
8179    11  46
8190    11  46
8178    11  43
8181    11  33
8184    11  33
8189    11  33
8183    11  26
8185    11  23
8182    11  16
8177    11  13
**8187  12  73** now 8187 has at 10h the max value and now at 12h too, so 2 !! 
8188    12  66
8179    12  60
8190    12  56

the result will be like结果会像

MyIndex     Count
8187          2 (times)
8188          1 (time)

How can I do this?我怎样才能做到这一点?

This is a histogram of histogram query.这是直方图查询的直方图。 You seem to want to count the number of hours that each value appears as a maximum.您似乎想要计算每个值作为最大值出现的小时数。 This ends up being a double group by :这最终成为一个双group by

select maxvalue, count(*) as numhours
from (select hour, max(value) as maxvalue
      from table t
      group by hour
     ) t
group by maxvalue
order by maxvalue;

EDIT:编辑:

I see, you don't want the histogram of values but of indexes.我明白了,你不想要值的直方图,而是索引的直方图。 You can do that with window functions, which are ANSI standard and available in most (but not all) databases:您可以使用窗口函数来做到这一点,这些函数是 ANSI 标准的,可在大多数(但不是全部)数据库中使用:

select myindex, count(*) as numhours
from (select t.*, row_number() over (partition by hour order by value desc) as seqnum
      from table t
      group by hour
     ) t
where seqnum = 1
group by myindex
order by myindex;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM