0

我有一个带有“名称”、“状态”-失败或成功、“计数”-从 1 到 100 的表。我想以这样的方式对其进行排序,即只有“失败”的名称应该一起显示在顶部,然后是相同的名称失败和成功在一起,然后只有成功的名字在一起。我们可以用 sql 查询语言来做吗?谢谢你。

4

2 回答 2

3

好的,这应该适用于大多数 RDBMS:

SELECT A.*
FROM YourTable A
INNER JOIN (SELECT  name, 
                    COUNT(DISTINCT status) StatusCount,
                    MIN(status) MinStatus
            FROM YourTable
            GROUP BY name) B
    ON A.name = B.name
ORDER BY CASE WHEN StatusCount = 1 AND MinStatus = 'fail' THEN 1
         WHEN StatusCount = 2 THEN 2 
         ELSE 3 END, A.name, A.status
于 2013-02-28T13:25:10.800 回答
0

如果是 MySQL

select *, 0 as o
from t
union all
select *, 1 as o
from t
order by
    o = 1 and status != 'fail', o != 1, status = fail
于 2013-02-28T13:25:16.327 回答