我有一个表'name','status' - 失败或成功,'count'-1到100.我想以这样的方式订购它,只有'fail'的名字应该一起显示在上面,然后相同的名字与失败和成功在一起,然后名称只有成功。 我们可以用sql查询语言吗? 谢谢。
答案 0 :(得分: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
答案 1 :(得分: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