有一数据表T:
D datetime, --年月日
R int --降水量 要求:
查询降水量的最大值,并指出最大降水量时的日期。请问怎么实现啊?我现构想了一函数:
函数名:GetOccurDate
返回:日期(字符串形式,如有多个日期,每个日期用","分开)
D datetime, --年月日
R int --降水量 要求:
查询降水量的最大值,并指出最大降水量时的日期。请问怎么实现啊?我现构想了一函数:
函数名: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日
D datetime, --年月日
Rain int, --降水量
Temp int, --温度
Press int, --气压
Humi int --湿度要求:
查询最大降水量、最大降水量出现时间、最高气温、最高气温出现时间、最高气压、最高气压出现时间、最大湿度、最大湿度出现时间。
用一个SQL语句一次性把这八个值求出来!我想构造一函数,输入一参数,如气温,则返回其对应的出现时间!这样,只需一个函数就可把上述要求中的四个时间字段求出来了!
该怎么构造这个函数啊?
一个函数,那拿那个参数直接查数据库就行了嘛,如果你想通用查,就带2个参数,第2个参数flag标志查什么...
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条.要不就每次只查一个参数,则记录数就为当前参数符合条件的记录数.