当前位置: 代码迷 >> Sql Server >> 求各位帮忙解答这个排序的有关问题
  详细解决方案

求各位帮忙解答这个排序的有关问题

热度:93   发布时间:2016-04-27 14:09:19.0
求各位帮忙解答这个排序的问题
tableC

Id Time
301 2011-03-01
301 2011-03-03
301 2011-03-04
304 2011-03-05
301 2011-03-08
304 2011-03-07


先按照Time降序排序(排在最上面的日期是最近的一个日期)
再把所有相同的Id排在一起(ID不排序,只是将相同的ID连在一起,只对Time进行排序)

我想实现这样的效果

301 2011-03-08
301 2011-03-04
301 2011-03-03
301 2011-03-01
304 2011-03-07
304 2011-03-05



------解决方案--------------------
SQL code
declare @tableC table (Id int,Time datetime)insert into @tableCselect 301,'2011-03-01' union allselect 301,'2011-03-03' union allselect 301,'2011-03-04' union allselect 304,'2011-03-05' union allselect 301,'2011-03-08' union allselect 304,'2011-03-07'select * from @tableC order by id ,time desc/*Id          Time----------- -----------------------301         2011-03-08 00:00:00.000301         2011-03-04 00:00:00.000301         2011-03-03 00:00:00.000301         2011-03-01 00:00:00.000304         2011-03-07 00:00:00.000304         2011-03-05 00:00:00.000*/
------解决方案--------------------
select * from tableC
order by Id,Time desc

ID不排序是放不到一起去的
------解决方案--------------------
SQL code
order by id,[time] desc
------解决方案--------------------
正常情况主键不能为空,其他为空的时候,排序desc现在在最上面。
  相关解决方案