Oracle: how to “group by” over a range?

后端 未结 10 1571
隐瞒了意图╮
隐瞒了意图╮ 2020-12-07 22:26

If I have a table like this:

pkey   age
----   ---
   1     8
   2     5
   3    12
   4    12
   5    22

I can \"group by\" to get a count

10条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-07 23:33

    I had to group data by how many transactions appeared in an hour. I did this by extracting the hour from the timestamp:

    select extract(hour from transaction_time) as hour
          ,count(*)
    from   table
    where  transaction_date='01-jan-2000'
    group by
           extract(hour from transaction_time)
    order by
           extract(hour from transaction_time) asc
    ;
    

    Giving output:

    HOUR COUNT(*)
    ---- --------
       1     9199 
       2     9167 
       3     9997 
       4     7218
    

    As you can see this gives a nice easy way of grouping the number of records per hour.

提交回复
热议问题