Oracle SQL

I'm working in an Oracle DB and I'm trying to convert from 12 hours to 24 hours.

I have currently updated the HH to HH24 and I'm still seeing the full 24hour time and in fact it's only pulling the first 12 hours of the day. The below is my working query with no errors and output correct results besides the other missing 12 hours.

SELECT
CASE(EXTRACT(HOUR FROM a.TIME)) 
WHEN 1 THEN '1'
WHEN 2 THEN '2'
WHEN 3 THEN '3'
WHEN 4 THEN '4'
WHEN 5 THEN '5'
WHEN 6 THEN '6'
WHEN 7 THEN '7'
WHEN 8 THEN '8'
WHEN 9 THEN '9'
WHEN 10 THEN '10'
WHEN 11 THEN '11'
WHEN 12 THEN '12'
WHEN 13 THEN '13'
WHEN 14 THEN '14'
WHEN 15 THEN '15'
WHEN 16 THEN '16'
WHEN 17 THEN '17'
WHEN 18 THEN '18'
WHEN 19 THEN '19'
WHEN 20 THEN '20'
WHEN 21 THEN '21'
WHEN 22 THEN '22'
WHEN 23 THEN '23'
WHEN 24 THEN '24'
ELSE 'OTHERS' END AS "ENTRY_TIME_HOUR",
COUNT(*) AS "TOTAL_WITHIN_THE_HOUR"

 FROM Table1 a 
 WHERE 
 a.TIME >= TO_DATE('2018/01/05 01:00:01', 'YYYY/MM/DD HH24:MI:SS') 
 AND a.TIME <= TO_DATE('2018/01/05 12:59:59', 'YYYY/MM/DD HH24:MI:SS')
 GROUP BY EXTRACT(HOUR FROM a.TIME)

The above query would output something like below but only up to 12 (should be 1-24)

ENTRY_TIME_HOUR  TOTAL_WITHIN_THE_HOUR
   11                    68
   8                      3
   9                     83
  10                     26
  12                     62

In addition, I have found the TO_CHAR to be useful but I could not run the TO_CHAR and TO_DATE together. The ultimate goal is to SUM all outputs on the TIME for the given hour. The output would return 24 lines with a total count for each hour.

The below is the seperate query that would output the FULL time (not just the hour HH):

SELECT
to_char(a.TIME, 'DD/MM/YYYY HH24:MI:SS') AS "TIME in 24"
FROM TABLE a 
WHERE 
AND a.TIME >= TO_DATE('2018/01/05 01:00:01', 'YYYY/MM/DD HH24:MI:SS') 
AND a.TIME <= TO_DATE('2018/01/05 23:59:59', 'YYYY/MM/DD HH24:MI:SS')

The above query would provide like the below:

 05/01/2018 15:00:40
 05/01/2018 16:01:45
 05/01/2018 09:59:51
 05/01/2018 10:04:58

However, I'm not able to merge the TO_CHAR and TO_DATE queries together without running into multiple issues. Would it be possible to merge the second query with the first query the provide the count results of the hour to the full 24 hours of the day?

Thanks


First, the hours of the day go from 0 to 23, not 1 to 24.

Second, you are complicating simple things. What's wrong with

select to_char(a.time, 'HH24') theHour
, count(*) occurrences

from yourTable a

where a.time >= date '2018-01-05'
and a.time < date '2018-01-06'
group by to_char(a.time, 'HH24')

This is too long for a comment. Start by learning to use the DATE and TIMESTAMP keywords. Much simpler for inputting unambiguous timestamps:

WHERE a.TIME >= TIMESTAMP '2018-01-05 01:00:01' AND
      a.TIME <= TIMESTAMP '2018-01-05 12:59:59'

Written like this, it is much clearer that you are only choosing hours between 1 and 12, which is why you are only getting those hours. Change the WHERE conditions and you might get additional hours.

链接地址: http://www.djcxy.com/p/36918.html

上一篇: Yii2 Oracle日期列

下一篇: Oracle SQL