2016-12-07 73 views
0

有什麼方法可以動態地命名Netezza中的pivoted列?在SSMS中,我會使用存儲過程,但是我們新的Netezza框限制了SP的創建。使用CASE語句,我可以使用行號來每月循環每日總和,但是我希望新的列名是實際的任意MONTHID而不是行號(MONTH_ID1包含ROW_NUMBER 1-12和相應的MONTHID)。例如,我在戰略上考慮引用和子查詢,但我無法實現它。Netezza中的動態列命名

select "DAY" 
,sum(case when "ROW_NUMBER" = 1 then SUMOFCHGS else null end) as "1" 
,sum(case when "ROW_NUMBER" = 2 then SUMOFCHGS else null end) as "2" 
,sum(case when "ROW_NUMBER" = 3 then SUMOFCHGS else null end) as "3" 
,sum(case when "ROW_NUMBER" = 4 then SUMOFCHGS else null end) as "4" 
,sum(case when "ROW_NUMBER" = 5 then SUMOFCHGS else null end) as "5" 
,sum(case when "ROW_NUMBER" = 6 then SUMOFCHGS else null end) as "6" 
,sum(case when "ROW_NUMBER" = 7 then SUMOFCHGS else null end) as "7" 
,sum(case when "ROW_NUMBER" = 8 then SUMOFCHGS else null end) as "8" 
,sum(case when "ROW_NUMBER" = 9 then SUMOFCHGS else null end) as "9" 
,sum(case when "ROW_NUMBER" = 10 then SUMOFCHGS else null end) as "10" 
,sum(case when "ROW_NUMBER" = 11 then SUMOFCHGS else null end) as "11" 
,sum(case when "ROW_NUMBER" = 12 then SUMOFCHGS else null end) as "12" 
FROM DLYAMTS D 
    JOIN MONTH_ID1 M ON D.MONTHID = M.MONTHID 
GROUP BY 1 
ORDER BY 1 

我可以想象在未來與列的數目可變這樣做,但我只是在尋找一個一年的月柱的現在。

回答

1

如果你想創建一個返回'表格對象'的sp,我有同樣的理解:不可能。 然而,在我們的商店裏,我們創建了基於一系列參數創建(永久)表的sp。其中一個參數是「目標表名稱」。其他參數在你的情況:'輸入表名','按列組','where子句','組織在'列和'分配在'列。

我希望這有助於:)

拉爾斯