Jos*_*729 1 sql oracle oracle11g oracle-sqldeveloper
我的表有X个不同的供应商,每个供应商都有Y个目标(包括没有)
vendor varchar2(100),
location varchar2(100),
type varchar2(100),
rating varchar2(20),
control_objective varchar2(1000)
Run Code Online (Sandbox Code Playgroud)
现在我需要显示一个在以下脚本上运行的条形图
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);
Run Code Online (Sandbox Code Playgroud)
并产生以下输出
-----vendor_location----Cont Obj
1----Big Blue Car---------5
2----Big Red Car----------4
3----Small Pink Truck-----4
4----Small White Truck----2
Run Code Online (Sandbox Code Playgroud)
问题是,可能有第五个供应商,比如说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
Run Code Online (Sandbox Code Playgroud)
原因是,脚本将值提供给条形图,因此我需要图表上的值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);
Run Code Online (Sandbox Code Playgroud)
和
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);
Run Code Online (Sandbox Code Playgroud)
和
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);
Run Code Online (Sandbox Code Playgroud)
所有上述尝试都给了我与之前相同的输出.
老实说,我想不出更多的方法.我的最后一个选项是将输出写入表,并手动输入缺少的值,然后显示表.但这真的不是我想要的,因为脚本应该适用于未来几个季度.所以在将来,其他一些价值可能是空的,写入和读入表格会破坏"面向未来"的目的,所以基本上它不是一个选项,只是对即将到来的截止日期的快速修复.
我使用SQL Developer来测试脚本,数据库是Oracle 11g
PS如果不可能,请告诉我.我甚至愿意接受这个答案!我对SQL没有多少经验.
编辑
谢谢你们.我在回家的路上意识到了自己的问题.评级条件不满意,但在我回到家之前无法发布.非常感谢Marcin!
您应该看到"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);
Run Code Online (Sandbox Code Playgroud)
这意味着:给我所有vendor_locations,并为每个show me显示那些记录的不同control_objective值的计数(评级='需要改进'或评级='不可接受')和"年"='2011'和季度='Q3'
| 归档时间: |
|
| 查看次数: |
2117 次 |
| 最近记录: |