0

我正在从两个不同的表中编写查询。

表 A 和表 B

这是查询。

select 
        A.OUT_NUM,
        A.TIMESTAMP,
        A.LAST_name,
        A.event_type, 
        A.comments,
        B.name
   from TABLEA A
   left outer join TABLEB B ON A.feed_id = B.id
   where A.OUT_NUM = '12345'
   and A.event_type In ('cause','status')

B.NAME不为空,event_type = xyz否则为空

我只想看什么时候event_type in ('CAUSE','STATUS'),还想看名称字段但不为空。

第二张桌子是我想要实现的。 在此处输入图像描述

谢谢

4

2 回答 2

1

使用 NVL() 和 LAG() 函数。

使用我的样本数据的一般示例。此查询用数据填充空白行 - 请参阅 first_exam 和 last_exam 列:

SELECT id, name, proc_date, proc_type, first_exam_date
     , NVL(prev_exam_date, LAG(prev_exam_date) OVER (ORDER BY name, proc_date)) last_exam_date
  FROM
   (
   SELECT id, name, proc_date, proc_type, first_exam_date
     , NVL(first_exam_date, LAG(first_exam_date) OVER (ORDER BY name, proc_date) ) prev_exam_date
    FROM
  (
  SELECT id
       , name
       , proc_date
       , proc_type
      , (SELECT MIN(proc_date) OVER (PARTITION BY name, proc_date)
           FROM stack_test WHERE proc_type LIKE 'Exam%' AND a.id = id 
       ) first_exam_date
   FROM stack_test a
  ));
  ID    NAME    PROC_DATE    PROC_TYPE    FIRST_EXAM_DATE    LAST_EXAM_DATE
  --------------------------------------------------------------------------
  1    George    1/1/2013    ExamA             1/1/2013      1/1/2013
  2    George    1/3/2013    TreatmentA                      1/1/2013
  3    George    1/5/2013    TreatmentB                      1/1/2013
  4    George    2/1/2013    ExamB             2/1/2013      2/1/2013
  5    George    2/5/2013    TreatmentA                      2/1/2013
于 2013-02-19T19:42:53.483 回答
1

对评论中的数据做出一些假设,特别是关于如何匹配和选择替代name值;并使用一些我认为与您匹配的虚拟数据:

create table tablea(out_num number,
    equip_name varchar2(5),
    event_type varchar2(10),
    comments varchar2(10),
    timestamp date, feed_id number);

create table tableb(id number, name varchar2(10));

alter session set nls_date_format = 'MM/DD/YYYY HH24:MI';

insert into tablea values (12345, null, 'abcd', null, to_date('02/11/2013 11:12'), 1);
insert into tablea values (12345, null, 'abcd', null, to_date('02/11/2013 11:11'), 1);
insert into tablea values (12345, null, 'abcd', null, to_date('02/11/2013 11:06'), 1);
insert into tablea values (12345, null, 'abcd', null, to_date('02/11/2013 11:06'), 1);
insert into tablea values (12345, null, 'SUB', null, to_date('02/11/2013 11:11'), 2);
insert into tablea values (12345, null, 'SUB', null, to_date('02/11/2013 11:12'), 2);
insert into tablea values (12345, null, 'XYZ', null, to_date('02/11/2013 11:13'), 3);
insert into tablea values (12345, null, 'XYZ', null, to_date('02/11/2013 11:13'), 3);
insert into tablea values (12345, null, 'XYZ', null, to_date('02/11/2013 11:13'), 3);
insert into tablea values (12345, null, 'XYZ', null, to_date('02/11/2013 11:13'), 3);
insert into tablea values (12345, null, 'XYZ', null, to_date('02/11/2013 11:13'), 3);
insert into tablea values (12345, null, 'XYZ', null, to_date('02/11/2013 11:03'), 3);
insert into tablea values (12345, null, 'CAUSE', 'APPLE', to_date('02/11/2013 11:13'), 4);
insert into tablea values (12345, null, 'CAUSE', 'APPLE', to_date('02/11/2013 11:13'), 4);
insert into tablea values (12345, null, 'CAUSE', 'APPLE', to_date('02/11/2013 11:13'), 4);
insert into tablea values (12345, null, 'STATUS', 'BOOKS', to_date('02/11/2013 11:13'), 5);
insert into tablea values (12345, null, 'STATUS', 'BOOKS', to_date('02/11/2013 11:13'), 5);
insert into tablea values (12345, null, 'STATUS', 'BOOKS', to_date('02/11/2013 11:03'), 5);

