小编典典

SQL:需要使用group by或任何其他方式对值进行排序

sql

这是我桌子上的风景。首先,我必须按照ITime
asc的顺序查找,但如果ITime中有相同的记录,则记录顺序为``减->失败->撤回’‘。请检查下面的临时表。

if object_id('tempdb.dbo.#temp321','U') is not null
drop table tempdb.dbo.#temp321
create table #temp321(
id int,
uname varchar(50),
ITime datetime,
Result varchar(10)
)
INSERT into #temp321 values('1','a','2012-11-12 13:12:28.103','pass')
INSERT into #temp321 values('2','b','2012-11-12 13:12:28.103','fail')
INSERT into #temp321 values('3','c','2012-11-12 12:58:30.000','pass')
INSERT into #temp321 values('4','d','2012-11-12 13:12:28.103','withdrow')
INSERT into #temp321 values('5','e','2012-11-12 12:58:41.360','withdraw')
INSERT into #temp321 values('6','f','2012-11-12 13:12:28.103','pass')
INSERT into #temp321 values('7','g','2012-11-12 13:12:28.103','fail')
select ID from #temp321 ORDER BY ITime ASC
drop table #temp321


Expected outcomes ID value : 3,5,1,6,2,7,4

我该怎么做?提前致谢。


阅读 232

收藏
2021-03-08

共1个答案

小编典典

ORDER BY ITIME ASC,
         CASE Result WHEN 'pass' THEN 1
                     WHEN 'fail' THEN 2
                     WHEN 'withdraw' THEN 3
                     ELSE 4
                     END,
         ID;
2021-03-08