当每个表的日期都被修改时,从不同的表中获取数据

时间:2018-08-17 20:17:11

标签: oracle join

关于我目前正在努力解决的一种情况,喜欢从大家那里获得一些意见。以下是问题陈述

我有表A,B,C

A在下面的列中 用户|修改日期| wokred_on A | ..一些与用户操作有关的

B有列 用户|修改日期|在B工作| ..其他一些列

C有列 用户|修改日期|在C |上工作..其他一些列

除了用户之外,这些表没有任何直接关系。

我们必须使用count op操作或用户在给定日期范围内完成的工作从给定日期之间的用户中提取这些表中的数据?

我在这里遇到的困难是,如果选择的日期范围不在其他列中,但是每个表都有自己修改的日期,但是由于用户在两个日期之间进行处理,我仍然需要提取数据。

是否可以选择这些日期并将in放在一列中,以便可以将其放在where子句中并具有外部联接以拉出其他记录?

很抱歉出现这个大问题。任何建议都非常感谢

下面是一个用例。只需扩展littlefoot给出的假设

首先,测试用例:

SQL> create table a (cuser varchar2(10), modified_date date,action );

varchar2(10))     表已创建。

SQL> create table b (
Table created.cuser varchar2(10), modified_date date,action 

varchar2(10));

SQL> create table c (cuser varchar2(10), modified_date date,action 

varchar2(10)));

Table created.

SQL> insert into a values ('lf', date '2018-05-01', 'issue raised');

1 row created.

SQL> insert into a values ('mc', date '2018-05-01', 'issue raised ');

1 row created.

SQL> insert into b values ('lf', date '2018-05-01',issue raised');

1 row created.

SQL> insert into b values ('lf', date '2018-05-01','issue resolved');

1 row created.

SQL> insert into c values ('if', date '2018-05-28',' issue resolved');

1 row created.

SQL> insert into c values ('mc', date '2018-05-13','issue raised');

1 row created.

SQL> insert into c values ('mc', date '2018-05-13','issue resolved');

1 row created.

SQL> alter session set nls_date_format = 'yyyy-mm-dd';

Session altered.

SQL> select * from a;

CUSER      MODIFIED_D. ACTION 
---------- ----------
lf         2018-05-01     issue raised
mc         2018-05-01    issue raised

SQL> select * from b;

CUSER      MODIFIED_D    ACTION
---------- ----------.         ______________
lf         2018-05-01    issue raised
lf         2018-05-01.  issue resolve 

SQL> select * from c;

CUSER      MODIFIED_D. ACTION
---------- ----------
If         2018-05-28.      issue resolve
mc         2018-05-13.    issue raised
mc         2018-05-13.    issue resolve

CUSER    DATE             CNT_ISSUE_RAISED   CNT_ISSUE_RESOLVED 

------ ------- --------------- -------------------
    如果2018-05-01 2 1
    lf 2018-05-28 0 1
    mc 2018-05-01 0 1
    mc 2018-05-13 1 1

1 个答案:

答案 0 :(得分:0)

这就是我对问题的理解。

首先,测试用例:

SQL> create table a (cuser varchar2(10), modified_date date);

Table created.

SQL> create table b (cuser varchar2(10), modified_date date);

Table created.

SQL> create table c (cuser varchar2(10), modified_date date);

Table created.

SQL> insert into a values ('lf', date '2018-05-01');

1 row created.

SQL> insert into a values ('mc', date '2018-05-15');

1 row created.

SQL> insert into b values ('lf', date '2018-05-07');

1 row created.

SQL> insert into b values ('lf', date '2018-05-08');

1 row created.

SQL> insert into c values ('jw', date '2018-05-28');

1 row created.

SQL> insert into c values ('mc', date '2018-05-13');

1 row created.

SQL> insert into c values ('mc', date '2018-05-22');

1 row created.

SQL> alter session set nls_date_format = 'yyyy-mm-dd';

Session altered.

SQL> select * from a;

CUSER      MODIFIED_D
---------- ----------
lf         2018-05-01
mc         2018-05-15

SQL> select * from b;

CUSER      MODIFIED_D
---------- ----------
lf         2018-05-07
lf         2018-05-08

SQL> select * from c;

CUSER      MODIFIED_D
---------- ----------
jw         2018-05-28
mc         2018-05-13
mc         2018-05-22

返回期望结果的查询-每个表中每个用户在期望日期范围内的行数。当我使用SQL * Plus时,变量前面带有&&,以避免出现多个插入请求。在您使用的工具中,可能是冒号(:)。

SQL> select nvl(nvl(a.cuser, b.cuser), c.cuser) cuser,
  2         count(distinct a.modified_date) cnt_a,
  3         count(distinct b.modified_date) cnt_b,
  4         count(distinct c.modified_date) cnt_c
  5  from a full outer join b on a.cuser = b.cuser
  6         full outer join c on a.cuser = c.cuser
  7  where a.modified_date between &&date_from and &&date_to
  8     or b.modified_date between &&date_from and &&date_to
  9     or c.modified_date between &&date_from and &&date_to
 10  group by nvl(nvl(a.cuser, b.cuser), c.cuser)
 11  order by 1;
Enter value for date_from: '2018-05-01'
Enter value for date_to: '2018-06-01'

CUSER           CNT_A      CNT_B      CNT_C
---------- ---------- ---------- ----------
jw                  0          0          1
lf                  1          2          0
mc                  1          0          2

SQL>