【问题标题】:Select Maximum Count From Group Of Events [duplicate]从事件组中选择最大计数[重复]
【发布时间】:2017-08-11 12:21:26
【问题描述】:

SQL 脚本

select eventid_nbr, trunc(received_date, 'DD'), sentindicator, count(eventid_nbr) as count
from eventlog
where received_date between to_date('2017-07-01, 10:00 A.M.','YYYY-MM-DD, HH:MI A.M.') and to_date('2017-07-01, 11:00 A.M.','YYYY-MM-DD, HH:MI A.M.')
group by eventid_nbr, trunc(received_date, 'DD'), sentindicator
order by count desc, trunc(received_date, 'DD');

有输出

EVENT  RECEIVED_DATE  SENTINDICATOR   COUNT

1      01-JUL-17      Y               128
1      01-JUL-17      E1              2
104    01-JUL-17      Y               55
105    01-JUL-17      Y               4
106    01-JUL-17      Y               3

因此我需要在每个事件中选择的最大计数,从而输出将显示为

 EVENT  RECEIVED_DATE  SENTINDICATOR   COUNT

 1      01-JUL-17      Y           128
 104    01-JUL-17      Y           55
 105    01-JUL-17      Y           4
 106    01-JUL-17      Y           3

对于每个分组,我需要选择 max(count) 吗?我怎样才能做到这一点?

【问题讨论】:

  • 我已经更新了问题,表明我需要选择包含与事件 1 相同的上述重复项的所有事件(可能是 200 个),并且仅包括其列 COUNT 为 MAX(COUNT) 的元组。对于非重复事件,例如 104、105 和 106,它们的元组包含在结果集中,因此它们的计数已经是 MAX(COUNT)

标签: sql oracle greatest-n-per-group


【解决方案1】:

在 Oracle 12c 中,您可以:

select eventid_nbr, trunc(received_date, 'DD'), sentindicator, 
       count(eventid_nbr) as count
from eventlog
where received_date between to_date('2017-07-01, 10:00 A.M.','YYYY-MM-DD, HH:MI A.M.') and
                            to_date('2017-07-01, 11:00 A.M.','YYYY-MM-DD, HH:MI A.M.')
group by eventid_nbr, trunc(received_date, 'DD'), sentindicator
order by count desc, trunc(received_date, 'DD')
fetch first 1 row only;

在早期版本中,子查询做同样的事情:

select el.*
from (select eventid_nbr, trunc(received_date, 'DD'), sentindicator, 
             count(eventid_nbr) as count
      from eventlog
      where received_date between to_date('2017-07-01, 10:00 A.M.','YYYY-MM-DD, HH:MI A.M.') and
                                  to_date('2017-07-01, 11:00 A.M.','YYYY-MM-DD, HH:MI A.M.')
      group by eventid_nbr, trunc(received_date, 'DD'), sentindicator
      order by count desc, trunc(received_date, 'DD')
     ) el
where rownum = 1;

【讨论】:

  • 您能否根据对问题的编辑进行详细说明以进一步澄清?
【解决方案2】:
select eventid,dt,sentindicator,max(count)
from 
 (
  select eventid_nbr, trunc(received_date, 'DD') as dt, sentindicator, 
  count(eventid_nbr) as count
  from eventlog
  where received_date between to_date('2017-07-01, 10:00 A.M.','YYYY-MM-DD, 
  HH:MI A.M.') 
  and to_date('2017-07-01, 11:00 A.M.','YYYY-MM-DD, HH:MI A.M.'
  )
    group by eventid_nbr, trunc(received_date, 'DD'), sentindicator
    order by count desc, trunc(received_date, 'DD'))a
group by eventid,dt,sentindicator

【讨论】:

  • 只需将具有 max(count) 的外部查询作为外部查询添加到您的查询中
【解决方案3】:
select eventid_nbr, received_day, max(sentindicator) keep(dense_rank last order by count), max(count) as count
from (select eventid_nbr, trunc(received_date, 'DD') as received_day,  sentindicator, count(*) as count
        from eventlog
       where received_date between to_date('2017-07-01, 10:00 A.M.','YYYY-MM-DD, HH:MI A.M.') and to_date('2017-07-01, 11:00 A.M.','YYYY-MM-DD, HH:MI A.M.')
       group by eventid_nbr, trunc(received_date, 'DD'), sentindicator)
group by eventid_nbr, received_day
order by 4 desc, received_day;

【讨论】:

    猜你喜欢
    • 2021-01-29
    • 2021-02-04
    • 1970-01-01
    • 1970-01-01
    • 2017-08-11
    • 1970-01-01
    • 2022-10-13
    • 1970-01-01
    • 2012-02-13
    相关资源
    最近更新 更多