首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

求教高手-根据两列的时间列进行统一[倒序]排序,该如何解决

2012-04-13 
求教高手-根据两列的时间列进行统一[倒序]排序表中分别有如下所示4个列,其中DialogueTime可能为NULL,ComeT

求教高手-根据两列的时间列进行统一[倒序]排序
表中分别有如下所示4个列,其中DialogueTime可能为NULL,ComeTimeC也可能为NULL。但是两列不可能同时为NULL。
要求:DialogueTime若不为NULL,则取DialogueTime进行排序,无视ComeTimeC。
  当DialogueTime为NULL时,则取ComeTimeC排序

HTML code
ID           NAME                   DialogueTime                                ComeTimeC1            小黑                   2012-02-18 08:33:39                     2012-02-25 23:57:392            小红                         NULL                              2015-03-18 08:33:393            小白                   2014-03-18 08:33:39                     2010-03-18 08:33:394            小青                   2012-03-18 08:33:39                              NULL5            小紫                         NULL                              2010-03-18 08:33:39


我想要的输出结果为(注:我要的是倒序排列,既日期大的显示在前面)->
HTML code
ID           NAME                     DialogueTime                                ComeTimeC2            小红                          NULL                             2015-03-18 08:33:393            小白                   2014-03-18 08:33:39                     2010-03-18 08:33:394            小青                   2012-03-18 08:33:39                              NULL5            小紫                            NULL                           2010-03-18 08:33:391            小黑                   2012-02-18 08:33:39                     2012-02-25 23:57:39



希望高手指点。。。

[解决办法]
要求:DialogueTime若不为NULL,则取DialogueTime进行排序,无视ComeTimeC。
当DialogueTime为NULL时,则取ComeTimeC排序

order by (case when DialogueTime is null then ComeTimeC else DialogueTime end) --desc
[解决办法]
SQL code
use Tempdbgo--> -->  if not object_id(N'Tempdb..#T') is null    drop table #TGoCreate table #T([ID] int,[NAME] nvarchar(2),[DialogueTime] Datetime,[ComeTimeC] Datetime)Insert #Tselect 1,N'小黑','2012-02-18 08:33:39','2012-02-25 23:57:39' union allselect 2,N'小红',null,'2015-03-18 08:33:39' union allselect 3,N'小白','2014-03-18 08:33:39','2010-03-18 08:33:39' union allselect 4,N'小青','2012-03-18 08:33:39',null union allselect 5,N'小紫',null,'2010-03-18 08:33:39'GoSelect * from #T  order by isnull([DialogueTime],[ComeTimeC]) desc/*2    小红    NULL    2015-03-18 08:33:39.0003    小白    2014-03-18 08:33:39.000    2010-03-18 08:33:39.0004    小青    2012-03-18 08:33:39.000    NULL1    小黑    2012-02-18 08:33:39.000    2012-02-25 23:57:39.0005    小紫    NULL    2010-03-18 08:33:39.000*/ 

热点排行