Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
当一列在SQL Server中以相同的值出现两次时,如何自定义排序_Sql_Sql Server - Fatal编程技术网

当一列在SQL Server中以相同的值出现两次时,如何自定义排序

当一列在SQL Server中以相同的值出现两次时,如何自定义排序,sql,sql-server,Sql,Sql Server,当前表格: Id name statusId CreateDate -------------------------------------------- 1 "Test" 2 2018-09-21 01:40:14.950 1 "Test" 8 2018-09-21 01:40:01.603 2 "Test" 2 2018-09-21 02:29:55.403 1 "Test" 2 2018-09

当前表格:

Id name    statusId  CreateDate    
--------------------------------------------
1  "Test"  2         2018-09-21 01:40:14.950
1  "Test"  8         2018-09-21 01:40:01.603
2  "Test"  2         2018-09-21 02:29:55.403
1  "Test"  2         2018-09-21 01:36:59.383
3  "Test"  3         2018-09-21 01:40:22.707
4  "Test"  8         2018-09-21 01:10:11.630
1  "Test"  3         2018-09-21 01:40:16.707
1  "Test"  8         2018-09-21 01:40:16.630
2  "Test"  2         2018-09-21 01:20:15.950
2  "Test"  8         2018-09-21 01:30:02.603
1  "Test"  2         2018-09-21 02:19:55.403
3  "Test"  2         2018-09-21 01:56:59.383
想要找到:

1  "Test"  2         2018-09-21 02:19:55.403
1  "Test"  3         2018-09-21 01:40:16.707
1  "Test"  8         2018-09-21 01:40:16.630
1  "Test"  2         2018-09-21 01:40:14.950
1  "Test"  8         2018-09-21 01:40:01.603
1  "Test"  2         2018-09-21 01:36:59.383
如何将statusId排序为固定顺序(2-3-8-2-8-2)和CreateDate DESC

我尝试使用此SQL语句,但它不会返回预期结果:

select id 
from table
where statusId = 8
   or statusId = 3
   or statusId = 2
order by 
    createDate desc, 
    case 
       when statusId = 3 then 1
       when statusId = 8 then 2
       when statusId = 2 then 3
    end asc

我想你可以用
行号()来完成你想要的:


我想你可能只是把你的分类倒过来了

按照现在的方式,您将按照
CreateDate
进行排序。
排序依据的第二个子句用于断开任何关系。它的意思是“如果有多行具有相同的
CreateDate
值,则按
case
表达式对它们进行排序。”因为所有CreateDate值都是唯一的,所以忽略第二个sort子句。没有关系可以打破

create table MyTable ( ID         int, 
                       Name       varchar(10),
                       StatusID   int,
                       CreateDate datetime )

insert into MyTable ( ID, Name, StatusID, CreateDate ) values
( 1, 'Ella',    2, '2018-09-21T01:40:14.950' ),
( 1, 'Spencer', 8, '2018-09-21T01:40:01.603' ),
( 2, 'Daisy',   2, '2018-09-21T02:29:55.403' ),
( 1, 'Chloe',   2, '2018-09-21T01:36:59.383' ),
( 3, 'Ivy',     3, '2018-09-21T01:40:22.707' ),
( 4, 'Alyssia', 8, '2018-09-21T01:10:11.630' ),
( 1, 'Hallie',  3, '2018-09-21T01:40:16.707' ),
( 1, 'Carole',  8, '2018-09-21T01:40:16.630' ),
( 2, 'Blake',   2, '2018-09-21T01:20:15.950' ),
( 2, 'Mary',    8, '2018-09-21T01:30:02.603' ),
( 1, 'Vivian',  2, '2018-09-21T02:19:55.403' ),
( 3, 'Theresa', 2, '2018-09-21T01:56:59.383' )

select ID, Name, StatusID , CreateDate
from MyTable
where StatusID in ( 2, 3, 8 )
order by

  case
    when StatusID = 3 then 1
    when StatusID = 8 then 2
    when StatusID = 2 then 3
  end asc,

  CreateDate desc

