2012-01-19 67 views
1

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

+1

您的表格是否有「Big white van」的記錄?該行的評分,年份,季度值是多少? – shahkalpesh

+1

所有供應商(包括那些沒有控制目標的供應商)是否已經在some_table上輸入期望的年份和季度? –

回答

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併爲每個告訴我從它(等級=「需要改進」或評級的記錄數不同control_objective值=「不可接受')和「Year」='2011'和Quarter ='Q3'

+0

是的,它確實具有一切價值和回家的路上我意識到它不符合評級標準(評級是'可接受的')但我仍然需要顯示這個價值。我在想別的東西,將它作爲編輯發佈!謝謝 ! – Joshua1729

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

謝謝,我意識到我的問題!真是太盲目了! – Joshua1729