我试图从我的两个表中选择特定的列,其中一个是派生表,但出于某种原因我只能选择*
这有效:
SELECT *
FROM
(
--Pivot the Custom Fields into a derived table--------------
SELECT [Id], [CustomField], [CustomField2], [CustomField3], [CustomField4], [CustomField5]
FROM
(
SELECT [Id], [FieldValue], [FieldName]
FROM
EntityCustomFieldValues
AS t1
INNER JOIN Contact t2 ON t1.ParentId = t2.Id
)AS SourceTable
PIVOT
(
MAX([FieldValue]) FOR [FieldName] IN ([CustomField], [CustomField2], [CustomField3], [CustomField4], [CustomField5])
)AS PivotTable
-----------------------------------------------------------
) AS t
INNER JOIN Contact ON t.Id = Contact.Id
问题是,一旦我尝试选择特定列,我会收到语法错误???
SELECT [t].[Id], [Contact].[FirstName], [t].[CustomField], [t].[CustomField2],
FROM
(
--Pivot the Custom Fields---------------------------------
SELECT [Id], [CustomField], [CustomField2], [CustomField3], [CustomField4], [CustomField5]
FROM
(
SELECT [Id], [FieldValue], [FieldName]
FROM
EntityCustomFieldValues
AS t1
INNER JOIN Contact t2 ON t1.ParentId = t2.Id
)AS SourceTable
PIVOT
(
MAX([FieldValue]) FOR [FieldName] IN ([CustomField], [CustomField2], [CustomField3], [CustomField4], [CustomField5])
)AS PivotTable
----------------------------------------------------
) AS t
INNER JOIN Contact ON t.Id = Contact.Id;
第15行,第1行,第4行 关键字' FROM'。
附近的语法不正确第15行,第15行,第1行,第25行 关键字' AS'附近的语法不正确。
我试图将其包装在另一个选择中,但这也不起作用。
答案 0 :(得分:1)
试试这个:
SELECT [t].[Id]
,[Contact].[FirstName]
,[t].[CustomField]
,[t].[CustomField2]
FROM (
--Pivot the Custom Fields---------------------------------
SELECT [Id]
,[CustomField]
,[CustomField2]
,[CustomField3]
,[CustomField4]
,[CustomField5]
FROM (
SELECT [Id]
,[FieldValue]
,[FieldName]
FROM EntityCustomFieldValues AS t1
INNER JOIN Contact t2 ON t1.ParentId = t2.Id
) AS SourceTable
PIVOT(MAX([FieldValue]) FOR [FieldName] IN (
[CustomField]
,[CustomField2]
,[CustomField3]
,[CustomField4]
,[CustomField5]
)) AS PivotTable
----------------------------------------------------
) t
INNER JOIN Contact ON t.Id = Contact.Id;
答案 1 :(得分:1)
请试试这个:
SELECT [t].[Id], [Contact].[FirstName], [t].[CustomField], [t].[CustomField2]
FROM
(
--Pivot the Custom Fields---------------------------------
SELECT [Id], [CustomField], [CustomField2], [CustomField3], [CustomField4], [CustomField5]
FROM
(
SELECT [Id], [FieldValue], [FieldName]
FROM
EntityCustomFieldValues t1
INNER JOIN Contact t2 ON t1.ParentId = t2.Id
) SourceTable
PIVOT
(
MAX([FieldValue]) FOR [FieldName] IN ([CustomField], [CustomField2], [CustomField3], [CustomField4], [CustomField5])
) PivotTable
----------------------------------------------------
) t
INNER JOIN Contact ON t.Id = Contact.Id;