`
seara
  • 浏览: 626991 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

妙用SQL Server聚合函数和子查询迭代求和

阅读更多
本文为原创,如需转载,请注明作者和出处,谢谢!

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

t_product


1

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

图2

从上面的查询结果可以看出,totalprice字段值的规则是从第1条记录到当前记录的price之和。如第3条记录的totalprice字段的值是10 + 25 + 36 = 71

现在要通过t_product表中的数据生成图2所示的查询结果。可能会有很多读者想到使用循环和游标,不过这种方式效率并不高,尤其在记录非常多的情况。

从图2的查询结果分析可知,这个结果仍然是求和的操作,只是并不是对所有的记录求和,也不是分组求和,而是使用迭代的方式进行求和,求和的公式如下:

当前记录的totalprice = 当前记录的price + 上一条记录的totalprice

上一条记录的totalprice值也可看成是当前记录以前所有记录的price值之和。因此,可以对每一条记录进行求和(使用sum函数),不过要求出当前记录及以前的记录的price之和,如下面的SQL语句:

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->selecta.xh,a.price,
(
selectsum(price)fromt_productbwhereb.xh<=a.xh)astotalprice
fromt_producta

从上面的SQL语句可以看出,使用了一个子查询来求totalprice字段的值,基本原理就是根据当前记录的xh值(a.xh)来计算从当前记录往前所有记录的price值之和,b.xh表示子查询当前的xh值,在子查询中,a.xh相当于常量。上面的SQL语句的查询结果和图2完全一样。如果我们的需求是不包含当前记录的price值,也就是说,计算totalprice字段的公式如下:

当前记录的totalprice = 上一条当前记录的price + 上一条记录的totalprice

第一条记录的totalprice值就是当前记录的price值,查询t_product表的结果如图3所示。


3

要查询出上述的记录也很容易,只需要将<=改成<即可,SQL语句如下:


<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->selecta.xh,a.price,
(
selectsum(price)fromt_productbwhereb.xh<a.xh)astotalprice
fromt_producta

但上面的SQL查询出来的记录的第一条的totalprice字段值为null,如图4所示。


4

为了将这个null换成10,可以使用case语句,SQL语句如下:


<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->selectxh,price,
(
casewhentotalpriceisnullthenpriceelsetotalpriceend)astotalprice
from
(
selecta.xh,(selectsum(price)fromt_productbwhereb.xh<a.xh)astotalprice,a.price
fromt_producta)x

在上面的SQL语句共有三层select查询,最里面一层如下:

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->selectsum(price)fromt_productbwhereb.xh<a.xh)

中间一层的子查询如下:

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->selecta.xh,(selectsum(price)fromt_productbwhereb.xh<a.xh)astotalprice,a.price
fromt_producta

最外面一层当然就是整个select语句了。

在执行上面的SQL后,将会得到和图3一样的查询结果了。

如果读者不喜欢写太长的SQL,可以将部分内容写到函数里,代码如下:

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->createfunctionmysum(@xhint,@priceint)returnsint
begin
return(select
(
casewhentotalpriceisnullthen@priceelsetotalpriceend)astotalprice
from(selectsum(price)astotalpricefromt_productwherexh<@xh)x)
end

可使用下面的SQL语句来使用这个函数:

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->selectxh,price,dbo.mysum(xh,price)astotalprice
fromt_product

在执行上面的SQL后,将得出如图3所示的查询结果。

建立t_product表的SQL语句(SQL Server 2005)如下:

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->SETANSI_NULLSON
GO
SETQUOTED_IDENTIFIERON
GO
IFNOTEXISTS(SELECT*FROMsys.objectsWHEREobject_id=OBJECT_ID(N'[dbo].[t_product]')ANDtypein(N'U'))
BEGIN
CREATETABLE[dbo].[t_product](
[xh][int]NOTNULL,
[price][int]NOTNULL,
CONSTRAINT[PK_t_product]PRIMARYKEYCLUSTERED
(
[xh]ASC
)
WITH(IGNORE_DUP_KEY=OFF)ON[PRIMARY]
)
ON[PRIMARY]
END



国内最棒的Google Android技术社区(eoeandroid),欢迎访问!

《银河系列原创教程》发布

《Java Web开发速学宝典》出版,欢迎定购

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics