Oracle视图创建

时间:2014-02-18 14:34:05

标签: sql oracle

我的案例如下图所示

enter image description here

在输入表中,name列下的每个值应该有五个步骤A,B,C,D,E ....但是名称“VINOD”有3个值A,B,C,它们的对应日期是28.12 .2013,11.10.2013和NULL。因此,在视图中,我应该为每个名称分配五个diffrenet列,如下所示。由于“Vinod”没有D和E,因此相应的日期应该是NA(但由于表中的C值为NULL,因此只应将其视为NULL)。

希望我解释了我的疑问。请帮助我。

提前致谢。

2 个答案:

答案 0 :(得分:0)

你可以尝试:

SELECT name, 
       (CASE WHEN A_DATE IS NULL THEN 'NA' ELSE A_DATE END) as A_DATE, 
       (CASE WHEN B_DATE IS NULL THEN 'NA' ELSE B_DATE END) as B_DATE, 
       (CASE WHEN C_DATE IS NULL THEN 'NA' ELSE C_DATE END) as C_DATE, 
       (CASE WHEN D_DATE IS NULL THEN 'NA' ELSE D_DATE END) as D_DATE, 
       (CASE WHEN E_DATE IS NULL THEN 'NA' ELSE E_DATE END) as E_DATE
  FROM (
        SELECT name,
               (SELECT date
                  FROM inputtable as i2
                 WHERE i2.name=i1.name
                   AND i2.step = 'A') as A_DATE,
               (SELECT date
                  FROM inputtable as i3
                 WHERE i3.name=i1.name
                   AND i3.step = 'B') as B_DATE,
               (SELECT date
                  FROM inputtable as i4
                 WHERE i4.name=i1.name
                   AND i4.step = 'C') as C_DATE,
               (SELECT date
                  FROM inputtable as i5
                 WHERE i5.name=i1.name
                   AND i5.step = 'D') as D_DATE,
               (SELECT date
                  FROM inputtable as i6
                 WHERE i6.name=i1.name
                   AND i6.step = 'E') as E_DATE
         FROM inputtable as i1
        GROUP BY name
       ) s1

答案 1 :(得分:0)

您不应该混合这样的数据类型。 使用以下测试用例:

-- drop table input_table;

create table input_table(
   name      varchar2(20) not null
  ,step      varchar2(10) not null
  ,step_date varchar2(10)
); 

insert into input_table(name, step, step_date) values('VINOD', 'A', '28.12.013');
insert into input_table(name, step, step_date) values('VINOD', 'B', '11.10.2013');
insert into input_table(name, step, step_date) values('VINOD', 'C', null);

commit;

以下(可怕的)查询应该按照您的要求执行:

select name 
    -- If a row exists (has flag) pick whatever value was there, otherwise set NA
      ,case when max(a_flag) = 'A' then max(a_date) else 'NA' end as a_date  
      ,case when max(b_flag) = 'B' then max(b_date) else 'NA' end as b_date
      ,case when max(c_flag) = 'C' then max(c_date) else 'NA' end as c_date
      ,case when max(d_flag) = 'D' then max(d_date) else 'NA' end as d_date
      ,case when max(e_flag) = 'E' then max(e_date) else 'NA' end as e_date
  from (select name
            -- Pivot the dates to columns
              ,case when step = 'A' then step_date end as a_date
              ,case when step = 'B' then step_date end as b_date
              ,case when step = 'C' then step_date end as c_date
              ,case when step = 'D' then step_date end as d_date
              ,case when step = 'E' then step_date end as e_date
            -- Create flags because we have two kinds of NULL...
              ,case when step = 'A' then 'A' end as a_flag
              ,case when step = 'B' then 'B' end as b_flag
              ,case when step = 'C' then 'C' end as c_flag
              ,case when step = 'D' then 'D' end as d_flag
              ,case when step = 'E' then 'E' end as e_flag
          from input_table
       )
 group
    by name;