1
如果你可以請這個幫忙。該代碼會生成一個錯誤: Msg 8156,級別16,狀態1,行236 對'piv1'多次指定了'Classification_Value_Id'列。SQL數據透視問題
我在SQL Server上這樣做。代碼步驟如下: 1.將源數據庫中的數據取消轉移[dbo]。[分類]合成一列 2.將此未轉移數據加入名爲DB.dbo [Classification_Value]的表中返回/加列「cv.Classification_Name」的數據集 3樞軸此數據集(這是返回錯誤的部分)
CODE:
SELECT
activityCode
, actjvPartnerRef
, actMonth
, actSalesChannel
, addCBPCharge
, agentId
, appType
, areaCode
--SELECT
--polRef,[Arrangement_Id],UnpivotedData.Classification_Value_Id,UnpivotedData.Classification_Scheme_Id,ColValues, ColNames,cv.Classification_Name
FROM
(
SELECT top 10
[polRef]
, [Arrangement_Id]
, [Classification_Scheme_Id]
, [Classification_Value_Id]
-- ,[Arrangement_Classification_Type_Id]
-- ,[Effective_TimeStamp]
-- ,[End_date]
, CAST((ISNULL([character_measure],'')) AS NVARCHAR(MAX)) AS character_measure
, CAST((ISNULL([datetime_measure],'')) AS NVARCHAR(MAX)) AS datetime_measure
, CAST([decimal_measure] AS NVARCHAR(MAX)) AS decimal_measure
, CAST((ISNULL([integer_measure],'')) AS NVARCHAR(MAX)) AS integer_measure
, CAST((ISNULL([logical_measure],'')) AS NVARCHAR(MAX)) AS logical_measure
, CAST((ISNULL([charmax_measure],'')) AS NVARCHAR(MAX)) AS charmax_measure
, CAST((ISNULL([long_measure],'')) AS NVARCHAR(MAX)) AS long_measure
FROM DB.[dbo].[Classification]
) AS SrcDataConverted
UNPIVOT
(
ColValues FOR ColNames IN
(
character_measure
, datetime_measure
, decimal_measure
, integer_measure
, logical_measure
, charmax_measure
, long_measure
)
) AS UnpivotedData
LEFT JOIN DB.dbo.[Classification_Value] cv
ON cv.[Classification_Scheme_Id] = UnpivotedData.[Classification_Scheme_Id]
AND cv.Classification_Value_Id = UnpivotedData.Classification_Value_Id
PIVOT
(MAX(ColValues) for Classification_Name in (
activityCode
, actjvPartnerRef
, actMonth
, actSalesChannel
, addCBPCharge
, agentId
, appType
, areaCode
)) AS piv1;
任何幫助將不勝感激
謝謝
1 - 一個附加派生表需要在開始重新PIVOT前包裹'UNPIVOT'的結果,作爲聯接引入了複製'Classification_Value_Id'和'Classification_Scheme_id',這是需要的連接。 [SqlFiddle here](http://sqlfiddle.com/#!3/a2ac0/2) – StuartLC
@brett感謝您的回覆:以下錯誤代碼:消息207,級別16,狀態1,行12 無效的列名稱' activityCode」。 消息207,級別16,狀態1,行13 無效的列名稱'actjvPartnerRef'。 消息207,級別16,狀態1,行14 無效的列名稱'actMonth'。 ..... 消息265,級別16,狀態1,行56 在PIVOT運算符中指定的列名稱「activityCode」與PIVOT參數中的現有列名稱衝突。 Msg 265,Level 16,State 1,Line 57 ... – Aarion
我已根據提供的小提琴更正了我的代碼(謝謝@StuartLC)。我以爲你只是從他的小提琴(已經工作)的版本。 –