从五个日期字段中,依次从最早的日期开始排列到最晚的日期, 不管是哪个字段,只早日期比其它字段日期早就往前 排
如果第二条记录第二个字段日期比第一条记录的 第一个字段日期早那么就把第二条记录排在第一条前面,依次类推

解决方案 »

  1.   

    order by 日期1,日期2,日期3,日期4,日期5
      

  2.   


    ;with ach as
    (
      select id,date1 as date from tb
      union all
      select id,date2 from tb
      union all
      select id,date3 from tb
      union all
      select id,date4 from tb
      union all
      select id,date5 from tb
    ),art as
    (
      select *,rid=row_number() over (partition by id order by (case when date is null then 0 else date end),date)
      from ach
    )select id,
      max(case when rid=1 then date else null end) date1,
      max(case when rid=2 then date else null end) date2,
      max(case when rid=3 then date else null end) date3,
      max(case when rid=4 then date else null end) date4,
      max(case when rid=5 then date else null end) date5
    from art
    group by id
      

  3.   

    order by 日期字段1,日期字段2,日期字段3,日期字段4,日期字段5
      

  4.   

    order by (case when date is null then 0 else 1 end),date
      

  5.   

    例如写个函数
    DBO.GetMinDate(col1,col2,col3,col4)
    比较后返回最小值select * from tablename order by dbo.GetMinDate(col1,col2,col3,col4)
      

  6.   

    如果楼主只是要日期的顺序,而不是要表中所有数据的排序,可以:
    select 日期1 as 日期 from tb
    union
    select 日期2 from tb
    union
    select 日期3 from tb
    union
    select 日期4 from tb
    union
    select 日期5 from tb order by 1
      

  7.   

    create function f_name(@date1 @datetime,@date2 @datetime,@date3 @datetime,
                           @date4 @datetime,@date5 @datetime)
    return @datetime
    as
    begin
        select @date1=(case when @date1<@date2 then @date2 else @date1 end)
        select @date1=(case when @date1<@date3 then @date3 else @date1 end)
        select @date1=(case when @date1<@date4 then @date4 else @date1 end)
        select @date1=(case when @date1<@date5 then @date5 else @date1 end)
        return @date1
    endselect * from tb order by dbo.f_name(日期1,日期2,日期3,日期4,日期5)
      

  8.   

    create function f_name(@date1 datetime,@date2 datetime,@date3 datetime,
                           @date4 datetime,@date5 datetime)
    return datetime
    as
    begin
        select @date1=(case when @date1<@date2 then @date2 else @date1 end)
        select @date1=(case when @date1<@date3 then @date3 else @date1 end)
        select @date1=(case when @date1<@date4 then @date4 else @date1 end)
        select @date1=(case when @date1<@date5 then @date5 else @date1 end)
        return @date1
    endselect * from tb order by dbo.f_name(日期1,日期2,日期3,日期4,日期5)
      

  9.   

    如果楼主要的是以列中日期最小值比较,作为列的排序优先次序,则要进行一大堆判断:
    order by 含有最小日期的列名,含有第二个最小日期的列名,含有......
      

  10.   

    --如果把同行记录的字段从小到大排序,假设每行有个ID
    --sql 2000select id , 
           max(case px when 1 then c else null end) c1,
           max(case px when 2 then c else null end) c2,
           max(case px when 3 then c else null end) c3,
           max(case px when 4 then c else null end) c4,
           max(case px when 5 then c else null end) c5
    from
    (
    select m.* , px = (select count(1) from 
    (
    select id , c1 c from tb
    union all
    select id , c2 c from tb
    union all
    select id , c3 c from tb
    union all
    select id , c4 c from tb
    union all
    select id , c5 c from tb
    ) n where n.id = m.id and n.c < m.c) + 1 from
    (
    select id , c1 c from tb
    union all
    select id , c2 c from tb
    union all
    select id , c3 c from tb
    union all
    select id , c4 c from tb
    union all
    select id , c5 c from tb
    ) m
    ) t
    group by id--sql 2005
    select id , 
           max(case px when 1 then c else null end) c1,
           max(case px when 2 then c else null end) c2,
           max(case px when 3 then c else null end) c3,
           max(case px when 4 then c else null end) c4,
           max(case px when 5 then c else null end) c5
    from
    (
    select m.* , px = row_number() over(partition by id , order by c) from
    (
    select id , c1 c from tb
    union all
    select id , c2 c from tb
    union all
    select id , c3 c from tb
    union all
    select id , c4 c from tb
    union all
    select id , c5 c from tb
    ) m
    ) t 
    group by id
      

  11.   

    修改下,最早的话,函数应该是小于
    create function f_name(@date1 datetime,@date2 datetime,@date3 datetime,
                           @date4 datetime,@date5 datetime)
    return datetime
    as
    begin
        select @date1=(case when @date1>@date2 then @date2 else @date1 end)
        select @date1=(case when @date1>@date3 then @date3 else @date1 end)
        select @date1=(case when @date1>@date4 then @date4 else @date1 end)
        select @date1=(case when @date1>@date5 then @date5 else @date1 end)
        return @date1
    endselect * from tb order by dbo.f_name(日期1,日期2,日期3,日期4,日期5)