SQL查询中的数据透视表

时间:2014-06-17 16:15:18

标签: sql sql-server pivot

我在这里有一个SQL查询,它完全正常:

select
R.[Source_Loaction] as VendorID
,R.[Article_Nbr]
,A.[Article_Desc]
,S.[Destination_Location] as Site
,AH.[External_Product_SType_Arth_Desc] as Product_Subtype
,datepart (week, R.[Scheduled_Ship_dae]) as WK
,SUM(S.[Major_Ship_Qty]) as UoM
,SUM(R.[Shipment_Qty]) as Qty

from [dbo].[FnR_RECEIVED_SHIPMENT_V] as R

INNER JOIN [dbo].[FnR_SOURCING_V] as S
ON (R.[Article_Nbr]=S.[Article_Nbr] AND R.[Destination_Location]=S.[Destination_Location])
INNER JOIN [dbo].[FnR_EXTERNAL_ARTICLE_V] as AH
ON R.[Article_Nbr]=AH.[Article_Nbr]
INNER JOIN [dbo].[FnR_ARTICLE_V] as A
ON R.[Article_Nbr]=A.[Article_Nbr]

Group by 
R.[Source_Loaction]
,R.[Article_Nbr]
,A.[Article_Desc]
,S.[Destination_Location]
,AH.[External_Product_SType_Arth_Desc]
,datepart (week, R.[Scheduled_Ship_dae])

我希望将PIVOT表格放在以下结构中:

PIVOT([25],[26],[27],[28],[29],[30],[31],[32],[33]中[WK]的总和(数量) ,[34],[35],[36]))作为支点

这两个SQL函数不能协同工作,我不确定在哪里放置数据透视表。有人可以协助我在这个问题上出错吗?数据透视表中的项目25到36是周开始日期,这是我想要转向的内容。

我感激任何煽动。

我正在基于周函数在qty上创建一个数据透视表。一旦我能做到这一点,我的问题就解决了。

1 个答案:

答案 0 :(得分:1)

select *
from 
(
     -- your first query here
) src
PIVOT(sum(Qty) for [WK] in ([25],[26],[27],[28],[29],[30],[31],[32],[33],[34],[35],[36])) as pivot

根据documentation