先看看下面的表和其中的数据:
t_product

该表有两个字段:xh 和price , 其中xh 是主索引字段,现在要得到如下的查询结果:


从上面的查询结果可以看出,totalprice 字段值的规则是从第1 条记录到当前记录的price 之和。如第3 条记录的totalprice 字段的值是10 + 25 + 36 = 71 。
现在要通过t_product 表中的数据生成图2 所示的查询结果。可能会有很多读者想到使用循环和游标,不过这种方式效率并不高,尤其在记录非常多的情况。
从图2 的查询结果分析可知,这个结果仍然是求和的操作,只是并不是对所有的记录求和,也不是分组求和,而是使用迭代的方式进行求和,求和的公式如下:
当前记录的totalprice 值 = 当前记录的price 值 + 上一条记录totalprice 值
上一条记录的totalprice 值也可看成是当前记录以前所有记录的price 值之和。因此,可以对每一条记录进行求和(使用sum 函数),不过要求出当前记录及以前的记录的price 之和,如下面的SQL 语句:
select a.xh, a.price,
( select sum (price) from t_product b where b.xh <= a.xh) as totalprice
from t_product a
( select sum (price) from t_product b where b.xh <= a.xh) as totalprice
from t_product a
从上面的SQL 语句可以看出,使用了一个子查询来求totalprice 字段的值,基本原理就是根据当前记录的xh 值(a.xh )来计算从当前记录往前所有记录的price 值之和,b.xh 表示子查询当前的xh 值,在子查询中,a.xh 相当于常量。上面的SQL 语句的查询结果和图2 完全一样。如果我们的需求是不包含当前记录的price 值,也就是说,计算totalprice 字段的公式如下:
当前记录的totalprice 值 = 上一条当前记录的price 值 + 上一条记录的totalprice 值
第一条记录的totalprice 值就是当前记录的price 值,查询t_product 表的结果如图3 所示。

要查询出上述的记录也很容易,只需要将<= 改成< 即可,SQL 语句如下:
select a.xh, a.price,
( select sum (price) from t_product b where b.xh < a.xh) as totalprice
from t_product a
( select sum (price) from t_product b where b.xh < a.xh) as totalprice
from t_product a
但上面的SQL 查询出来的记录的第一条的totalprice 字段值为null ,如图4 所示。

为了将这个null 换成10 ,可以使用case 语句,SQL 语句如下:
select xh, price,
( case when totalprice is null then price else totalprice end ) as totalprice
from
( select a.xh, ( select sum (price) from t_product b where b.xh < a.xh) as totalprice , a.price
from t_product a) x
( case when totalprice is null then price else totalprice end ) as totalprice
from
( select a.xh, ( select sum (price) from t_product b where b.xh < a.xh) as totalprice , a.price
from t_product a) x
在上面的SQL 语句共有三层select 查询,最里面一层如下:
select sum (price) from t_product b where b.xh < a.xh)
中间一层的子查询如下:
select a.xh, ( select sum (price) from t_product b where b.xh < a.xh) as totalprice , a.price
from t_product a
from t_product a
最外面一层当然就是整个select 语句了。
在执行上面的SQL 后,将会得到和图3 一样的查询结果了。
如果读者不喜欢写太长的SQL ,可以将部分内容写到函数里,代码如下:
create function mysum( @xh int , @price int ) returns int
begin
return ( select
( case when totalprice is null then @price else totalprice end ) as totalprice
from ( select sum (price) as totalprice from t_product where xh < @xh ) x)
end
begin
return ( select
( case when totalprice is null then @price else totalprice end ) as totalprice
from ( select sum (price) as totalprice from t_product where xh < @xh ) x)
end
可使用下面的SQL 语句来使用这个函数:
select xh, price, dbo.mysum(xh, price) as totalprice
from t_product
from t_product
在执行上面的SQL 后,将得出如图3 所示的查询结果。
建立t_product 表的SQL 语句(SQL Server 2005 )如下:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS ( SELECT * FROM sys.objects WHERE object_id = OBJECT_ID (N ' [dbo].[t_product] ' ) AND type in (N ' U ' ))
BEGIN
CREATE TABLE [ dbo ] . [ t_product ] (
[ xh ] [ int ] NOT NULL ,
[ price ] [ int ] NOT NULL ,
CONSTRAINT [ PK_t_product ] PRIMARY KEY CLUSTERED
(
[ xh ] ASC
) WITH (IGNORE_DUP_KEY = OFF ) ON [ PRIMARY ]
) ON [ PRIMARY ]
END
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS ( SELECT * FROM sys.objects WHERE object_id = OBJECT_ID (N ' [dbo].[t_product] ' ) AND type in (N ' U ' ))
BEGIN
CREATE TABLE [ dbo ] . [ t_product ] (
[ xh ] [ int ] NOT NULL ,
[ price ] [ int ] NOT NULL ,
CONSTRAINT [ PK_t_product ] PRIMARY KEY CLUSTERED
(
[ xh ] ASC
) WITH (IGNORE_DUP_KEY = OFF ) ON [ PRIMARY ]
) ON [ PRIMARY ]
END
《银河系列原创教程》发布
《Java Web开发速学宝典》出版,欢迎定购
版权声明:本文为weixin_34174422原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。