求sql排序

surferc 2004-12-22 03:29:07
结构
TBA
id numA uptime
1 0 2004-12-11
2 1 2004-12-11
3 2 2004-12-11
4 0 2004-12-12
5 2 2004-12-13
6 2 2004-12-13
7 0 2004-12-14
8 0 2004-12-15
9 1 2004-12-16
10 1 2004-12-17
11 0 2004-12-17

结果
id numc uptime
5 2 2004-12-13
6 2 2004-12-13
10 1 2004-12-17
9 1 2004-12-16 ->这里两条numc=2 numc=1 为一组分别按uptime排序 直到没有
3 2 2004-12-11
2 1 2004-12-11
11 0 2004-12-17 ->没numc=2 numc=1没有时 numc=0按uptime排序
8 0 2004-12-15
7 0 2004-12-14
4 0 2004-12-12
1 0 2004-12-11

就是两条numc=2两条numc=1为一组显示没有再显示numc=0按uptime排序
谢谢各位了。这两天分花的真多啊,不过也学到不少 :)
...全文
226 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
surferc 2004-12-23
  • 打赏
  • 举报
回复
再次感谢邹老师的热心帮忙。
同时感谢gpczlj(c#笨笨) 631799(杭州工人) quanyi(长生天)
zjcxc 元老 2004-12-23
  • 打赏
  • 举报
回复
改的那个没错,是那样的
quanyi 2004-12-22
  • 打赏
  • 举报
回复
學習
surferc 2004-12-22
  • 打赏
  • 举报
回复
其实就是/2、/3得到排序的值可是同一组中会出现不同的值这样没关系吗?
surferc 2004-12-22
  • 打赏
  • 举报
回复
想明白了!拜邹老大(五体投地)
我改了一下,这样就是grade=2的2条,grade=3的3条了对吧?我测试了一下没问题。但总感觉哪不对又说不上来。((select count(*) from TBA where numA=a.numA and(uptime>a.uptime or uptime=a.uptime and id>=a.id))-1)/3 这样同一组的值最后有几条不一致但不影响排序


select *
from TBA a
order by case when numA in(1,2) then 1 else 2 end
,case when numA=2 then ((select count(*) from TBA where numA=a.numA and(uptime>a.uptime or uptime=a.uptime and id>=a.id))-1)/2
,when numA=1 then ((select count(*) from TBA where numA=a.numA and (uptime>a.uptime or uptime=a.uptime and id>=a.id))-1)/3
else 1 end
,numA desc,uptime desc,id
631799 2004-12-22
  • 打赏
  • 举报
回复
up.
zjcxc 元老 2004-12-22
  • 打赏
  • 举报
回复
排序的,你把这个x放在select 列表中,指定一个列名,看看结果来理解吧
surferc 2004-12-22
  • 打赏
  • 举报
回复
order by case when numA in(1,2) then 1 else 2 end -->这个numA含有1,2 为1
,case when numA in(1,2) then X else 1 end --这个X代表select... 为X ?
,numA desc,uptime desc,id

这个X怎么理解呢?
zjcxc 元老 2004-12-22
  • 打赏
  • 举报
回复
是啊
surferc 2004-12-22
  • 打赏
  • 举报
回复
case when numA in(1,2) then ((select count(*) from TBA where numA=a.numA and(uptime>a.uptime or uptime=a.uptime and id>=a.id))-1)/2 else 1 end

这句里的select....-1/2 代表的是什么呢?应该得到的是一个数值对吧?
surferc 2004-12-22
  • 打赏
  • 举报
回复
对对就是这个,排序我是整半天没出来思路有问题。
邹老大明天我再结贴,今天我先消化消化有问题再问。
zjcxc 元老 2004-12-22
  • 打赏
  • 举报
回复
这次的每页大小可以任意指定,不需要内部修改东西
zjcxc 元老 2004-12-22
  • 打赏
  • 举报
回复
--最后套上上次的那个分页处理过程就行了

--示例数据
create table TBA(id int,numA int,uptime datetime)
insert TBA select 1 ,0,'2004-12-11'
union all select 2 ,1,'2004-12-11'
union all select 3 ,2,'2004-12-11'
union all select 4 ,0,'2004-12-12'
union all select 5 ,2,'2004-12-13'
union all select 6 ,2,'2004-12-13'
union all select 7 ,0,'2004-12-14'
union all select 8 ,0,'2004-12-15'
union all select 9 ,1,'2004-12-16'
union all select 10,1,'2004-12-17'
union all select 11,0,'2004-12-17'
go

--分页处理的存储过程
create proc p_split
@currentpage int=1, --要显示的当前页
@pagesize int=4 --每页的大小(如果调整了这个,则存储过程中,排序的处理也要做相应的修改,即:case grade when 'c' then 2 when 'b' then 1 when 'a' then 2 end 部分,这个控制每类/每页多少条记录
as
set nocount on
set @currentpage=@currentpage*@pagesize
set rowcount @currentpage
select * into #t from TBA a
order by case when numA in(1,2) then 1 else 2 end
,case when numA in(1,2) then ((select count(*) from TBA where numA=a.numA and(uptime>a.uptime or uptime=a.uptime and id>=a.id))-1)/2 else 1 end
,numA desc,uptime desc,id
if @currentpage>@pagesize
begin
set @currentpage=@currentpage-@pagesize
set rowcount @currentpage
delete from #t
end
select * from #t
go

--调用
exec p_split 1
exec p_split 2
exec p_split 3
go

--删除测试
drop table TBA
drop proc p_split

/*--测试结果

id numA uptime
----------- ----------- -------------------------
5 2 2004-12-13 00:00:00.000
6 2 2004-12-13 00:00:00.000
10 1 2004-12-17 00:00:00.000
9 1 2004-12-16 00:00:00.000

id numA uptime
----------- ----------- -------------------------
3 2 2004-12-11 00:00:00.000
2 1 2004-12-11 00:00:00.000
11 0 2004-12-17 00:00:00.000
8 0 2004-12-15 00:00:00.000

id numA uptime
----------- ----------- -------------------------
7 0 2004-12-14 00:00:00.000
4 0 2004-12-12 00:00:00.000
1 0 2004-12-11 00:00:00.000

--*/
zjcxc 元老 2004-12-22
  • 打赏
  • 举报
回复
--最关键的问题是排序,你用这个排序就行了

select *
from TBA a
order by case when numA in(1,2) then 1 else 2 end
,case when numA in(1,2) then ((select count(*) from TBA where numA=a.numA and(uptime>a.uptime or uptime=a.uptime and id>=a.id))-1)/2 else 1 end
,numA desc,uptime desc,id
surferc 2004-12-22
  • 打赏
  • 举报
回复
我觉得可以先把2、1的整理好先插入临时表然后再把0的追加到临时表里不知道这样行不行(试了半天没试出来笨-_-b)。假如分页的话数据太多插入临时表在访问量大的时候我觉得会出问题所以上来问问。这个需求天天在不断变化....正好借机会增长实力。
surferc 2004-12-22
  • 打赏
  • 举报
回复
gpczlj(c#笨笨) 这个是不对滴...
surferc 2004-12-22
  • 打赏
  • 举报
回复
是啊:)一次显示、分页的都来一个我学习学习好吧?邹老师~
gpczlj 2004-12-22
  • 打赏
  • 举报
回复
select * from TBA order by numc desc,uptime desc
zjcxc 元老 2004-12-22
  • 打赏
  • 举报
回复
要分页,还是一次显示,只是分组?
zjcxc 元老 2004-12-22
  • 打赏
  • 举报
回复
和上次那个差不多嘛

34,591

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