首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

SQL排序與計算問題解决思路

2012-03-04 
SQL排序與計算問題auto_nomat_qtydec_qtyid_noA000510000100001B0001-50095002C000115000245004B0002-1200

SQL排序與計算問題
auto_nomat_qtydec_qtyid_no      
A000510000100001
B0001-50095002
C000115000245004
B0002-12000125003
B0003-500075005
B0004300045006

表格內容及結果如上﹐dec_qty值為上一筆dec_qty與當前記錄mat_qty之和﹐請各位幫忙以下表格內容要如何實現?謝謝﹗﹗

auto_nomat_qtydec_qtyid_no
A000510000100001
B0001-50095002
C000115000245004
B0002-12000125003
B0003-500075005
B0004300045006


[解决办法]
魚不對吧
[解决办法]
--如果id_no沒有順序的話

--創建測試環境
Create Table 表
(auto_no Char(5),
mat_qty Int,
dec_qty Int,
id_no Int)
--插入數據
Insert 表 Select 'A0005 ',10000,0,1
Union All Select 'B0001 ',-500,0,2
Union All Select 'C0001 ',15000,0,4
Union All Select 'B0002 ',-12000,0,3
Union All Select 'B0003 ',-5000,0,5
Union All Select 'B0004 ',-3000,0,6
GO
--測試
--更新
Declare @dec_qty Int
Select @dec_qty = 0

Update 表 Set @dec_qty = @dec_qty + mat_qty, dec_qty = @dec_qty

Select * From 表
GO
--刪除測試環境
Drop Table 表
--結果
/*
auto_nomat_qtydec_qtyid_no
A000510000100001
B0001-50095002
C000115000245004
B0002-12000125003
B0003-500075005
B0004-300045006
*/

热点排行