我找到的解决方案如下,它符合我的预期

select * from MyTable t1
join MyTable t2
    on t1.ID = t2.ID
join MyTable t3
    on t1.ID = t3.ID
join MyTable t4
    on t1.ID = t4.ID
join MyTable t5
    on t1.ID = t5.ID
where t1.statusID = 3
    and t2.statusID = 8
    and t3.statusID = 2
    and t4.statusID = 8
    and t5.statusID = 2
    and t1.CreateDate > t2.CreateDate
    and t2.CreateDate > t3.CreateDate
    and t3.CreateDate > t4.CreateDate
    and t4.CreateDate > t5.CreateDate
    and DATEDIFF(minute, t2.CreateDate, t1.CreateDate) < 1 
    and DATEDIFF(minute, t3.CreateDate, t2.CreateDate) < 1
    and DATEDIFF(minute, t4.CreateDate, t3.CreateDate) < 1
    and DATEDIFF(minute, t5.CreateDate, t4.CreateDate) < 10
order by t1.ID, t1.CreateDate desc
从MyTable t1中选择*
联接我的表t2
在t1.ID=t2.ID上
加入MyTableT3
在t1.ID=t3.ID上
加入我的表t4
在t1.ID=t4.ID时
加入MyTable t5
在t1.ID=t5.ID上
其中t1.statusID=3
t2.statusID=8
和t3.statusID=2
t4.statusID=8
和t5.statusID=2
和t1.CreateDate>t2.CreateDate
和t2.CreateDate>t3.CreateDate
t3.CreateDate>t4.CreateDate
和t4.CreateDate>t5.CreateDate
和DATEDIFF(分钟,t2.CreateDate,t1.CreateDate)<1
和日期差(分钟,t3.CreateDate,t2.CreateDate)<1
和日期差(分钟,t4.CreateDate,t3.CreateDate)<1
和日期差(分钟,t5.CreateDate,t4.CreateDate)<10
按t1.ID、t1.CreateDate desc排序

因此,您希望statusID=3的所有记录先出现,然后是statusID=8的所有记录,最后是statusID=2的记录。然后,在每个组中,按createDate降序排序。我认为排序参数可能有误。首先放案例,然后创建日期。您觉得这有帮助吗?很抱歉延迟响应,这不是我的预期,只是想找到状态ID为的固定订单(2-3-8-2-8-2),示例表只有一个ID与此案例匹配,因此结果必须是
1“Vivian”2 2018-09-21 02:19:55.403 1“Hallie”3 2018-09-21 01:40:16.707 1“卡罗尔”8 2018-09-21 01:40:16.630 1“埃拉”2 2018-09-21 01:40:14.950 1“斯宾塞”8 2018-09-21 01:40:01.603 1“克洛伊”2 2018-09-21 01:36:59.383
select * from MyTable t1
join MyTable t2
    on t1.ID = t2.ID
join MyTable t3
    on t1.ID = t3.ID
join MyTable t4
    on t1.ID = t4.ID
join MyTable t5
    on t1.ID = t5.ID
where t1.statusID = 3
    and t2.statusID = 8
    and t3.statusID = 2
    and t4.statusID = 8
    and t5.statusID = 2
    and t1.CreateDate > t2.CreateDate
    and t2.CreateDate > t3.CreateDate
    and t3.CreateDate > t4.CreateDate
    and t4.CreateDate > t5.CreateDate
    and DATEDIFF(minute, t2.CreateDate, t1.CreateDate) < 1 
    and DATEDIFF(minute, t3.CreateDate, t2.CreateDate) < 1
    and DATEDIFF(minute, t4.CreateDate, t3.CreateDate) < 1
    and DATEDIFF(minute, t5.CreateDate, t4.CreateDate) < 10
order by t1.ID, t1.CreateDate desc