Pergunta

I have a query that I'm trying to aggregate data based on hours between two unix timestamps in Oracle. Hardest part is that I get error'd out with "ORA-00936: missing expression" error even I don't see anything wrong in the query. Need some expert advice here. Below is the query -

Query -

select DATE(FROM_UNIXTIME(C.DATETIMEORIGINATION)) the_date,
HOUR(FROM_UNIXTIME(C.DATETIMEORIGINATION)) the_hour,
count(c.RECORD_ID) the_count 
FROM TABLE_A C 
WHERE C.DATETIMEORIGINATION between 1380033019 AND 1379702408 
GROUP BY 1,2;

Any help is greatly appreciated. Thanks

Foi útil?

Solução

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top