Sql server 2008 在SQL语句中使用Pivot?

Sql server 2008 在SQL语句中使用Pivot?,sql-server-2008,tsql,pivot,Sql Server 2008,Tsql,Pivot,我正在尝试为一个驱动器记录分配多个其他记录时创建其他列。在我附带的屏幕截图中,您可以看到有三个驱动器,其中所有数据都是恒定的,但每个驱动器都有多个分配给驱动器的激励 轴心功能是否可以为激励创建一个额外的列?我不是在寻找一些动态的东西,我可以在最多4个额外的列上硬编码,以便为一个驱动器分配5个奖励的空间 我希望我的结果是: 最后一篇专栏文章是我如何执行useforxmlpath来完成它的,但这并不是我想要的 以下是我的SQL语句: Select DM.DriveID [DriveID], DM.F

我正在尝试为一个驱动器记录分配多个其他记录时创建其他列。在我附带的屏幕截图中,您可以看到有三个驱动器,其中所有数据都是恒定的,但每个驱动器都有多个分配给驱动器的激励

轴心功能是否可以为激励创建一个额外的列?我不是在寻找一些动态的东西,我可以在最多4个额外的列上硬编码,以便为一个驱动器分配5个奖励的空间

我希望我的结果是:

最后一篇专栏文章是我如何执行useforxmlpath来完成它的,但这并不是我想要的

以下是我的SQL语句:

Select
DM.DriveID [DriveID],
DM.FromDateTime [FromDateTime],
Case When DM.OwnerType = 0 Then Acct.Name Else CD.DescLong End As [OwnerName],
Acct.AccountID [AcctID],
Inc.Description [Incentive],

Stuff ((Select ', ' + isnull(EM.Description,'')
    From Production.[dbo].EquipmentMaster EM
        Inner Join Production.[dbo].EquipmentDetail ED On EM.EquipmentID = ED.EquipmentID And EM.EquipmentType=3
        Where ED.DriveID = DM.DriveID
            For XML PATH(''), TYPE).value('.', 'varchar(max)')
            ,1, 2, '') as [XML_Incentives]

From
Production.[dbo].rpt_DriveMaster DM
Left Outer Join Production.[dbo].rpt_Accounts Acct on DM.AccountID=Acct.AccountID
Inner Join Production.[dbo].rpt_CenterDetail CD on DM.CenterID=CD.CenterID
Left Outer Join Production.[dbo].OBI_Incentives Inc on DM.DriveID=Inc.DriveID

Where
DM.DriveID in (658946,597611,651136)
我正在查看标记为重复时提供的链接,但对我来说几乎就像在读希腊语。我正试图修改我的SQL以以此为基础,但运气不太好:

有没有机会提供一些关于这项工作原理的指导

Select
*
From
( Select
DM.DriveID [DriveID],
DM.FromDateTime [FromDateTime],
Case When DM.OwnerType = 0 Then Acct.Name Else CD.DescLong End As [OwnerName],
Acct.AccountID [AcctID],
Inc.Description [Incentive1],
null [Incentive2],
null [Incentive3],
null [Incentive4],
null [Incentive5]

From
Production.[dbo].rpt_DriveMaster DM
Left Outer Join Production.[dbo].rpt_Accounts Acct on DM.AccountID=Acct.AccountID
Inner Join Production.[dbo].rpt_CenterDetail CD on DM.CenterID=CD.CenterID
Left Outer Join Production.[dbo].OBI_Incentives Inc on DM.DriveID=Inc.DriveID

Where
DM.DriveID in (658946,597611,651136)
) as P
Pivot (
    min(P.[Incentive])
    for P.[DriveID] in ([Incentive1], [Incentive2], [Incentive3])
) as PIV

复制什么是枢轴?本质上,它将行数据和位置放入列中。换句话说,它是一种可用于可视化数据的方法,这些数据在当前格式下可能难以理解。