SQL Pivot返回多行的文本列

时间:2011-11-29 19:31:24

标签: sql sql-server-2008

我已经浏览过一系列关于SO的例子和其他问题,但我仍然遇到这个问题的问题。

select [Address],[Address 1],[Address 2],[Address2],[Birth Day],[City],
       [Email],[First Name],[Last Name],[Phone],[State],[Zip]
from 
(SELECT *, row_number() over(PARTITION by formid order by formid) as rnk   
 FROM [Sitecore_WebForms].[dbo].[Field] 
 where FormId='C9681226-5951-403D-80A0-00019218785C'
) as SourceTable
pivot
(
max(value)
for [fieldname] in 
([Address],[Address 1],[Address 2],[Address2],[Birth Day],[City],[Email],
 [First Name],[Last Name],[Phone],[State],[Zip])
) as pt

结果返回9行,每列1行。如何将这些行组合成一行?

Address Address 1   Address 2   Address2    Birth Day   City    Email   First Name  Last Name   Phone   State   Zip
NULL    NULL    NULL    NULL    NULL    NULL    NULL    FIRS    NULL    NULL    NULL    NULL
NULL    NULL    NULL    NULL    NULL    CITY    NULL    NULL    NULL    NULL    NULL    NULL
NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    LAST    NULL    NULL    NULL
NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    ZIP
ADDR    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL
NULL    NULL    NULL    NULL    DATE    NULL    NULL    NULL    NULL    NULL    NULL    NULL
NULL    NULL    NULL    NULL    NULL    NULL    EMAI    NULL    NULL    NULL    NULL    NULL
NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    PHON    NULL    NULL
NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL

源行

Id  FieldId FormId  Value   Data    FieldName
CE160D15-EBCC-45B6-AB67-0CCA4AA081C7    09C0317E-E235-469D-AE7E-6CB9C45916EA    C9681226-5951-403D-80A0-00019218785C    FIRS        First Name
393E6220-8CC8-4B4E-A75B-29E1801A9A65    2ED6CAC8-7210-4BBC-9497-3B0E8A5F7CF7    C9681226-5951-403D-80A0-00019218785C    LAST        Last Name
64F9A694-406E-4385-932E-2D0D68A17FB7    FA5C1C21-CCFC-4E96-A1D8-57AC7B1AEF2C    C9681226-5951-403D-80A0-00019218785C    ADDR        Address
960A2BB5-0B50-4640-8EC8-170677809E64    CA9184AE-99CD-46C0-A72D-47FBD4F768AB    C9681226-5951-403D-80A0-00019218785C    CITY        City
CD954912-B24A-4726-9729-2ACD6DDD00C9    E695546B-2BBA-4AFF-9DAF-1B5666E42C74    C9681226-5951-403D-80A0-00019218785C    ZIP         Zip
FD19C907-F779-4CF8-90B7-4E072CDE36BE    DF056320-27BD-4E15-BF73-AC0C4A1CF543    C9681226-5951-403D-80A0-00019218785C    DATE        Birth Day
497AC034-6686-4023-ADE9-CB825F65DBFE    06807268-1689-4DDF-BDE2-08FBA31E56AE    C9681226-5951-403D-80A0-00019218785C    PHON        Phone
8A438C22-9C8E-4759-998F-542917408E38    D9402E5E-09CE-40E6-86F7-D80D99417208    C9681226-5951-403D-80A0-00019218785C    EMAI        Email

2 个答案:

答案 0 :(得分:4)

如果我们只从Source中选择value和filedName,它将按预期工作。

select [Address],[Address 1],[Address 2],[Address2],[Birth Day],[City],
       [Email],[First Name],[Last Name],[Phone],[State],[Zip]
from 
(SELECT value, fieldname   
 FROM [Sitecore_WebForms].[dbo].[Field] 
 where FormId='C9681226-5951-403D-80A0-00019218785C'
) as SourceTable
pivot
(
max(value)
for [fieldname] in 
([Address],[Address 1],[Address 2],[Address2],[Birth Day],[City],[Email],
 [First Name],[Last Name],[Phone],[State],[Zip])
) as pt

所有表格的更新:

DECLARE @form TABLE 
( 
    id INT, 
    formid INT,
    fieldName VARCHAR(32),
    fieldValue VARCHAR(32)
)

insert into @form values(1,1,'First Name','FRST')
insert into @form values(2,1,'Last Name','LST')
insert into @form values(3,1,'Address','ADDR')
insert into @form values(4,2,'First Name','FRST2')
insert into @form values(5,2,'Last Name','LST2')
insert into @form values(6,2,'Address','ADDR2')



select FormId,[Address],[Address 1],[Address 2],[Address2],[Birth Day],[City],
       [Email],[First Name],[Last Name],[Phone],[State],[Zip]
from 
(SELECT FormId,fieldName,fieldValue
 FROM @form 
 --where FormId=1
) as SourceTable
pivot
(
max(fieldValue)
for [fieldname] in 
([Address],[Address 1],[Address 2],[Address2],[Birth Day],[City],[Email],
 [First Name],[Last Name],[Phone],[State],[Zip])
) as pt

答案 1 :(得分:1)

PIVOT只会“轮换”您列出的列;任何其他列仍将隐藏自己的行。在您的情况下,IdFieldId都会导致每行保留。

要解决此问题,请在子查询中排除这些列:

select  *
from    (
        select  FormId
        ,       Value
        ,       FieldName 
        from    @t 
        ) as SourceTable        
pivot   (
        max(value) 
        for FieldName in 
        (Address,[Address 1],[Address 2],Address2,...)
        ) as PivotAlias;

Working example at SE DATA.