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

后端 未结 10 1560
隐瞒了意图╮
隐瞒了意图╮ 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:19

    I had to get a count of samples by day. Inspired by @Clarkey I used TO_CHAR to extract the date of sample from the timestamp to an ISO-8601 date format and used that in the GROUP BY and ORDER BY clauses. (Further inspired, I also post it here in case it is useful to others.)

    SELECT 
      TO_CHAR(X.TS_TIMESTAMP, 'YYYY-MM-DD') AS TS_DAY, 
      COUNT(*) 
    FROM   
      TABLE X
    GROUP BY
      TO_CHAR(X.TS_TIMESTAMP, 'YYYY-MM-DD')
    ORDER BY
      TO_CHAR(X.TS_TIMESTAMP, 'YYYY-MM-DD') ASC
    /
    

提交回复
热议问题