数据库有内容大概形式如下
日期 值
2007-05-06 20
2007-05-07 23
2007-05-09 15
2007-05-11 2
2007-05-12 55
......
怎样显示如下形式,把所缺天数的值显示为0
2007-05-05=0
2007-05-06=20
2007-05-07=23
2007-05-08=0
2007-05-09=19
2007-05-10=0
2007-05-11=2
2007-05-12=55
......
要求效率高的方法和代码,谢谢各位。
日期 值
2007-05-06 20
2007-05-07 23
2007-05-09 15
2007-05-11 2
2007-05-12 55
......
怎样显示如下形式,把所缺天数的值显示为0
2007-05-05=0
2007-05-06=20
2007-05-07=23
2007-05-08=0
2007-05-09=19
2007-05-10=0
2007-05-11=2
2007-05-12=55
......
要求效率高的方法和代码,谢谢各位。
2.查的时候在存储过程做文章
1.table 显示前做个循环替换下
2.查的时候在存储过程做文章
-------------------------------
正解
gisfarmer(菜菜阿杰)能否把存储过程代码帖出来
select 日期,值 from 表
while(dr.read)
{
创建动态控件 ... 付值
}
insert ta
select '2007-01-02', '45'
union all select '2007-01-01' , '12'
union all select '2007-01-04' , '24'生成一个时间段列:
declare @ta table(t_date datetime)
declare @i datetime
set @i='2007-01-01 00:00:00'
while @i!>'2007-1-31 00:00:00'
begin
insert @ta select convert(varchar(10),@i,120)
select @i=@i+1
endselect convert(varchar(10),t_date,120)+' = '+isnull(值,0) from @ta a , ta
where convert(varchar(10),t_date,120)*=convert(varchar(10),"日期",120)
and convert(varchar(10),t_date,120) between '2007-01-02'and '2007-01-31'
//各种数据库的SQL语句有些不同,这个是oracle
select concat(concat(a1,'='),a2) from
(
select cast(日期 as varchar(20 )) a1 ,cast(值 as varchar(20)) a2
from 表
)//如果要对值进行空值的判断,可以使用case when ,查一下它的用法吧
tempDeclare @begindate datetime
Declare @enddate datetime
set @begindate ='2007-05-05'
set @enddate ='2007-06-05'select * into temp from 原表WHILE (@begindate < @enddate )
BEGIN
--要求效率这里可以判断一下@begindate在原表是否存在,不存在再添加
INSERT INTO temp VALUES
(@begindate ,0)
SET @begindate = @begindate + 1
END
select 日期,sum(值) as 值 from
temp group by 日期
drop temp
采用表对比的方法left JOIN然后isnull(#t1.值,0)谢谢各位了