简体   繁体   English

SQL查询中的数据透视表

[英]Pivot Table within SQL query

I have an SQL query here, which works perfectly fine: 我在这里有一个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])

I wish to place a PIVOT table as structured HERE: 我希望将PIVOT表放置在此处:

PIVOT(sum(Qty) for [WK] in ([25],[26],[27],[28],[29],[30],[31],[32],[33],[34],[35],[36])) as pivot [[25],[26],[27],[28],[29],[30],[31],[32],[33],[34]中[WK]的PIVOT(sum(Qty) ],[35],[36]))作为枢轴

These two SQL functions are not working together and I am unsure where to place the pivot. 这两个SQL函数不能一起使用,我不确定在哪里放置数据透视表。 Could someone assist as to where I have gone wrong on this one? 有人可以帮我解决这个问题吗? The items 25 through 36 in the pivot tables are week start dates and this is what I wish to pivot on. 数据透视表中的第25到36项是星期开始日期,这是我希望讨论的内容。

I appreciate any incite. 我很感激。

I am creating a pivot table on qty's based on week function. 我正在基于周功能在数量上创建数据透视表。 Once I can do this, I have my problem solved. 一旦可以做到,就可以解决我的问题。

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

as per the documentation 根据文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM