T-SQL计算固定资产折旧(直线法)

2014-11-24 16:47:41 · 作者: · 浏览: 0

T-SQL计算固定资产折旧(直线法)
使用 CTE, 计算固定资产折旧(直线法)。
[sql]
DECLARE @Assets TABLE (NAME VARCHAR(20), PurchaseCost MONEY, Period INT)
INSERT INTO @Assets
SELECT '计算机', 5000, 24
;WITH SLDepSched (AssetID, [Month], Period -- 固定资产
,SLDepAmt, SLBookValue, SLCumDep -- 直线法
) AS (
www.2cto.com
SELECT NAME, 0, Period
,ROUND(PurchaseCost/Period, 2) -- 直线法折旧额
,PurchaseCost, CAST(0 AS MONEY)
FROM @Assets
UNION ALL
SELECT AssetID, [Month]+1, Period
,CASE [Month]+1 WHEN Period THEN SLBookValue ELSE SLDepAmt END
,CASE [Month]+1 WHEN Period THEN CAST(0 AS MONEY) ELSE SLBookValue - SLDepAmt END
,CASE [Month]+1 WHEN Period THEN SLCumDep + SLBookValue ELSE SLCumDep + SLDepAmt END
FROM SLDepSched
WHERE [Month] < Period)
SELECT AssetID, [Month], SLDepAmt, SLBookValue, SLCumDep
FROM SLDepSched
www.2cto.com
ORDER BY AssetID, [Month]
计算机 0 208.33 5000.00 0.00
计算机 1 208.33 4791.67 208.33
计算机 2 208.33 4583.34 416.66
计算机 3 208.33 4375.01 624.99
计算机 4 208.33 4166.68 833.32
计算机 5 208.33 3958.35 1041.65
计算机 6 208.33 3750.02 1249.98
计算机 7 208.33 3541.69 1458.31
计算机 8 208.33 3333.36 1666.64
计算机 9 208.33 3125.03 1874.97
计算机 10 208.33 2916.70 2083.30
计算机 11 208.33 2708.37 2291.63
计算机 12 208.33 2500.04 2499.96
计算机 13 208.33 2291.71 2708.29
计算机 14 208.33 2083.38 2916.62
计算机 15 208.33 1875.05 3124.95
计算机 16 208.33 1666.72 3333.28
计算机 17 208.33 1458.39 3541.61
计算机 18 208.33 1250.06 3749.94
计算机 19 208.33 1041.73 3958.27
计算机 20 208.33 833.40 4166.60
计算机 21 208.33 625.07 4374.93
计算机 22 208.33 416.74 4583.26
计算机 23 208.33 208.41 4791.59
计算机 24 208.41 0.00 5000.00
作者 incognito007