添加SELECT语句输出中不存在的值

时间:2013-01-31 15:48:58

标签: sql plsql oracle10g

由于我缺乏经验,我不知道如何问这个,所以请耐心等待。

我正在使用SELECT语句提取待处理的申请(即没有完成的日期)和count他们的总数(加上其他一些计数)。我需要收集某些项目类型,其中8个,其中只有4个目前有待处理的应用程序。我仍然需要在count列中显示其他项目类型为0。如果我尝试与将提取所有数据的查询联合,无论是否挂起,我将获得具有当前待定应用程序的4个项目类型的重复行(减去计数列)。如果我将join连接到项目类型表,我会得到一个空行。这也不是我想要的。有没有人有任何建议?

联盟的输出:

General Construction       0    0       90    0
General Coverage           0    0       90    0
General Coverage           1    740     90    90
General Renewal            0    0       90    0
Individual Construction    0    0       90    0
Individual Coverage        0    0       180   0
Individual Renewal         0    0       90    0
Individual Renewal         2    1027    90    180
Approval                   0    0       90    0
Approval                   22   565     90    1980
Other - Renewal            0    0       90    0
Other - Renewal            21   1119    90    1890

没有必要的'0'行的输出:

General Coverage           1    740     90    90
Individual Renewal         2    1027    90    180
Other - Renewal            21   1119    90    1890
Approval                   22   566     90    1980

我希望看到的输出是:

    General Construction        0   0    90    0
    General Coverage            1   740  90    90
    General Renewal             0   0    90    0
    Individual Construction     0   0    90    0
    Individual Coverage         0   0    180   0
    Individual Renewal          2   1027 90    180
    Approval                    22  565  90    1980
    Other - Renewal             21  1119 90    1890    

请让我知道我还能提供什么来帮助你。

1 个答案:

答案 0 :(得分:0)

您不提供列的名称或类似名称。

您需要的是具有所有有效值的驱动程序表。然后,您可以使用left outer join获取所需的结果集:

select driver.val, coalesce(q.col2, 0), coalesce(q.col3, 0), coalesce(q.col4, 0)
from (select 'General Construction' as val from dual union all
      select 'General Coverage'  as val from dual union all
      select 'General Renewal'  as val from dual union all
      select 'Individual Construction' as val from dual union all
      select 'Individual Coverage'  as val from dual union all
      select 'Individual Renewal' as val from dual union all
      select 'Approval' as val from dual union all
      select 'Other - Renewal' as val from dual
     ) driver left outer join
     (query without 0 rows) q
     on driver.val = q.col1

如果NULL可以而不是0,则不需要coalesce()