两次排序,这个语句如何写?

时间:2021-09-01 20:01:55
比如有个表格 

name   time         type
        
aa     1990-2-1      1
bb     1990-2-2      1
cc     1990-2-3      2
dd     1990-1-8      1
ee     1990-2-5      2
ff     1990-2-8      2
gg     1990-3-21     1

我想让他先按照“分类”排序,都是如果是1的都在一组, (order by type)即可
然后再按照时间排序:每组中按照时间进行排序

结果为:
内容    时间        分类
dd     1990-1-8      1
aa     1990-2-1      1
bb     1990-2-2      1
gg     1990-3-21     1
cc     1990-2-3      2
ee     1990-2-5      2
ff     1990-2-8      2


9 个解决方案

#1


select *
from 表
order by type,时间

#2


谢谢楼上的,按照你的方法已经解决了问题。

再提个问题。
我想选择“分类”中的前两组,就是如果表还有其他数据,我把所有排在前面的两组type提取出来,应该怎么办?

#3


你要的是这样?
内容    时间        分类
dd     1990-1-8      1
aa     1990-2-1      1
cc     1990-2-3      2
ee     1990-2-5      2

#4


create table #t (nr varchar(10),rq varchar(10),type varchar(1))
insert into #t values('dd',     '1990-1-8',      '1')
insert into #t values('aa',     '1990-2-1',      '1')
insert into #t values('bb',     '1990-2-2',      '1')
insert into #t values('gg',     '1990-3-21',     '1')
insert into #t values('cc',     '1990-2-3',      '2')
insert into #t values('ee',     '1990-2-5',      '2')
insert into #t values('ff',     '1990-2-8',      '2')

select *
from #t t
where (select count(1) from #t where type=t.type and rq<=t.rq)<=2

结果:
dd     1990-1-8      1
aa     1990-2-1      1
cc     1990-2-3      2
ee     1990-2-5      2

#5


请lw1a2解释一下用count(1)是什么意思?

#6


count(1)就是任意行的数量。
比如,select 1 from Table1,那么行数就是count(1)

#7


很强,学习中............................................

#8


lw1a2(一刀 知我者谓我心忧,不知我者谓我何求) 大哥,能解释一下你那语句吗?
select *
from #t t
where (select count(1) from #t where type=t.type and rq<=t.rq)<=2
实在太强了,有点难理解,谢谢.....

#9


count(1)就是任意行的数量。

select count(1) from #t where type=t.type and rq<=t.rq
那整句怎么解释?

#1


select *
from 表
order by type,时间

#2


谢谢楼上的,按照你的方法已经解决了问题。

再提个问题。
我想选择“分类”中的前两组,就是如果表还有其他数据,我把所有排在前面的两组type提取出来,应该怎么办?

#3


你要的是这样?
内容    时间        分类
dd     1990-1-8      1
aa     1990-2-1      1
cc     1990-2-3      2
ee     1990-2-5      2

#4


create table #t (nr varchar(10),rq varchar(10),type varchar(1))
insert into #t values('dd',     '1990-1-8',      '1')
insert into #t values('aa',     '1990-2-1',      '1')
insert into #t values('bb',     '1990-2-2',      '1')
insert into #t values('gg',     '1990-3-21',     '1')
insert into #t values('cc',     '1990-2-3',      '2')
insert into #t values('ee',     '1990-2-5',      '2')
insert into #t values('ff',     '1990-2-8',      '2')

select *
from #t t
where (select count(1) from #t where type=t.type and rq<=t.rq)<=2

结果:
dd     1990-1-8      1
aa     1990-2-1      1
cc     1990-2-3      2
ee     1990-2-5      2

#5


请lw1a2解释一下用count(1)是什么意思?

#6


count(1)就是任意行的数量。
比如,select 1 from Table1,那么行数就是count(1)

#7


很强,学习中............................................

#8


lw1a2(一刀 知我者谓我心忧,不知我者谓我何求) 大哥,能解释一下你那语句吗?
select *
from #t t
where (select count(1) from #t where type=t.type and rq<=t.rq)<=2
实在太强了,有点难理解,谢谢.....

#9


count(1)就是任意行的数量。

select count(1) from #t where type=t.type and rq<=t.rq
那整句怎么解释?