如果您希望该the_date
字段作为实际日期:
select trunc(date '1970-01-01' + datetimeorigination / (24*60*60)) as the_date,
to_char(date '1970-01-01' + datetimeorigination / (24*60*60),
'HH24') as the_hour,
count(record_id)
from table_a
group by trunc(date '1970-01-01' + datetimeorigination / (24*60*60)),
to_char(date '1970-01-01' + datetimeorigination / (24*60*60), 'HH24');
THE_DATE THE_HOUR COUNT(RECORD_ID)
--------- -------- ----------------
24-SEP-13 14 1
20-SEP-13 18 1
如果您希望小时值作为数字,您可以将该字段包装在to_number()
呼叫中。如果这是为了显示,那么您也应该明确格式化日期:
select to_char(date '1970-01-01' + datetimeorigination / (24*60*60),
'YYYY-MM-DD') as the_date,
to_char(date '1970-01-01' + datetimeorigination / (24*60*60),
'HH24') as the_hour,
count(record_id)
from table_a
group by to_char(date '1970-01-01' + datetimeorigination / (24*60*60),
'YYYY-MM-DD'),
to_char(date '1970-01-01' + datetimeorigination / (24*60*60), 'HH24');
THE_DATE THE_HOUR COUNT(RECORD_ID)
---------- -------- ----------------
2013-09-24 14 1
2013-09-20 18 1
或者将日期和时间的一个字段放在一起:
select to_char(date '1970-01-01' + datetimeorigination / (24*60*60),
'YYYY-MM-DD HH24') as the_hour,
count(record_id)
from table_a
group by to_char(date '1970-01-01' + datetimeorigination / (24*60*60),
'YYYY-MM-DD HH24');
THE_HOUR COUNT(RECORD_ID)
------------- ----------------
2013-09-24 14 1
2013-09-20 18 1
取决于您想看到什么以及您将如何处理它。
无论您使用哪个字段进行聚合,都需要在子句中以相同的方式指定它们group by
- 您不能使用位置符号,例如group by 1, 2
. 而且您已经意识到这些between
值必须按升序排列,否则根本找不到任何东西。