0

我有这个查询:

SELECT     
   dbo.[Values].Value, dbo.Definition.FieldName, dbo.Definition.DefinitionID
FROM         
   dbo.Records AS R 
INNER JOIN
   dbo.[Values] ON R.RecordID = dbo.[Values].RecordID 
INNER JOIN
   dbo.Definition ON dbo.[Values].DefinitionID = dbo.Definition.DefinitionID

结果是:

Value   FieldName   DefinitionID
1   test new    6
4   abc 6
0   test new    7
0   abc 7
34  test new    8
910 abc 8

等等

我试图得到这个:

test new | abc | DefinitionID
1           4     6
0           0     7
34          910   8

我试图改变这一点,但没有成功。列dbo.[Values].Value是类型nvarchar(MAX)

4

1 回答 1

0
select [test new],[abc],DefinitionID
from
(
   -- your query here
SELECT     dbo.[Values].Value, dbo.Definition.FieldName, dbo.Definition.DefinitionID 
FROM         dbo.Records AS R INNER JOIN 
                      dbo.[Values] ON R.RecordID = dbo.[Values].RecordID INNER JOIN 
                      dbo.Definition ON dbo.[Values].DefinitionID = dbo.Definition.DefinitionID 
) src
PIVOT
(MAX(Value) for FieldName in ([test new],[abc])) p
于 2012-10-05T08:57:49.923 回答