取决于时间的变量日期

问题描述:

我有个问题。我有一个查询将作为夜间工作的一部分运行。这个查询应该会给我所有在那一天发生的所有操作。但是,这是棘手的部分,它不会总是在同一时间运行。取决于时间的变量日期

因为它是夜间工作的一部分,所以可能发生在第1天,查询在00:05运行,第2天运行在23:55。这使查询变得复杂,因为我不能说使用今天的日期或使用昨天的日期。

我得到了下面的查询到目前为止:

select deuda_id from deuda where n_expediente in 
    (select 
     (case when to_char(sysdate, 'HH24:MI:SS') between '00:00:00' and '12:00:00' then 
      (select n_expediente from cartas_enviadas where codigo_carta in ('OIEUR','OIGBP') and f_envio > trunc(sysdate-1) 
      ) else 
      (select n_expediente from cartas_enviar where codigo_carta in ('OIEUR','OIGBP') 
      ) 
     ) from dual 
    ); 

一点解释:(数据库处于西班牙/意大利): deuda_id是唯一的发票号。 n_expediente是案例编号(对于此客户始终是唯一的)。 f_envio是执行日期。 codigo_carta是动作类型。

Cartas_enviar包含所有到期的操作。当采取行动时,输入f_envio。一夜之间,所有已执行的操作将从cartas_enviar移至cartas_enviadas)。

笏我试图做的是以下几点:

我想看看在当前时间。如果是在午夜之前,我想看看cartas_enviar表,然后从那里取出n_expediente,但前提是动作是OIEUR或OIGBP。如果是在午夜之后,我想看看cartas_enviadas表,并从那里取出n_expediente,但前提是动作是OIEUR或OIGBP,并且该动作是在昨天执行的。

但是,当我试图执行此查询时,出现以下错误消息: ORA-00905: missing keyword

有人可以帮我看看这个查询吗?

PS:这是Oracle数据库

+0

好的,菜鸟的错误......我忘了把'结束'放在声明中。但是,现在我得到的错误ORA-01427:单行子查询返回多个行 – Gurbs

+0

我编辑了我的答案,检查它是否适用于您。 –

的问题是,你不能从一个子查询的子查询拉一样,许多值,将其传回。尝试这种方法,而不是:

select deuda_id 
    from deuda 
where 
    (to_char(sysdate, 'HH24:MI:SS') between '00:00:00' and '12:00:00' 
    AND n_expediente IN (select n_expediente 
         from cartas_enviadas 
         where codigo_carta in ('OIEUR','OIGBP') 
         and f_envio > trunc(sysdate-1))) 
    OR (NOT to_char(sysdate, 'HH24:MI:SS') between '00:00:00' and '12:00:00' 
    AND n_expediente IN (select n_expediente 
          from cartas_enviar 
          where codigo_carta in ('OIEUR','OIGBP'))) 
;