【问题标题】:Fetching result from Oracle db从 Oracle 数据库中获取结果
【发布时间】:2020-03-03 11:59:12
【问题描述】:

我有一个数据库表 频率表如下 (companyid, task, and frequency) 是唯一键

 | companyid| task  |frequency | date        |
 | -------- | ----- | -------  | -----       |
 | 123      | abc   | Day      | 03-03-2020  |
 | 123      | def   | Week     | 09-03-2020  |
 | 456      | abc   | Week     | 10-03-2020  |
 | 456      | klm   | Week     | 12-03-2020  |

历史表如下 PRIMARY KEY("companyid", "RECORDNO")

| companyid| RECORDNO | STAGE    |  STATE |
| -------- | -----    | -------  |  ----- | 
| 123      | 12       | 1        |    P   |
| 456      | 13       | 2        |    A   |
| 123      | 13       | 1        |    S   |

条目表,唯一的(“companyid”,“ID”)

| companyid| ID       | STATE       |  task    |
| -------- | -----    | -----       |  -----   |
| 123      | 12       | Purchased   |  abc     |
| 123      | 13       | Accesible   |  def     |
| 456      | 13       |  Store      |  abc     |
| 456      | 14       |  Store      |  klm     |

我正在运行一项经常每 6 小时运行一次的作业。 我必须获取唯一的 companyids,其中历史表中的状态为“S”并且频率中的任务 = 条目表中的任务,并且记录号是历史表 = 条目表中的 id,并且当前系统日期大于频率表中的日期.

基本需求是处理日期小于当前系统日期的作业(条目表中的条目)。 (注意系统日期始终为 UTC,频率表中的日期可由用户配置)。 companyid 在所有表中都是通用的。

我已尝试使用以下查询来获取结果,而无需使用日期过滤器。 在正确获取时间过滤器方面需要帮助。

select distinct cny# 
from frequency 
where exists 
  (select 1 
   from history, entry  
   where history.companyid = entry.companyid 
   and history.state='S' 
   and history.record# = entry.id);

【问题讨论】:

    标签: mysql oracle oracle10g oracle-sqldeveloper


    【解决方案1】:

    您可以使用oraclesysdate函数获取数据库服务器的当前日期和时间,并在查询中使用。

    select distinct companyid 
    from frequency 
    where exists 
      (select 1 
       from history, entry  
       where history.companyid = entry.companyid 
       and history.state='S' 
       and history.recordno = entry.id)
    and sysdate > to_date(date,'DD-MM-YYYY');
    

    【讨论】:

      【解决方案2】:

      这不是三张表的简单连接吗?

      select distinct e.company_id
      from entry e 
      join history h on h.companyid = e.companyid and h,recordno = e.id
      join frequency f on f.companyid = e.companyid and f.task = e.task
      where f.date < trunc(sysdate)
      and h.state = 'S';
      

      或使用ON 子句中的条件:

      select distinct e.company_id
      from entry e 
      join history h on h.companyid = e.companyid and h,recordno = e.id and h.state = 'S'
      join frequency f on f.companyid = e.companyid and f.task = e.task and f.date < trunc(sysdate)
      ;
      

      不过,通常你会有一张公司表,所以你不需要DISTINCT

      select * from company where companyid in (select e.companyid from ...);
      

      【讨论】:

        猜你喜欢
        • 2015-11-09
        • 1970-01-01
        • 1970-01-01
        • 2012-05-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-04-28
        相关资源
        最近更新 更多