select
    id,max(time) as time
from 
   (select id,time1 as time from A 
    union all 
    select id,time2 as time from A 
    union all 
    select id,time3 as time from A 
    union all 
    select id,time4 as time from A) t
group by
    id

解决方案 »

  1.   

    select max([time]) from (
    select time1 [time] from A where id=@id union all
    select time2 from A where id=@id union all
    select time3 from A where id=@id union all
    select time4 from A where id=@id )a
      

  2.   


    select id,[time]=(select distinct max(time) from (select time1 from A where id=b.id union all select time2 from A where id=b.id union all select time3 from A where id=b.id union all select time4 from A where id=b.id) c) b
      

  3.   

    select
        id,max(time) as time
    from 
       (select id,time1 as time from A 
        union all 
        select id,time2 as time from A 
        union all 
        select id,time3 as time from A 
        union all 
        select id,time4 as time from A) t
    group by
        id----------------------------------------------------
    赞一个!libin_ftsafe(子陌红尘)
      

  4.   

    --创建表
    create table maxTime(
    id int,
    time1 datetime,
    time2 datetime,
    time3 datetime,
    time4 datetime
    )--测试数据
    insert into maxTime
    select 1,'9:01','9:02','9:03','9:04'
    union all
    select 2,'10:01','10:02','10:03','10:04'
    union all
    select 3,'11:01','11:02','11:03','11:04'--测试
    declare @id int
    set @id=2
    select max(t) as MaxTime from (
    select [time1] as t from maxTime where id=@id
    union all
    select [time2] as t from maxTime where id=@id
    union all
    select [time3] as t from maxTime where id=@id
    union all
    select [time4] as t from maxTime where id=@id
    ) tempTable--删除表
    drop table maxTime
      

  5.   

    create   table   maxTime( 
    id   int, 
    time1   datetime, 
    time2   datetime, 
    time3   datetime, 
    time4   datetime 
    ) --测试数据 
    insert   into   maxTime 
    select   1,'9:01','9:02','9:03','9:04' 
    union   all 
    select   2,'10:01','10:02','10:03','10:04' 
    union   all 
    select   3,'11:01','11:02','11:03','11:04' select id,(select distinct max(time1) from (select time1 from maxtime where id=b.id union all select time2 from maxtime where id=b.id union all select time3 from maxtime where id=b.id union all select time4 from maxtime where id=b.id) c)  from maxtime bid          
    ----------- -----------------------
    1           1900-01-01 09:04:00.000
    2           1900-01-01 10:04:00.000
    3           1900-01-01 11:04:00.000(3 行受影响)
      

  6.   

    我的和 libin_ftsafe(子陌红尘)比较一下执行计划create   table   maxTime( 
    id   int, 
    time1   datetime, 
    time2   datetime, 
    time3   datetime, 
    time4   datetime 
    ) --测试数据 
    insert   into   maxTime 
    select   1,'9:01','9:02','9:03','9:04' 
    union   all 
    select   2,'10:01','10:02','10:03','10:04' 
    union   all 
    select   3,'11:01','11:02','11:03','11:04' select id,(select distinct max(time1) from (select time1 from maxtime where id=b.id union all select time2 from maxtime where id=b.id union all select time3 from maxtime where id=b.id union all select time4 from maxtime where id=b.id) c)  from maxtime bselect
        id,max(time) as time
    from 
       (select id,time1 as time from maxtime 
        union all 
        select id,time2 as time from maxtime
        union all 
        select id,time3 as time from maxtime 
        union all 
        select id,time4 as time from maxtime) t
    group by
        id好像我的快点哦 
      

  7.   

    --不能用UNION 当在A表中存在ID 相同的行时会丢失数据。
    select id,
    case when 
          (case when time1 > time2 then time1 else time2 end) >
          (case when time3 > time4 then time2 else time4 end) 
         then
          (case when time1 > time2 then time1 else time2 end)  
         else
          (case when time3 > time4 then time3 else time4 end) 
         end
    from A
      

  8.   

    create function getmax(@t1 datetiem,@t2 datetiem,@t3 datetiem,@t4 datetiem)
    returns datetime
    as
    begin
     declare @t datetime
     set @t = @t1
     if @t < @t2 set @t = @t2
     if @t < @t3 set @t = @t3
     if @t < @t4 set @t = @t4
     return @t
    end
     select id,getmax(time1,time2,time3,time4) from A