[SQL]排序的解决方案

现有规则:A,B,C,D

现有顺序:

A1 B1 空 空
A1 B1 空 D1
A1 B1 C1 空

期望顺序:

A1 B1 空 空
A1 B1 C1 空
A1 B1 空 D1

解答:

/*

名称:解决排序问题

作者:cosio

时间:2006-06-13

*/

declare @t table
(
A char(10),
B char(10),
C char(10),
D char(10)
)

insert into @t
select 'A1','B1','','' union all
select 'A1','B1','','D1' union all
select 'A1','B1','C1','' union all
select 'A1','B1','',''

select A,B,C,D from @t where C=''and D=''
union all
select A,B,C,D from @t where C<>''
union all
select A,B,C,D from @t where D<>''

/*

测试数据

*/
drop table times
create table times(id int,times datetime)
--插入数据
insert into times
select 1,'2006-08-20 18:00:01'
union all        
select 2,'2006-08-20 18:00:01'
union all        
select 2,'2006-08-20 18:01:01'
union all        
select 3,'2006-08-20 18:00:01'
union all        
select 3,'2006-08-20 18:01:01'
union all        
select 4,'2006-08-20 18:00:01'
union all        
select 4,'2006-08-20 18:01:01'
union all        
select 1,'2006-08-20 13:00:01'
--解答

select * from times
select distinct * from (select id ,times=(select min(times) from times where times<=a.times and datediff(minute,times,a.times)<=1)
from times a) b

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值