0

我有一个问题。我有一个查询将作为夜间工作的一部分运行。这个查询应该给我那天发生的所有动作。然而,这是棘手的部分,它不会总是同时运行。

因为它是夜间工作的一部分,所以可能会在第 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数据库

4

1 回答 1

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')))
;
于 2013-10-17T13:58:34.603 回答