现在有个数据表:data 表中字段有:
id, dianliang, datatime 1 36800.2 2008-04-18 11:38:12.000 1 36750.2 2008-04-18 07:32:39.000 1 36700.2 2008-04-17 11:27:12.000 1 36500.2 2008-04-16 11:21:45.000 1 36300.2 2008-04-10 11:16:15.000 1 36200.2 2008-04-09 05:10:43.000 1 36100.2 2008-04-08 02:05:06.000 下面是SQL语句: txtSQL = "select top 1 case when Datediff(day,datatime ,getdate()) <1 then rtrim(dianliang) else '0' end [days],case when Datediff(day,datatime ,getdate()) <7 then rtrim(dianliang) else '0' end [weeks],case when Datediff(day,datatime ,getdate()) <30 then rtrim(dianliang) else '0' end [moths] from data where id='" & Text1.Text& "' order by datatime desc" 这个语句得到日,周,月的最大值 在 Text1.Text中输入1 得到结果: 日:50 周:500 月:700 现在在程序里这样执行速度很慢,希望大家帮个忙,谢谢,俺菜鸟一个别介意
谢谢楼上的几位兄弟,根据情况我改了下代码:
create table A (id int,dianliang decimal(20,2) , date datetime)
insert into A values(1,36800.2,'2008-04-18 11:38:12.000')
insert into A values(1,36750.2,'2008-04-18 07:32:39.000 ')
insert into A values(1,36700.2,'2008-04-17 11:27:12.000')
insert into A values(1,36500.2,'2008-04-16 11:21:45.000')
insert into A values(1,36300.2,'2008-04-10 11:16:15.000')
insert into A values(1,36200.2,'2008-04-09 05:10:43.000')
insert into A values(1,36100.2,'2008-04-08 02:05:06.000')
go
CREATE proc t_up1
@id int
as
begin
declare @maxDL decimal ,
@minDayDL decimal,
@minWeekDL decimal ,
@minMonthDL decimal,
@DayDL decimal
-- get the maxDL
select @maxDL = max(convert(decimal,dianliang)) from A where date >= convert(nvarchar(20),getdate(),101)
--get the minDayDL
select @minDayDL = min(dianliang) from A where date>convert(nvarchar(20),getdate(),101)
--get the minWeekDL
select @minWeekDL = min(dianliang) from A where date>convert(nvarchar(20),dateadd(day,-7,getdate()),101)
--get the minMonthDL
select @minMonthDL = min(dianliang) from A where date>convert(nvarchar(20),dateadd(day,-30,getdate()),101)
begin
set @DayDL=@maxDL -@minDayDL
if @maxDL=null
begin
select @maxDL=max(dianliang) from a
set @DayDL=0
end
select @DayDL as DayDL, @maxDL - @minWeekDL as WeekDL,@maxDL - @minMonthDL as MonthDL
end
end GO
这样如果当天没有数据的话那么周,月就不会出现空的情况