如果不同值的计数为空,如何显示0?

时间:2012-01-19 14:50:06

标签: sql oracle oracle11g oracle-sqldeveloper

我的桌子有 X 不同的供应商,每个供应商都有 Y 目标(包括没有)

vendor varchar2(100),
location varchar2(100),
type varchar2(100),
rating varchar2(20),
control_objective varchar2(1000)

现在我需要显示一个在以下脚本上运行的条形图

select trim(vendor)||' '||trim(location)||' '||trim(type1) AS vendor_location,
       count(distinct control_objective) as "Cont Obj" 
from some_table
where (Rating = 'Needs improvement' or Rating = 'Unacceptable') and 
      "Year" = '2011' and 
      Quarter = 'Q3'
group by trim(vendor)||' '||trim(location)||' '||trim(type1)
order by trim(vendor)||' '||trim(location)||' '||trim(type1);

并生成以下输出

-----vendor_location----Cont Obj
1----Big Blue Car---------5
2----Big Red Car----------4
3----Small Pink Truck-----4
4----Small White Truck----2

问题是,可能有一个第五供应商,比如 Big White Van ,它在目标字段中没有值,因此不会显示。但我希望它显示如下。

-----vendor_location----Cont Obj
1----Big Blue Car---------5
2----Big Red Car----------4
3----Small Pink Truck-----4
4----Small White Truck----2
5----Big White Van--------0

原因是,脚本将值提供给条形图,因此我需要图表上的值0。我已经尝试了几种克服这种方法的方法,这些方法发布在下面

select trim(vendor)||' '||trim(location)||' '||trim(type1) AS vendor_location,
       count(distinct nvl(control_objective,0)) as "Cont Obj" 
from some_table
where (Rating = 'Needs improvement' or Rating = 'Unacceptable') and 
      "Year" = '2011' and 
      Quarter = 'Q3'
group by trim(vendor)||' '||trim(location)||' '||trim(type1)
order by trim(vendor)||' '||trim(location)||' '||trim(type1);

select trim(vendor)||' '||trim(location)||' '||trim(type1) AS vendor_location,
       (count(distinct control_objective)+0) as "Cont Obj" from some_table
where (Rating = 'Needs improvement' or Rating = 'Unacceptable') and 
      "Year" = '2011' and 
      Quarter = 'Q3'
group by trim(vendor)||' '||trim(location)||' '||trim(type1)
order by trim(vendor)||' '||trim(location)||' '||trim(type1);

select trim(vendor)||' '||trim(location)||' '||trim(type1) AS vendor_location,
       case when (count(distinct control_objective)<1) 
            then 0 
            else count(distinct control_objective) 
       end as "Cont Obj" 
from some_table
where (Rating = 'Needs improvement' or Rating = 'Unacceptable') and 
      "Year" = '2011' and 
      Quarter = 'Q3'
group by trim(vendor)||' '||trim(location)||' '||trim(type1)
order by trim(vendor)||' '||trim(location)||' '||trim(type1);

以上所有尝试都给了我与之前相同的输出。

老实说,我想不出更多的方法。我有最后一个选项是将输出写入表格,然后手动输入缺失值,然后显示表格。但这真的不是我想要的,因为脚本应该适用于未来几个季度。所以在未来,其他一些价值可能是空的,写入和阅读表格会破坏“面向未来”的目的,所以基本上它不是一个选项,只是对即将到来的截止日期的快速修复。

我使用 SQL Developer 来测试脚本,数据库是 Oracle 11g

P.S。如果不可能,请告诉我。我甚至愿意接受这个答案!我对SQL没有多少经验。

修改

谢谢大家。我在回家的路上意识到了自己的问题。评级条件不满意,但在我回到家之前无法发布。非常感谢Marcin!

2 个答案:

答案 0 :(得分:5)

你应该看到'Big White Van',如果它在表中,其他字段(评级,年份,季度)满足您的条件。我想你宁愿需要这样的东西:

select trim(vendor) || ' ' || trim(location) || ' ' || trim(type1) AS vendor_location,
       count(distinct case
               when (Rating = 'Needs improvement' or Rating = 'Unacceptable') and "Year" = '2011' and Quarter = 'Q3' then
                control_objective
               else
                null
             end) as "Cont Obj"
  from some_table
 group by trim(vendor) || ' ' || trim(location) || ' ' || trim(type1)
 order by trim(vendor) || ' ' || trim(location) || ' ' || trim(type1);

这意味着:给我所有的vendor_locations并为每个show我计算那些记录的不同control_objective值(评级='需要改进'或评级='不可接受')和“年”='2011'和季度= 'Q3'

答案 1 :(得分:1)

它没有出现的原因不是因为该值为空,而是因为它没有满足其他条件。检查一下。

SELECT a, COUNT(DISTINCT b)
FROM (
    SELECT 'a' a, 'b' b FROM dual UNION ALL
    SELECT 'b', NULL FROM dual UNION ALL
    SELECT 'b', NULL FROM dual
)
GROUP BY a

->

b - 0
a - 1