If you want the the_date
field as an actual 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
If you want the hour value as a number you can wrap that field in a to_number()
call. If this is for display then you should explicitly format the date as well:
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
Or with one field for the date and time together:
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
Depends what you want to see and what you're going to do with it.
Whichever fields you use for the aggregation, you need to specify them the same way in the group by
clause - you can't use positional notation, e.g. group by 1, 2
. ANd you already realised that the between
values have to be in ascending order or it won't find anything at all.
DATE()
,HOUR()
andFROM_UNIXTIME()
are not built-in Oracle functions. You can't always use code from another RDBMS directly. Though I guess you could have created them as functions yourself. What output do you want - that will determine the most appropriate equivalent? You also can't use positional notation in thegroup by clause
. – Anus