有一数据表T:
D datetime, --年月日
R int       --降水量 要求:
查询降水量的最大值,并指出最大降水量时的日期。请问怎么实现啊?我现构想了一函数:
函数名:GetOccurDate
返回:日期(字符串形式,如有多个日期,每个日期用","分开)

解决方案 »

  1.   

    select * from [tbl] where R = (select max(R) from [tbl])
      

  2.   

    需要函数吗?好像很简单sql就解决了啊?楼主是不是有什么其他的需求?
      

  3.   

    http://community.csdn.net/Expert/topic/4210/4210476.xml?temp=.5060388
    具体问题!
      

  4.   

    /************函数如下,其中表名是Table3****************/CREATE FUNCTION GetOccurDate ()  
    RETURNS varchar(1024) AS  
    BEGIN 
    declare @D datetime
    declare @RtnValue varchar(1024)

    set @RtnValue=''
    declare cur1 cursor
    for
    select D from Table3 where R=(select max(R) from Table3)
    open cur1
    fetch cur1 into @D
    while @@fetch_status>=0
    begin
    if @RtnValue=''
    set @RtnValue=convert(varchar(4),year(@D))+'年'+convert(varchar(4),month(@D))+'月'+convert(varchar(4),day(@D))+'日'
    else
    set @RtnValue=@RtnValue+','+convert(varchar(4),year(@D))+'年'+convert(varchar(4),month(@D))+'月'+convert(varchar(4),day(@D))+'日'
    fetch cur1 into @D
    end
    close cur1
    deallocate cur1 return @RtnValue
    END/*****************调用示例,MyTest是数据库名******************/
    declare @ttt varchar(1024)select @ttt=MyTest.dbo.GetOccurDate()print @ttt/*****************返回值********************/
    1999年1月1日,2004年12月5日,2003年4月5日
      

  5.   

    select * from tableName where R = (select max(R) from tableName)
      

  6.   

    数据表Weather为:
    D datetime,    --年月日
    Rain int,      --降水量 
    Temp int,      --温度
    Press int,     --气压
    Humi  int      --湿度要求:
    查询最大降水量、最大降水量出现时间、最高气温、最高气温出现时间、最高气压、最高气压出现时间、最大湿度、最大湿度出现时间。
    用一个SQL语句一次性把这八个值求出来!我想构造一函数,输入一参数,如气温,则返回其对应的出现时间!这样,只需一个函数就可把上述要求中的四个时间字段求出来了!
    该怎么构造这个函数啊?
      

  7.   

    什么意思?
    一个函数,那拿那个参数直接查数据库就行了嘛,如果你想通用查,就带2个参数,第2个参数flag标志查什么...
      

  8.   

    (select Rain, D  from [tbl] where R = (select max(Rain) from [tbl])) union ((select Temp, D from [tbl] where R = (select max(Temp) from [tbl])) union (...) union (...)没试它得正确性,楼主试一下了 
      

  9.   


    select w1.D,w1.Rain,w2.D,w2.[Temp],w3.D,w3.Press,w4.D,w4.Humi 
    from  Weather as w1,Weather as w2,Weather as w3,Weather as w4
    where 
    w1.Rain in (select max(Rain) from Weather) and
    w2.[Temp] in (select max([Temp]) from Weather) and
    w3.Press in (select max(Press) from Weather) and
    w4.Humi in (select max(Humi) from Weather)同一字段最大值记录为多条时就有多条记录.
    并且是交叉连接的形式.如
    W1.D 有m条,W2.D 有n条,W3.D 有j条,W4.D 有k条,则搜索出来的记录就有m*n*j*k条.要不就每次只查一个参数,则记录数就为当前参数符合条件的记录数.