SQL Pivot基于一列的Max

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQL Pivot基于一列的Max相关的知识,希望对你有一定的参考价值。

我试图将具有三个值(1,2,3)的列展开为expected1,expected2和expected3。我想根据DeployTime列中的每个值选择与最大部署时间相关的每个值(三个度量中的每一个都可以具有不同的最大部署时间,因为它们并不总是同时部署) 。

示例数据看起来像这样

CustomerNM FacilityAccountID Metric Expected DeployTime
-------------------------------------------------------
1                  1           1       1         1
1                  1           1      .5        .5
1                  1           2       2         2
1                  1           2       1         1
1                  1           3       2         2 
1                  1           3       3         3 
2                  1           1       10        15
2                  1           1       26        17
2                  1           2       25        29
2                  1           2       15        34
2                  1           3       21        7 
2                  1           3       33        2 

我有:

SELECT 
    CustomerNM, FacilityAccountID,
    MAX(DeployTime) OVER (Partition BY CustomerNM, FacilityAccountID) as DeployTime,
    [1] AS Expected1,
    [2] AS Expected2,
    [3] AS Expected3
FROM   
    (SELECT 
         CustomerNM, FacilityAccountID, metric, expected, DeployTime
     FROM 
         TABLE) p  
PIVOT  
    (SUM (expected)
     FOR metric IN ([1], [2], [3])  
    ) AS p

由于某种原因,这会返回只填充了一个新的预期列的行;当应该只有一行时(对于每个客户名称/设施帐户ID),基本上给出三行。我认为groupby可能有用,但不确定。

样本的预期输出应该是这样的

CustomerNM FacilityAccountID Expected1  Expected2  Expected3 
-----------------------------------------------------------
   1               1             1           2         3   
   2               1            26          15        21   
答案

条件聚合似乎更简单:

SELECT CustomerNM, FacilityAccountID,
       MAX(CASE WHEN metric = 1 THEN expected END) as expected1,
       MAX(CASE WHEN metric = 2 THEN expected END) as expected2,
       MAX(CASE WHEN metric = 3 THEN expected END) as expected3
FROM TABLE p
GROUP BY CustomerNM, FacilityAccountID;
另一答案

关键是在枢轴之前过滤您想要的结果:

SELECT CustomerNM, FacilityAccountID,
MAX([1]) AS Expected1, MAX([2]) AS Expected2, MAX([3]) AS Expected3
FROM   
(SELECT CustomerNM, FacilityAccountID, metric, expected, DeployTime
FROM TABLE t
WHERE DeployTime =
(SELECT MAX(DeployTime)
 FROM TABLE t2
 WHERE t.CustomerNM = t2.CustomerNM
 AND t.metric = t2.metric
 AND t.FacilityAccountID = t2.FacilityAccountID)) p  
PIVOT  
(  
SUM (expected)
FOR metric IN  
( [1], [2], [3])  
) AS p
GROUP BY CustomerNM, FacilityAccountID

至于戈登的观点,你也可以在没有支点的情况下实现这一点,而是使用一些CASE语句:

SELECT CustomerNM, FacilityAccountID,
MAX(CASE WHEN metric = 1 THEN expected END) as expected1,
MAX(CASE WHEN metric = 2 THEN expected END) as expected2,
MAX(CASE WHEN metric = 3 THEN expected END) as expected3
FROM TABLE t
WHERE DeployTime =
(SELECT MAX(DeployTime)
 FROM TABLE t t2
 WHERE t.CustomerNM = t2.CustomerNM
 AND t.metric = t2.metric
 AND t.FacilityAccountID = t2.FacilityAccountID)
GROUP BY CustomerNM, FacilityAccountID;

在这里测试:http://rextester.com/JPXNU32454

以上是关于SQL Pivot基于一列的Max的主要内容,如果未能解决你的问题,请参考以下文章

MS SQL Pivot 来自多列的长数据

SQL查询以获取与另一列的MAX值对应的列值?

如何基于pandas.groupby()。max()中一列的最大值获得整行?

PIVOT在SQL Sever里面和Oracle里面的用法区别

如何将 SQL 中的列中的值 PIVOT 到新的列名中,然后在这些 PIVOT 列下列出其他列值?

Pandas,Pivot表来自2列,其值为其中一列的计数