SQL Pivot基于一列的Max

问题描述 投票:0回答:2

我试图将具有三个值(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   
sql pivot max
2个回答
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;

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

热门问题
推荐问题
最新问题