2014-01-09 51 views
1

的分工的結果,我有這樣一個查詢:訂購兩項罪名

SELECT 
    type, 
    count(case when STATUS = 'N/A' then 1 end) as NOTAPPLICABLE, 
    count(case when STATUS = 'Failed' then 1 end) as FAILED, 
    count(case when STATUS = 'No Run' then 1 end) as NO_RUN, 
    count(case when STATUS = 'Not Completed' then 1 end) as NOT_COMPLETE, 
    count(case when STATUS = 'Blocked' then 1 end) as Blocked, 
    count(case when STATUS = 'Passed' then 1 end) as PASSED, 
    count(case when STATUS <> 'N/A' then 1 end) as TOTAL 

FROM 
    table 

GROUP BY 
    type 

我要訂購結果,以便與傳遞比例最高的類型行是在上面。

不過,我覺得是這樣的:

ORDER BY 
    "PASSED"/"TOTAL" DESC 

但它不工作。

你有什麼想法做到這一點?

感謝,

+2

您正在使用哪些DBMS? –

+0

您正在按字符串排序,刪除引號。 – Mihai

+0

@Mihai - ANSI標準是引號分隔標識符而不是字符串。 –

回答

2

您可以使用表達式ORDER BY

SELECT 
    type, 
    count(case when STATUS = 'N/A' then 1 end) as NOTAPPLICABLE, 
    count(case when STATUS = 'Failed' then 1 end) as FAILED, 
    count(case when STATUS = 'No Run' then 1 end) as NO_RUN, 
    count(case when STATUS = 'Not Completed' then 1 end) as NOT_COMPLETE, 
    count(case when STATUS = 'Blocked' then 1 end) as Blocked, 
    count(case when STATUS = 'Passed' then 1 end) as PASSED, 
    count(case when STATUS <> 'N/A' then 1 end) as TOTAL 

FROM 
    table 

GROUP BY 
    type 
ORDER BY 
    count(case when STATUS = 'Passed' then 1 end)/count(case when STATUS <> 'N/A' then 1 end) desc 

但是這可能會產生division by zero例外,你必須檢查是否計數(情況下,當STATUS <> 'N/A',那麼1周端)不是零。

另一種方案是使用子查詢 - 您可以在子查詢中附上您的初始查詢,然後你可以訂購,限制或SQL

SELECT * 
FROM (
    SELECT 
     type, 
     count(case when STATUS = 'N/A' then 1 end) as NOTAPPLICABLE, 
     count(case when STATUS = 'Failed' then 1 end) as FAILED, 
     count(case when STATUS = 'No Run' then 1 end) as NO_RUN, 
     count(case when STATUS = 'Not Completed' then 1 end) as NOT_COMPLETE, 
     count(case when STATUS = 'Blocked' then 1 end) as Blocked, 
     count(case when STATUS = 'Passed' then 1 end) as PASSED, 
     count(case when STATUS <> 'N/A' then 1 end) as TOTAL 

    FROM 
     table 

    GROUP BY 
     type 
) AS SUB_DATA 
ORDER BY PASSED/TOTAL DESC 

過濾此子查詢作爲簡單的表,如果你是使用PostgreSQL你可以使用WITH構造(我非常喜歡它)。

WITH _records as (
    SELECT 
     type, 
     count(case when STATUS = 'N/A' then 1 end) as NOTAPPLICABLE, 
     count(case when STATUS = 'Failed' then 1 end) as FAILED, 
     count(case when STATUS = 'No Run' then 1 end) as NO_RUN, 
     count(case when STATUS = 'Not Completed' then 1 end) as NOT_COMPLETE, 
     count(case when STATUS = 'Blocked' then 1 end) as Blocked, 
     count(case when STATUS = 'Passed' then 1 end) as PASSED, 
     count(case when STATUS <> 'N/A' then 1 end) as TOTAL 

    FROM 
     table 

    GROUP BY 
     type 
) 
SELECT * 
FROM _records 
ORDER BY PASSED/TOTAL DESC 
2

如果SELECT中定義的列別名,然後在ORDER BY引用,他們必須對自己使用。不在表達式中。

您可以使用派生表。

SELECT * 
FROM 
(
/* Your Query here*/ 
) T 
ORDER BY PASSED/TOTAL DESC 

您可能還需要投PASSED爲數字,以避免整數除法取決於你的DBMS。

1
SELECT *, (PASSED/TOTAL) [percent] FROM 
( SELECT 
     type, 
     count(case when STATUS = 'N/A' then 1 end) as NOTAPPLICABLE, 
     count(case when STATUS = 'Failed' then 1 end) as FAILED, 
     count(case when STATUS = 'No Run' then 1 end) as NO_RUN, 
     count(case when STATUS = 'Not Completed' then 1 end) as NOT_COMPLETE, 
     count(case when STATUS = 'Blocked' then 1 end) as Blocked, 
     count(case when STATUS = 'Passed' then 1 end) as PASSED, 
     count(case when STATUS <> 'N/A' then 1 end) as TOTAL 

    FROM 
     table 

    GROUP BY 
     type) T 
ORDER BY [percent] 
1

你的代碼在sql server中工作,但不是在oracle中,我想。嘗試:

SELECT 
    type, 
    count(case when STATUS = 'N/A' then 1 end) as NOTAPPLICABLE, 
    count(case when STATUS = 'Failed' then 1 end) as FAILED, 
    count(case when STATUS = 'No Run' then 1 end) as NO_RUN, 
    count(case when STATUS = 'Not Completed' then 1 end) as NOT_COMPLETE, 
    count(case when STATUS = 'Blocked' then 1 end) as Blocked, 
    count(case when STATUS = 'Passed' then 1 end) as PASSED, 
    count(case when STATUS <> 'N/A' then 1 end) as TOTAL, 
    count(case when STATUS = 'Passed' then 1 end)/count(case when STATUS <> 'N/A' then 1 end) as sort 
FROM 
    table 

GROUP BY 
    type 
ORDER BY 9 
    sort DESC 
1

有在你的方法兩個問題:

  1. 正如其他人已經指出的那樣,你不能」使用列 計算別名。而不是ORDER BY PASSED/TOTAL DESC,請寫ORDER BY count(case when STATUS = 'Passed' then 1 end)/count(case when STATUS <> 'N/A' then 1 end)
  2. 如果您將PASSED除以TOTAL並且PASSED小於TOTAL,則您總是會得到0結果。就像select 5/10將返回0 而不是0.5 - 因爲這兩個值都是整數,您將得到整數作爲結果。 select 1.0*5/10將返回0.5