2014-01-09 113 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