我有一个用户表 
  id  name    tag(推荐为 1   不推荐为0 )  tjtime  ( 推荐时间)我想得到这样的结果 
  前面是所有推荐的用户按照 推荐时间 desc 排序
  接着是显示没有推荐的用户按照 ID 的desc 排序????????????????????在线等待!!!!!!

解决方案 »

  1.   

    Select * from [User]
    order by tag desc,(case when tag=1 then 0 else ID end) desc, 
             tjtime desc
      

  2.   

    select * from usetable
    order by tag,case when tag=1 then tjtime
       else convert(char(10),id) end 
     desc
      

  3.   

    Select * from [User]
    order by tag desc,
             (case when tag=1 then tjtime else null end) desc, 
             (case when tag=1 then null else id end) desc
    注意:0并不是最小的id
      

  4.   

    Select * from [User]
    order by tag desc,(case when (tag=1) and (Datediff(day,tjtime,getdate())=0)
                       then -1 else Abs(ID) end) desc,  tjtime desc
      

  5.   

    select *
    from [user]
    where tag=1
    order by tjtime desc
    union all
    select *
    from [user]
    where tag=0
    order by id desc
      

  6.   

    union不能有两个Order by吧!
      

  7.   

    declare @t table(id int,name varchar(20),tag int,tjtime datetime)
    insert into @t
    select 1,'n1',0,'1980-11-1' union all
    select 2,'n2',1,'1980-12-1' union all
    select 3,'n3',0,'1980-3-1' union all
    select 4,'n4',1,'1980-4-1' union all
    select 5,'n5',1,'1980-5-1' union all
    select 6,'n6',0,'1980-6-1' union all
    select 7,'n7',0,'1980-7-1' union all
    select 8,'n8',1,'1980-8-1' union all
    select 9,'n9',1,'1980-9-1'
    select * from @t
    order by tag desc,(case when tag=1 then tjtime else id end) descid          name                 tag         tjtime                                                 
    ----------- -------------------- ----------- ------------------------------------------------------ 
    2           n2                   1           1980-12-01 00:00:00.000
    9           n9                   1           1980-09-01 00:00:00.000
    8           n8                   1           1980-08-01 00:00:00.000
    5           n5                   1           1980-05-01 00:00:00.000
    4           n4                   1           1980-04-01 00:00:00.000
    7           n7                   0           1980-07-01 00:00:00.000
    6           n6                   0           1980-06-01 00:00:00.000
    3           n3                   0           1980-03-01 00:00:00.000
    1           n1                   0           1980-11-01 00:00:00.000(所影响的行数为 9 行)楼上说的很对:union不能有两个Order by
    改正了下,并提供了测试结果。
      

  8.   

    按原来的思路写Select * from [User]
    order by case when tag=1 and datediff(day,tjtime,getdate())<1 then 0 else tag end desc,
             (case when tag=1 then tjtime else null end) desc, 
             (case when tag=1 then null else id end) desc
      

  9.   

    Select * from [User]
    order by case when tag=1 and datediff(day,tjtime,getdate())<1 then 0 else tag end desc,
             (case when tag=1 and datediff(day,tjtime,getdate())>=1 then tjtime else null end) desc, 
             (case when tag=1 and datediff(day,tjtime,getdate())>=1 then null else id end) desc
      

  10.   

    declare @t table(
    id  int,
    name varchar(20),   
    tag bit, 
    tjtime datetime
    )insert @t
    select 1,'1',0,'2006-8-21'
    union all
    select 82,'82',0,'2006-8-17'
    union all
    select 3,'3',0,'2006-8-15'
    union all
    select 4,'4',0,'2006-8-19'
    union all
    select 5,'5',0,'2006-8-20'
    union all
    select 6,'6',0,'2006-8-18'
    union all
    select 11,'11',1,'2006-8-22'
    union all
    select 12,'12',1,'2006-8-19'
    union all
    select 13,'13',1,'2006-8-18'
    union all
    select 14,'14',1,'2006-8-17'
    union all
    select 15,'15',1,'2006-8-20'
    union all
    select 16,'16',1,'2006-8-21'
    union all
    select 97,'97',1,'2006-8-10'
    union all
    select 107,'107',0,'2006-8-10'
    Select * from @t
    order by case when tag=1 and datediff(day,tjtime,getdate())>-1 then 0 else tag end desc,
             (case when tag=1 and datediff(day,tjtime,getdate())<=-1 then tjtime else null end) desc, 
             (case when tag=1 and datediff(day,tjtime,getdate())<=-1 then null else id end) desc--结果
    id          name                 tag  tjtime                                                 
    ----------- -------------------- ---- ------------------------------------------------------ 
    11          11                   1    2006-08-22 00:00:00.000
    16          16                   1    2006-08-21 00:00:00.000
    15          15                   1    2006-08-20 00:00:00.000
    107         107                  0    2006-08-10 00:00:00.000
    97          97                   1    2006-08-10 00:00:00.000
    82          82                   0    2006-08-17 00:00:00.000
    14          14                   1    2006-08-17 00:00:00.000
    13          13                   1    2006-08-18 00:00:00.000
    12          12                   1    2006-08-19 00:00:00.000
    6           6                    0    2006-08-18 00:00:00.000
    5           5                    0    2006-08-20 00:00:00.000
    4           4                    0    2006-08-19 00:00:00.000
    3           3                    0    2006-08-15 00:00:00.000
    1           1                    0    2006-08-21 00:00:00.000(所影响的行数为 14 行)
      

  11.   

    --更正下
    declare @t table(
    id  int,
    name varchar(20),   
    tag bit, 
    tjtime datetime
    )insert @t
    select 1,'1',0,'2006-8-21'
    union all
    select 82,'82',0,'2006-8-17'
    union all
    select 3,'3',0,'2006-8-15'
    union all
    select 4,'4',0,'2006-8-19'
    union all
    select 5,'5',0,'2006-8-20'
    union all
    select 6,'6',0,'2006-8-18'
    union all
    select 11,'11',1,'2006-8-22'
    union all
    select 12,'12',1,'2006-8-19'
    union all
    select 13,'13',1,'2006-8-18'
    union all
    select 14,'14',1,'2006-8-17'
    union all
    select 15,'15',1,'2006-8-20'
    union all
    select 16,'16',1,'2006-8-21'
    union all
    select 97,'97',1,'2006-8-10'
    union all
    select 107,'107',0,'2006-8-10'
    Select * from @t
    order by case when tag=1 and datediff(day,tjtime,getdate())>0 then 0 else tag end desc,
             (case when tag=1 and datediff(day,tjtime,getdate())<=0 then tjtime else null end) desc, 
             (case when tag=1 and datediff(day,tjtime,getdate())<=0 then null else id end) desc--结果
    id          name                 tag  tjtime                                                 
    ----------- -------------------- ---- ------------------------------------------------------ 
    11          11                   1    2006-08-22 00:00:00.000
    16          16                   1    2006-08-21 00:00:00.000
    15          15                   1    2006-08-20 00:00:00.000
    12          12                   1    2006-08-19 00:00:00.000
    107         107                  0    2006-08-10 00:00:00.000
    97          97                   1    2006-08-10 00:00:00.000
    82          82                   0    2006-08-17 00:00:00.000
    14          14                   1    2006-08-17 00:00:00.000
    13          13                   1    2006-08-18 00:00:00.000
    6           6                    0    2006-08-18 00:00:00.000
    5           5                    0    2006-08-20 00:00:00.000
    4           4                    0    2006-08-19 00:00:00.000
    3           3                    0    2006-08-15 00:00:00.000
    1           1                    0    2006-08-21 00:00:00.000(所影响的行数为 14 行)