SELECT         MAX(字段名) AS Expr1
FROM             表名
where 字段名<>'1900-01-01'

解决方案 »

  1.   

    to xzl_kye(xiezhangliang) :好像你把人家的意思理解错了哦。
    是求横向,而不是纵向
      

  2.   

    --测试数据
    if object_id('ta') is not null drop table ta
    go
    create table ta (d1 datetime, d2 datetime, d3 datetime)
    insert ta select '2005-01-01', '2005-02-01', '1900-01-01'
    union all select '2005-01-01', '2005-02-01', '2005-02-04'
    union all select '2005-02-01', '2005-03-01', '2005-03-04'
    --创建用户自定义函数, 真正使用时,对函数进行一些较小的修改.
    --这种方式参数太多, 不过,个人觉得算是最简单的了.
    if object_id('dbo.maxtime') is not null drop  function dbo.maxtime
    go
    create function dbo.maxtime(@d1 datetime, @d2 datetime, @d3 datetime)
    returns datetime
    begin
     declare @result datetime
     set @result=@d1
     if @result<@d2 set @result=@d2
     if @result<@d3 set @result=@d3
     return @result
    end
    go
    --查询
    select 最晚时间= dbo.maxtime(d1, d2, d3)
    from ta 
    where '1900-01-01' not in(d1, d2, d3)