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

Sql 如果不同值的计数为空,如何显示0?,sql,oracle,oracle11g,oracle-sqldeveloper,Sql,Oracle,Oracle11g,Oracle Sqldeveloper,我的表格中有X不同的供应商,每个供应商都有Y目标(包括无目标) 现在我需要显示一个运行在以下脚本上的条形图 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

我的表格中有X不同的供应商,每个供应商都有Y目标(包括无目标)

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

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
问题是,可能有一个5th供应商,比如说大白面包车,它在目标字段中没有值,因此不显示。但我希望它显示如下

-----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);

选择修剪(供应商)| | | | | | | | | | | | | |修剪(位置)| | | | |修剪(类型1)作为供应商位置,

case when(count(distinct control_objective)它不出现的原因不是因为该值为null,而是因为它不满足其他条件。请检查

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

它不出现的原因不是因为值为null,而是因为它不满足其他条件。请检查

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

如果“大白车”在表中,其他字段(评级、年份、季度)满足您的条件,您应该看到它。我想您更需要这样的内容:

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);

这意味着:给我所有供应商的位置,并为每个显示给我不同控制目标值的计数,这些记录(评级=‘需要改进’或评级=‘不可接受’)和“年”=‘2011’和季度=‘Q3’

如果在表和其他字段(评级、年、季度)中,你应该看到‘大白车’)满足你的条件。我想你更需要这样的东西:

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);

这意味着:给我所有供应商的位置,并为每个位置显示不同控制目标值的计数,这些记录(评级=‘需要改进’或评级=‘不可接受’)和“年”=‘2011’和季度=‘Q3’

您的表中是否有“大白车”的记录?该行的评级、年度、季度值是多少?所有供应商(包括没有控制目标的供应商)是否在某个表格中有所需年度和季度的条目?您的表格中是否有“大白车”的记录?该行的评级、年度、季度值是多少?所有供应商(包括没有控制目标的供应商)是否都有在一些表格中有关于所需年度和季度的条目吗?是的,它对所有方面都有价值,在我回家的路上,我意识到它不符合评级标准(评级为“可接受”)但我仍然需要展示它的价值。我正在考虑其他东西,将其作为编辑发布!谢谢!是的,它对所有东西都有价值,在我回家的路上,我意识到它不符合评级标准(评级为“可接受”)但我仍然需要展示这个价值。我正在考虑其他东西,将其作为编辑发布!谢谢!谢谢,我意识到了我的问题!!太盲目了!谢谢,我意识到了我的问题!!太盲目了!
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);