google搜索 51Testing站内搜索                    软件测试门户 | 软件测试培 训 | 文章资料精选 | 软件测试论坛 | 软件测试博客 | 测试招聘求职 
打印

一个SQL的问题

一个SQL的问题


给出一个表的数据如下
日期     电费
6月1日   60
6月2日   34
6月3日
6月4日

条件3日的电费=1日+2日
  4日的电费=2日+3日
写sql语句,最终显示出六月所有电费

请教是不是要写一个存储器?

TOP

~写不来存储器啊~
不知道这个可不可以,呵呵~
if exists (select * from sysobjects where name = 'table1')
drop table table1
create table table1 (date int , fare int)
insert into table1 values(1 , 60)
insert into table1 values(2 , 34)
declare @a int
declare @b int
declare @x int
declare @y int
set @a = 1
set @b = 2
while (@b<=29)
begin
set @x = (select fare from table1 where date = @a)
set @y = (select fare from table1 where date = @b)
insert into table1 values(@a+2 , @x+@y)
set @a = @a + 1
set @b = @b + 1
end

select * from table1

TOP

建议使用一个函数:用来计算某天的电费,然后调用这个函数。
其实这个表设计的有问题,不应该这样设计的。
既然有这个需求,为什么不在设计数据库时多增加一列呢?

TOP

 
当前时区 GMT+8, 现在时间是 2008-7-6 09:11Copyright(C)上海博为峰软件技术有限公司 2001-2007 电话:021-64471599-8017
当您在访问网站、论坛及博客过程中遇到问题时可发送email:webmaster@51testing.com或发送论坛短信至管理员风在吹