[转]Sql Server累计求和问题
供电局6月1号到8号供点电量分别为8.14,8.25,8.33,8.36,8.47,8.77,8.23,8.45
以次数据构成的数据库,要求查询累积供电量,
比如2号的累积供电量为1号和2号供电量的加和;
4号供电量是1号,2号,3号,4号供电量的加和。
依次类推,要求用SQL语句将8天的累积供电量查询出来
1.建表:
USE [example]
GO
/****** 对象: Table [dbo].[DianLi] 脚本日期: 02/24/2006 12:52:02 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[DianLi](
[_Date] [datetime] NULL,
[DL] [real] NULL
) ON [PRIMARY]
2.Sql语句
select _date,DL=(select sum(DL) from dianli where _date<=a._date) from
dianli a
GO
/****** 对象: Table [dbo].[DianLi] 脚本日期: 02/24/2006 12:52:02 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[DianLi](
[_Date] [datetime] NULL,
[DL] [real] NULL
) ON [PRIMARY]
2.Sql语句
select _date,DL=(select sum(DL) from dianli where _date<=a._date) from
dianli a