我的表具有X不同廠商,每個這些供應商的具有Ý目標(包括無)如何顯示0如果不同值的計數爲空?
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
問題是,可能有一個第5個供應商,說大白色範它沒有在目標領域的價值,因此它不顯示。但我希望它顯示如下。
-----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開發測試腳本和數據庫的Oracle 11g
附:如果不可能,請告訴我。我甚至可以接受這個答案!我沒有太多的SQL經驗。
編輯
謝謝大家。我在回家的路上意識到自己的問題。評分條件未得到滿足,但無法發佈,直到我回到家中。非常感謝Marcin!
您的表格是否有「Big white van」的記錄?該行的評分,年份,季度值是多少? – shahkalpesh
所有供應商(包括那些沒有控制目標的供應商)是否已經在some_table上輸入期望的年份和季度? –