gpt4 book ai didi

SQL Server 在 2 个(或多个)聚合上进行透视

转载 作者:行者123 更新时间:2023-12-05 01:18:30 24 4
gpt4 key购买 nike

是否可以在 SQL Server 中对多个聚合列进行透视?我有以下订单表:

[ORDERS]
| CustName | OrderedProduct | QtyOrdered | UnitCost | UnitPrice | OrderDate
Bob | Canned Tuna | 6 | 11 | 14 | 21-12-2016 13:11:00
Steve | Canned Salmon | 2 | 15 | 19 | 03-11-2016 11:03:00
Bob | Canned Tuna | 10 | 10 | 13 | 22-12-2016 10:43:00
Bob | Canned Tuna | 4 | 9 | 10 | 13-11-2016 17:22:00

我有以下数据透视查询,它为我提供了 11 月/12 月每个客户每个产品的订单数量:

SELECT CustName, OrderedProduct, [VOL Dec 2016], [VOL Nov 2016] FROM
(
SELECT CustName, OrderedProduct, QtyOrdered,
'VOL ' + CONVERT(CHAR(4), OrderDate, 100) + CONVERT(CHAR(4), OrderDate, 120) AS YearMonthVol
FROM [ORDERS]
) src
PIVOT
(
SUM(QtyOrdered)
FOR YearMonthVol IN ([VOL Dec 2016], [VOL Nov 2016])
) pvtvol
WHERE CustName='Bob'

这给出了预期的结果

CustName | OrderedProduct | VOL Dec 2016 | VOL Nov 2016
Bob | Canned Tuna | 16 | 4

我想要的也是以边距为中心,因此将 src 查询更改为:

    SELECT CustName, OrderedProduct, QtyOrdered,
((UnitPrice-UnitCost)*QtyOrdered) AS Margin,
'VOL ' + CONVERT(CHAR(4), OrderDate, 100) + CONVERT(CHAR(4), OrderDate, 120) AS YearMonthVol
'MAR ' + CONVERT(CHAR(4), OrderDate, 100) + CONVERT(CHAR(4), OrderDate, 120) AS YearMonthMar
FROM [ORDERS]

我尝试了以下修改后的查询添加第二个数据透视

SELECT CustName, OrderedProduct, [VOL Dec 2016], [VOL Nov 2016], [MAR Dec 2016], [MAR Nov 2016] FROM
(
SELECT CustName, OrderedProduct, QtyOrdered,
((UnitPrice-UnitCost)*QtyOrdered) AS Margin,
'VOL ' + CONVERT(CHAR(4), OrderDate, 100) + CONVERT(CHAR(4), OrderDate, 120) AS YearMonthVol
'MAR ' + CONVERT(CHAR(4), OrderDate, 100) + CONVERT(CHAR(4), OrderDate, 120) AS YearMonthMar
FROM [ORDERS]
) src
PIVOT
(
SUM(QtyOrdered)
FOR YearMonthVol IN ([VOL Dec 2016], [VOL Nov 2016])
) pvtvol
PIVOT
(
SUM(Margin)
FOR YearMonthMar IN ([MAR Dec 2016], [MAR Nov 2016])
) pvtmar
WHERE CustName='Bob'

期望看到的表格是

CustName | OrderedProduct | VOL Dec 2016 | VOL Nov 2016 | MAR Dec 2016 | MAR Nov 2016
Bob | Canned Tuna | 16 | 4 | 48 | 4

实际看到的表格是

CustName | OrderedProduct | VOL Dec 2016 | VOL Nov 2016 | MAR Dec 2016 | MAR Nov 2016
Bob | Canned Tuna | 16 | NULL | 48 | NULL
Bob | Canned Tuna | NULL | 4 | NULL | 4

所以, margin 的枢轴似乎没有应用,我得到了每个订单的一行。我试图通过我尝试使用的方法真正实现的是什么,或者我会更好地使用 VOL 和 MAR 的交叉表查询或数据透视表和交叉表的混合(VOL 的数据透视表,MAR 的交叉表? )

非常感谢任何建议。

最佳答案

您可以稍微简化您的查询

示例

Select *
From (
Select A.CustName
,A.OrderedProduct
,B.*
From ORDERS A
Cross Apply ( values ('VOL '+left(DateName(Month,OrderDate),3)+' '+DateName(Year,OrderDate), QtyOrdered )
,('MAR '+left(DateName(Month,OrderDate),3)+' '+DateName(Year,OrderDate),((UnitPrice-UnitCost)*QtyOrdered) )
) B (Item,Value)
Where CustName='Bob'
) A
Pivot (sum([Value]) For [Item] in ([VOL Dec 2016], [VOL Nov 2016],[MAR Dec 2016], [MAR Nov 2016]) ) p

返回

enter image description here

关于SQL Server 在 2 个(或多个)聚合上进行透视,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45353542/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com