我很想知道我是否可以在sql server中的同一列上执行多个PIVOT,如下所示:
WITH T(ID, NAME, MSNAME, PLANED, ACTUAL)
AS (
SELECT 1, '45rpm', 'Raised to Supplier', '2014-12-17', '2015-12-17' UNION ALL
SELECT 1, '45rpm', 'Base Test Date', '2014-12-18', '2015-12-18' UNION ALL
SELECT 1, '45rpm', 'Washing Approval', '2014-12-19', '2015-12-19'
)
SELECT ID, NAME
, MAX(CASE WHEN MSNAME LIKE 'Raised to Supplier' THEN PLANED END) AS 'Raised to Supplier (PLANED)'
, MAX(CASE WHEN MSNAME LIKE 'Base Test Date' THEN PLANED END) AS 'Base Test Date (PLANED)'
, MAX(CASE WHEN MSNAME LIKE 'Washing Approval' THEN PLANED END) AS 'Washing Approval (PLANED)'
, MAX(CASE WHEN MSNAME LIKE 'Raised to Supplier' THEN ACTUAL END) AS 'Raised to Supplier (ACTUAL)'
, MAX(CASE WHEN MSNAME LIKE 'Base Test Date' THEN ACTUAL END) AS 'Base Test Date (ACTUAL)'
, MAX(CASE WHEN MSNAME LIKE 'Washing Approval' THEN ACTUAL END) AS 'Washing Approval (ACTUAL)'
FROM T
GROUP BY ID, NAME
对于列PLANED
,它运行良好但我无法添加第二列ACTUAL
(如上例中所示↑)
WITH T(ID, NAME, MSNAME, PLANED, ACTUAL)
AS (
SELECT 1, '45rpm', 'Raised to Supplier', '2014-12-17', '2015-12-17' UNION ALL
SELECT 1, '45rpm', 'Base Test Date', '2014-12-18', '2015-12-18' UNION ALL
SELECT 1, '45rpm', 'Washing Approval', '2014-12-19', '2015-12-19'
)
SELECT ID, NAME
, MAX([Raised to Supplier]) AS 'Raised to Supplier (PLANED)'
, MAX([Base Test Date]) AS 'Base Test Date (PLANED)'
, MAX([Washing Approval]) AS 'Washing Approval (PLANED)' FROM T
PIVOT
(
max(PLANED)
FOR MSNAME IN ([Raised to Supplier],[Base Test Date],[Washing Approval])
) AS p1
GROUP BY ID, NAME
修改
PIVOT关键字有一些限制,其中一个已经在这里讨论了,即SQL服务器不支持PIVOT中的多个聚合 - 由Giorgos评论[类似于PIVOT(min(),max(),sum()) ...)]
有3种替代方案可以应对这种情况:
在了解之后我认为这个问题的标题应该是:单个PIVOT()中的多个聚合而不是现有的
答案 0 :(得分:5)
SQL Server不允许在多个列上进行数据透视,我实际上建议首先查看对Planed
和Actual
列进行非透视,然后将数据转换为最终结果你要的那个。
您没有指定正在使用的SQL Server版本,但您可以使用UNPIVOT函数,也可以使用CROSS APPLY将多列转换为多行。基本语法为:
select
id,
name,
new_col = msname + '_' + col,
value
from t
cross apply
(
select 'planed', planed union all
select 'actual', actual
) c (col, value);
见SQL Fiddle with Demo。这会将您的数据转换为以下格式:
| ID | NAME | NEW_COL | VALUE |
|----|-------|---------------------------|------------|
| 1 | 45rpm | Raised to Supplier_planed | 2014-12-17 |
| 1 | 45rpm | Raised to Supplier_actual | 2015-12-17 |
| 1 | 45rpm | Base Test Date_planed | 2014-12-18 |
| 1 | 45rpm | Base Test Date_actual | 2015-12-18 |
| 1 | 45rpm | Washing Approval_planed | 2014-12-19 |
| 1 | 45rpm | Washing Approval_actual | 2015-12-19 |
现在,您拥有所需的新列名称以及单个列中的值,而不是多个列。现在,您可以将数据转换为最终结果:
select id, name,
[Raised to Supplier_planed], [Raised to Supplier_actual],
[Base Test Date_planed], [Base Test Date_actual],
[Washing Approval_planed], [Washing Approval_actual]
from
(
select
id,
name,
new_col = msname + '_' + col,
value
from t
cross apply
(
select 'planed', planed union all
select 'actual', actual
) c (col, value)
) d
pivot
(
max(value)
for new_col in ([Raised to Supplier_planed], [Raised to Supplier_actual],
[Base Test Date_planed], [Base Test Date_actual],
[Washing Approval_planed], [Washing Approval_actual])
) p;
见SQL Fiddle with Demo。这给出了相同的最终结果:
| ID | NAME | RAISED TO SUPPLIER_PLANED | RAISED TO SUPPLIER_ACTUAL | BASE TEST DATE_PLANED | BASE TEST DATE_ACTUAL | WASHING APPROVAL_PLANED | WASHING APPROVAL_ACTUAL |
|----|-------|---------------------------|---------------------------|-----------------------|-----------------------|-------------------------|-------------------------|
| 1 | 45rpm | 2014-12-17 | 2015-12-17 | 2014-12-18 | 2015-12-18 | 2014-12-19 | 2015-12-19 |
答案 1 :(得分:4)
要转移multiple column
,您需要使用Muliple Pivot's
而不是multiple Aggregates
。试试这个。
SELECT ID,
NAME,
Max([Raised to Supplier(PLANED)])[Raised to Supplier(PLANED)],
Max([Base Test Date(PLANED)])[Base Test Date(PLANED)],
Max([Washing Approval(PLANED)])[Washing Approval(PLANED)],
Max([Raised to Supplier(ACTUAL)])[Raised to Supplier(ACTUAL)],
Max([Base Test Date(ACTUAL)])[Base Test Date(ACTUAL)],
Max([Washing Approval(ACTUAL)])[Washing Approval(ACTUAL)]
FROM (SELECT 1 ID,'45rpm' NAME,'Raised to Supplier' + '(PLANED)' MSNAME_pl,'Raised to Supplier' + '(ACTUAL)' MSNAME_ac,'2014-12-17' PLANED,'2015-12-17' ACTUAL
UNION ALL
SELECT 1,'45rpm','Base Test Date' + '(PLANED)','Base Test Date' + '(ACTUAL)','2014-12-18','2015-12-18'
UNION ALL
SELECT 1,'45rpm','Washing Approval' + '(PLANED)','Washing Approval' + '(ACTUAL)','2014-12-19','2015-12-19') a
PIVOT ( Max(PLANED)
FOR MSNAME_pl IN ([Raised to Supplier(PLANED)],
[Base Test Date(PLANED)],
[Washing Approval(PLANED)]) ) AS p1
PIVOT ( MAX(ACTUAL)
FOR MSNAME_ac IN ([Raised to Supplier(ACTUAL)],
[Base Test Date(ACTUAL)],
[Washing Approval(ACTUAL)])) p2
GROUP BY ID, NAME