SQL从DNN用户数据转移数据

时间:2017-05-13 09:56:52

标签: sql sql-server pivot

我正在尝试将数据从DNN数据库导出到新系统,所以我试图首先将DNN数据转换为更合理的形式......但我无法让我的代码工作。

任何人都可以看到有什么问题吗?

我得到的错误表明第1行中的2个字段是无效的列名?

SELECT [Rownumber], [columnheading]

FROM
(
  SELECT

     udd1.UserDefinedRowId as Rownumber, 
     udf1.FieldTitle as columnheading,
     convert(nvarchar(max),udd1.FieldValue) as newFieldValue 
  FROM UserDefinedData udd1

  INNER JOIN UserDefinedFields udf1
    ON udf1.UserDefinedFieldId = udd1.UserDefinedFieldId

  INNER JOIN UserDefinedRows udr1 
    ON udr1.UserDefinedRowId = udd1.UserDefinedRowId
    WHERE udr1.ModuleId = 3806

) AS UserData

PIVOT

(
  MAX(Rownumber)

  FOR columnheading IN  ( "Evidence", "Contact", "Action Required")

) AS PivotTable;

1 个答案:

答案 0 :(得分:0)

我认为您正在寻找的枢轴输出是newfieldvalue所以它必须进入max column并选择必须是列出的列如下:

SELECT [Rownumber], [Evidence], [Contact], [Action Required]
FROM
(
  SELECT

     udd1.UserDefinedRowId as Rownumber, 
     udf1.FieldTitle as columnheading,
     convert(nvarchar(max),udd1.FieldValue) as newFieldValue 
  FROM UserDefinedData udd1

  INNER JOIN UserDefinedFields udf1
    ON udf1.UserDefinedFieldId = udd1.UserDefinedFieldId

  INNER JOIN UserDefinedRows udr1 
    ON udr1.UserDefinedRowId = udd1.UserDefinedRowId
    WHERE udr1.ModuleId = 3806

) AS UserData
PIVOT
(
  MAX(newFieldValue)
  FOR columnheading IN  ( [Evidence], [Contact], [Action Required])
) AS PivotTable;