先看看下面的表和其中的數據:
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開發速學寶典》出版,歡迎定購