insert into tableb values(3, 'LION');

这会得到你的结果:

select * from (
    select a.out_num,
        a.timestamp,
        a.equip_name,
        a.event_type,
        a.comments,
        coalesce(b.name,
            first_value(b.name)
                over (partition by a.out_num
                    order by b.name nulls last)) as name
    from tablea a
    left outer join tableb b on a.feed_id = b.id
    where a.out_num = '12345'
    and a.event_type in ('CAUSE', 'STATUS', 'XYZ')
)
where event_type in ('CAUSE', 'STATUS');

   OUT_NUM TIMESTAMP          EQUIP_NAME EVENT_TYPE COMMENTS   NAME     
---------- ------------------ ---------- ---------- ---------- ----------
     12345 02/11/2013 11:03              STATUS     BOOKS      LION       
     12345 02/11/2013 11:13              STATUS     BOOKS      LION       
     12345 02/11/2013 11:13              STATUS     BOOKS      LION       
     12345 02/11/2013 11:13              CAUSE      APPLE      LION       
     12345 02/11/2013 11:13              CAUSE      APPLE      LION       
     12345 02/11/2013 11:13              CAUSE      APPLE      LION       

内部查询包括XYZ并使用分析first_value()函数来选择name如果直接匹配的值是null-coalesce如果真的永远不会有直接匹配,则可能没有必要。(如果假设错误,您可能还需要调整partition byor子句)。order by外部查询只是删除XYZ记录,因为您不想要这些记录。


如果您想name从任何匹配的记录中获取值,那么只需删除内部查询中的过滤器。

但是现在您可能更有可能拥有多个非空记录;如果它存在,这会给你一个匹配a.feed_id的,或者如果它不存在,它会给你一个匹配的“第一个”(按字母顺序,ish)out_num。您可以改为订购,或;b.id中的任何其他列 tableb订购任何东西tablea都需要不同的解决方案。如果无论如何你只会有一个可能的匹配,那么这并不重要,你可以省略order by,尽管最好还是拥有它。

如果我为不同的添加更多数据out_num

insert into tablea values (12346, null, 'abcd', null, to_date('02/11/2013 11:11'), 1);
insert into tablea values (12346, null, 'SUB', null, to_date('02/11/2013 11:12'), 2);
insert into tablea values (12346, null, 'XYZ', null, to_date('02/11/2013 11:13'), 6);
insert into tablea values (12346, null, 'CAUSE', 'APPLE', to_date('02/11/2013 11:14'), 4);
insert into tablea values (12346, null, 'STATUS', 'BOOKS', to_date('02/11/2013 11:15'), 5);

insert into tableb values(1, 'TIGER');

...然后这个 - 只是过滤器被丢弃了,我coalesce这次省略了 - 给出了相同的答案12345,而这个12346

select * from (
    select a.out_num,
        a.timestamp,
        a.equip_name,
        a.event_type,
        a.comments,
        first_value(b.name)
            over (partition by a.out_num
                order by b.name nulls last) as name
    from tablea a
    left outer join tableb b on a.feed_id = b.id
)
where out_num = '12346'
and event_type in ('CAUSE', 'STATUS');

   OUT_NUM TIMESTAMP          EQUIP_NAME EVENT_TYPE COMMENTS   NAME     
---------- ------------------ ---------- ---------- ---------- ----------
     12346 02/11/2013 11:14              CAUSE      APPLE      TIGER      
     12346 02/11/2013 11:15              STATUS     BOOKS      TIGER      

...TIGER链接到哪里abcd,不是XYZ

于 2013-02-19T20:22:58.050 回答