SQL Pivot基于一列的Max

时间:2018-01-02 16:23:12

标签: 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   

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

条件聚合似乎更简单:

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;