ORA-00936 When using date function in the oracle select statement

落花浮王杯 提交于 2019-12-23 04:29:00

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/19143376/ora-00936-when-using-date-function-in-the-oracle-select-statement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!