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

发布于 2024-12-28 00:38:22 字数 3022 浏览 0 评论 0原文

我的表有 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

PS如果不可能,请告诉我。我什至愿意接受这个答案!我对 SQL 没有太多经验。

编辑

谢谢大家。在回家的路上我意识到了自己的问题。评分条件不满足,但回国后才发布。非常感谢马辛!

My table has X distinct vendors,each of those vendors has Y objectives (including none)

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

now I need to display a bar graph that runs on the following script

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

and produces the following output

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

The problem is, there might be a 5th vendor, say Big White Van which has no values in the objective field hence it is not displayed. But I want it to be displayed as follows.

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

Reason being, the script feeds values into a bar graph, so I need the value 0 on the graph. I've tried several ways of overcoming this which is posted below

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

and

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

and

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

All the above attempts gave me the same output as I got earlier.

Honestly, I can't think of any more ways. The last option I have is to write the output into a table, and manually enter the missing values and then display the table. But this is really not what I want because the script should work for future quarters and years. So in the future some other value may be null and writing and reading into a table defeats the purpose of being "future-proof" so basically its not an option, just a quick-fix for the coming deadline.

I am using SQL Developer to test the scripts and the database is Oracle 11g

P.S. If it is not possible, do let me know. I'm even open to that being an answer!! I don't have much experience with SQL.

EDIT

Thank you all. I realised my problem on the way back home. The rating condition was not being satisfied, but couldn't post it until I got back home. Big thanks to Marcin!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

诠释孤独 2025-01-04 00:38:22

如果表中存在“Big White Van”,并且其他字段(“评级”、“年份”、“季度”)满足您的条件,您应该会看到“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);

这意味着:给我所有的供应商位置,并为每个供应商显示来自那些记录的不同control_objective值的计数,其中(评级='需要改进'或评级='不可接受')和“年份”= “2011 年”和季度 =“第三季度”

You should see 'Big White Van' if it is in the table and the other fields (Rating , Year, Quarter) fulfill your condition. I guess you rather need something like this:

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

which means: give me all vendor_locations and for each show me count of distinct control_objective values from those records for which (Rating = 'Needs improvement' or Rating = 'Unacceptable') and "Year" = '2011' and Quarter = 'Q3'

爱情眠于流年 2025-01-04 00:38:22

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

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

The reason it's not appearing is not because the value is null, it's because it's not fulfilling some other condition. Check that.

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文