妙用SQL Server聚合函數和子查詢迭代求和

先看看下面的表和其中的數據:
t_product
該表有兩個字段:xhprice 其中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
從上面的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
但上面的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
在上面的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
最外面一層當然就是整個select語句了。
在執行上面的SQL後,將會得到和圖3一樣的查詢結果了。
如果讀者不喜歡寫太長的SQL,可以將部分內容寫到函數裏,代碼如下:
create function mysum(@xh int@price intreturns int
begin
  
return (select 
          (
case when totalprice is null then @price  else totalprice endas 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
在執行上面的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 = OFFON [PRIMARY]
ON [PRIMARY]
END
 
《銀河系列原創教程》發佈
《Java Web開發速學寶典》出版,歡迎定購
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章