【问题标题】:Select a period of time with to_date()使用 to_date() 选择时间段
【发布时间】:2019-12-13 12:48:05
【问题描述】:

我正在尝试选择一个特定的时间段(天和小时),以便计算人们观看特定频道的平均秒数。

我尝试了这个特定的功能:(to_date)

select  cust_id, ini_datetime,end_datetime, avg(qtd_seconds) as Avg_Sec, Channel

from crm.tmp_4

where   ini_datetime between ( to_date(2019-11-30 17:50:00.0, 'YYYY-MM-DD HH24:MI:SS.FF') and 
to_date(2019-11-30 18:00:00.0,'YYYY-MM-DD HH24:MI:SS.FF') ) 
    and end_datetime between ( to_date(2019-11-30 19:40:00.0,'YYYY-MM-DD HH24:MI:SS.FF') and to_date(2019-11-30 19:50:00.0,'YYYY-MM-DD HH24:MI:SS.FF') ) 

group by cust_id,ini_datetime,end_datetime, Channel

但它给了我这个错误:“ParseException line 3:50 cannot identify input near '17' ':' '50'”。 有没有人知道如何解决这个问题?

我也试过 to_timestamp 也没用。

谢谢!

【问题讨论】:

  • SQL 中的文字字符串必须用单引号括起来。

标签: oracle between to-date


【解决方案1】:

该值必须用引号括起来,因此您的查询应该是这样的:

select  cust_id, ini_datetime,end_datetime, avg(qtd_seconds) as Avg_Sec, Channel    
from crm.tmp_4    
where ini_datetime between ( to_date('2019-11-30 17:50:00', 'YYYY-MM-DD HH24:MI:SS') 
        and to_date('2019-11-30 18:00:00','YYYY-MM-DD HH24:MI:SS') ) 
    and end_datetime between ( to_date('2019-11-30 19:40:00','YYYY-MM-DD HH24:MI:SS') 
        and to_date('2019-11-30 19:50:00','YYYY-MM-DD HH24:MI:SS') )     
group by cust_id,ini_datetime,end_datetime, Channel

数据类型DATE 不支持小数秒。如果您需要它们,请使用 TO_TIMESTAMP 而不是 TO_DATE

【讨论】:

  • 好的,我将 where 子句更改为: where ini_datetime in (to_timestamp('2019-11-30 17:50:00.0', 'YYYY-MM-DD HH24:MI:SS.FF' ), to_timestamp('2019-11-30 18:00:00.0','YYYY-MM-DD HH24:MI:SS.FF') ) 和 end_datetime in (to_timestamp('2019-11-30 19:40:00.0 ','YYYY-MM-DD HH24:MI:SS.FF'), to_timestamp('2019-11-30 19:50:00.0','YYYY-MM-DD HH24:MI:SS.FF') ) 组通过 sa,ini_datetime,end_datetime;带有时间戳,因为我需要分数。但现在输出是“第 3:26 行无效函数'to_timestamp'”。是因为我的日期是“2019-11-30”而不是“30-Nov-19”吗?
  • timestamp '2019-11-30 17:50:00' 这样的standard literal 打字会少很多。
  • @Mary,我假设一个简单的错字。可能缺少引号或括号。
【解决方案2】:

你离得很近。 to_date 函数接受一个字符串并将其返回到日期。 您传递的不是字符串,而是一些对 Oracle 没有意义的字符。

您还需要删除between 和毫秒(.0.FF)处的括号,因为它们在to_date 中无效。

select cust_id, ini_datetime,end_datetime, avg(qtd_seconds) as Avg_Sec, Channel
from crm.tmp_4
where ini_datetime between  to_date('2019-11-30 17:50:00', 'YYYY-MM-DD HH24:MI:SS') and 
to_date('2019-11-30 18:00:00','YYYY-MM-DD HH24:MI:SS')  
    and end_datetime between  to_date('2019-11-30 19:40:00','YYYY-MM-DD HH24:MI:SS') and to_date('2019-11-30 19:50:00.0','YYYY-MM-DD HH24:MI:SS') 
group by cust_id,ini_datetime,end_datetime, channel;

【讨论】:

    【解决方案3】:

    您可以使用timestamp literals 使这更简单、更易于阅读:

    select cust_id, ini_datetime, end_datetime, channel
         , avg(qtd_seconds) as avg_sec
    from   crm.tmp_4
    where  ini_datetime between timestamp '2019-11-30 17:50:00' and timestamp '2019-11-30 18:00:00'
    and    end_datetime between timestamp '2019-11-30 19:40:00' and timestamp '2019-11-30 19:50:00'
    group  by cust_id, ini_datetime, end_datetime, channel
    

    【讨论】:

      猜你喜欢
      • 2012-08-17
      • 1970-01-01
      • 1970-01-01
      • 2014-11-16
      • 2022-01-16
      • 2019-07-30
      • 2023-03-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多