我怎样才能得到某个边缘神谕之间的时间

时间:2017-04-17 07:15:22

标签: sql oracle date hour

我需要获得一个时间段之间的小时数。 我的代码如下,但我没有得到我需要的结果。 因为它没有显示该范围内的所有结果

SELECT
    *
FROM
    (
        SELECT DISTINCT
            TO_DATE(TO_CHAR(date_quote,'dd/MM/yy'),'dd/MM/yy') date_quote,
            TO_DATE(TO_CHAR(start_time,'dd/MM/yyyy HH24:MI:SS'),'dd/MM/yyyy HH24:MI:SS') start_time,
            TO_DATE(TO_CHAR(end_time,'dd/MM/yyyy HH24:MI:SS'),'dd/MM/yyyy HH24:MI:SS') end_time

        FROM
            quote
        WHERE 
                start_time < to_timestamp('1970/01/01 11:30:00','yyyy/MM/dd HH24:MI:SS')
            and
                end_time > to_timestamp('1970/01/01 11:15:00','yyyy/MM/dd HH24:MI:SS') 

        AND
            date_quote= TO_CHAR(TO_DATE('2017-05-01','yyyy-MM-dd'),'dd/MM/yyyy')
    )
ORDER BY
    date_quote ASC,
    start_time ASC;

我也尝试过&gt; =&lt; =但是它没有给我预期的结果

2 个答案:

答案 0 :(得分:1)

您似乎想要计算关闭任务所需的总时间。  所以你可以使用

SELECT *
FROM
  (SELECT 
    TO_CHAR(TO_DATE(TO_CHAR(end_time,'dd/MM/yyyy HH24:MI:SS'),'dd/MM/yyyy HH24:MI:SS') - TO_DATE(TO_CHAR(start_time,'dd/MM/yyyy HH24:MI:SS'),'dd/MM/yyyy HH24:MI:SS'),'HH24:MI:SS') total_time_taken
  FROM quote
  WHERE start_time < to_timestamp('1970/01/01 11:30:00','yyyy/MM/dd HH24:MI:SS')
  AND end_time     > to_timestamp('1970/01/01 11:15:00','yyyy/MM/dd HH24:MI:SS')--- kinldy check your condition as end time is less then task start time
  AND date_quote   = TO_CHAR(TO_DATE('2017-05-01','yyyy-MM-dd'),'dd/MM/yyyy')
  )
ORDER BY date_quote ASC,
  start_time ASC;

答案 1 :(得分:0)

假设date_quotefecha_apunte_fhstart_timeend_time都是DATETIMESTAMP列,而start_time是{ end_time然后

SELECT *
FROM   (
  SELECT DISTINCT
         TRUNC( date_quote ) date_quote,
         start_time,
         end_time
  FROM   quote
  WHERE  start_time > TIMESTAMP '1970-01-01 11:15:00'
  AND    end_time   < TIMESTAMP '1970-01-01 11:30:00'
  AND    fecha_apunte_fh = DATE '2017-05-01'
)
ORDER BY
    date_quote ASC,
    start_time ASC;