我認爲下面是你想要的,或者至少可以讓你接近:
SELECT
CASE WHEN FCST_HR1 < 0 THEN 0 ELSE FCST_HR1 END AS FCST_HR1,
CASE WHEN FCST_HR2 < 0 THEN 0 ELSE FCST_HR2 END AS FCST_HR2,
CASE WHEN FCST_HR3 < 0 THEN 0 ELSE FCST_HR3 END AS FCST_HR3,
CASE WHEN FCST_HR4 < 0 THEN 0 ELSE FCST_HR4 END AS FCST_HR4,
CASE WHEN FCST_HR5 < 0 THEN 0 ELSE FCST_HR5 END AS FCST_HR5,
CASE WHEN FCST_HR6 < 0 THEN 0 ELSE FCST_HR6 END AS FCST_HR6,
CASE WHEN FCST_HR7 < 0 THEN 0 ELSE FCST_HR7 END AS FCST_HR7,
CASE WHEN FCST_HR8 < 0 THEN 0 ELSE FCST_HR8 END AS FCST_HR8,
CASE WHEN FCST_HR9 < 0 THEN 0 ELSE FCST_HR9 END AS FCST_HR9,
CASE WHEN FCST_HR10 < 0 THEN 0 ELSE FCST_HR10 END AS FCST_HR10,
CASE WHEN FCST_HR11 < 0 THEN 0 ELSE FCST_HR11 END AS FCST_HR11,
CASE WHEN FCST_HR12 < 0 THEN 0 ELSE FCST_HR12 END AS FCST_HR12
FROM
(select
FCST_HR1,
FCST_HR1 + FCST_HR2 AS FCST_HR2,
FCST_HR1 + FCST_HR2 + FCST_HR3 AS FCST_HR3,
FCST_HR1 + FCST_HR2 + FCST_HR3 + FCST_HR4 AS FCST_HR4,
FCST_HR1 + FCST_HR2 + FCST_HR3 + FCST_HR4 + FCST_HR5 AS FCST_HR5,
FCST_HR1 + FCST_HR2 + FCST_HR3 + FCST_HR4 + FCST_HR5 + FCST_HR6 AS FCST_HR6,
FCST_HR1 + FCST_HR2 + FCST_HR3 + FCST_HR4 + FCST_HR5 + FCST_HR6
+ FCST_HR7 AS FCST_HR7,
FCST_HR1 + FCST_HR2 + FCST_HR3 + FCST_HR4 + FCST_HR5 + FCST_HR6
+ FCST_HR7 + FCST_HR8 AS FCST_HR8,
FCST_HR1 + FCST_HR2 + FCST_HR3 + FCST_HR4 + FCST_HR5 + FCST_HR6
+ FCST_HR7 + FCST_HR8 + FCST_HR9 AS FCST_HR9,
FCST_HR1 + FCST_HR2 + FCST_HR3 + FCST_HR4 + FCST_HR5 + FCST_HR6
+ FCST_HR7 + FCST_HR8 + FCST_HR9 + FCST_HR10 AS FCST_HR10,
FCST_HR1 + FCST_HR2 + FCST_HR3 + FCST_HR4 + FCST_HR5 + FCST_HR6
+ FCST_HR7 + FCST_HR8 + FCST_HR9 + FCST_HR10 + FCST_HR11 AS FCST_HR11,
FCST_HR1 + FCST_HR2 + FCST_HR3 + FCST_HR4 + FCST_HR5 + FCST_HR6
+ FCST_HR7 + FCST_HR8 + FCST_HR9 + FCST_HR10 + FCST_HR11 + FCST_HR12 AS FCST_HR12
from RSRC_HR
where rsrc_id = '2045831579F511750') t
一點很難理解你想要什麼,但它似乎是跨列總運行?
在這種情況下,我可能會使用CLR函數並在.NET中執行繁重的工作。雖然你可以在存儲過程中做到這一點,但它需要很多代碼,我可能會使用遊標,樞軸和case語句,而不是SQL查詢最擅長的。 – XN16