在SQL第2部分中以分组序列升序/降序对结果最小值/最大值进行分组

时间:2016-03-22 06:57:20

标签: sql sql-server

如果系列符合数据顺序

,我想以升序/降序序列选择Min和Max

假设我按日期顺序获得数据:

LogDate      StartValue EndValue    Multiplier  DiffValue
2016-02-08   7661.25    7677.62     6.94        16.37
2016-02-09   7677.62    7693.02     6.94        15.4
2016-02-10   7693.02    7709.82     6.94        16.8
2016-02-11   7709.82    7727.08     6.94        17.26
2016-02-12   7727.08    7740.93     6.94        13.85
2016-02-13   3.02       12.22       6.94        9.2
2016-02-14   12.22      20.73       6.94        8.51
2016-02-15   20.73      37.04       6.94        16.31
2016-02-16   37.04      52.56       7           15.52
2016-02-17   52.56      67.82       7           15.26
2016-02-18   67.82      83.66       7           15.84
2016-02-19   83.66      98.77       7           15.11
2016-02-20   98.77      108.37      7           9.61

我希望结果像:

LogDateMin  LogDateMax  StartValue  EndValue    Multiplier  SumOfDiffValue
2016-02-08  2016-02-12  7661.25     7740.93     6.94        79.68
2016-02-13  2016-02-15  3.02        37.04       6.94        34.02
2016-02-16  2016-02-20  37.04       108.37      7           71.34

这里我也用Multiplier对结果进行分组并得到deffValue的总和

我们如何实现这一目标

请帮忙

2 个答案:

答案 0 :(得分:3)

对于SQL Server 2012及更高版本,您可以使用LAG来确定更改并按此分组。这是一种方式;

WITH cte AS (
  SELECT LogDate, StartValue, EndValue, Multiplier, DiffValue,
         LAG(EndValue)   OVER (ORDER BY LogDate) OldEndValue,
         LAG(Multiplier) OVER (ORDER BY LogDate) OldMultiplier
  FROM myTable
), cte2 AS (
  SELECT LogDate, StartValue, EndValue, Multiplier, DiffValue,
  SUM(CASE WHEN OldEndValue > StartValue OR Multiplier <> OldMultiplier 
           THEN 1 ELSE 0 END) OVER (ORDER BY LogDate) grp
  FROM cte
) 
SELECT MIN(LogDate) LogDateMin, MAX(LogDate) LogDateMax, MIN(StartValue) StartValue, 
       MAX(EndValue) EndValue, MAX(Multiplier) Multiplier, SUM(DiffValue) DiffValue
FROM cte2
GROUP BY grp
ORDER BY MIN(LogDate);

第一个CTE只是将EndValueMultiplier的先前值添加到每一行。

第二个CTE对一个检测到你想要的改变的case语句进行运行总和。

主要语句按运行总和进行分组(每次更改都会增加)并计算所需的值。

答案 1 :(得分:1)

@Joachim打败了我(这个答案比我的更优雅),但无论如何我都会发布我的变体。

编辑:对评论中突出显示的错误进行了非常糟糕的修复:)

CREATE TABLE #Test (
    LogDate DATE,
    StartValue DECIMAL(6,2),
    EndValue DECIMAL(6,2),
    Multiplier DECIMAL(3,2),
    DiffValue DECIMAL(4,2)
);

INSERT INTO #Test(
    LogDate
    ,StartValue
    ,EndValue
    ,Multiplier
    ,DiffValue
)
VALUES       
    ('2016-02-08',   7661.25,    7677.62,     6.94,        16.37),
    ('2016-02-09',   7677.62,    7693.02,     6.94,        15.4),
    ('2016-02-10',   7693.02,    7709.82,     6.94,        16.8),
    ('2016-02-11',   7709.82,    7727.08,     6.94,        17.26),
    ('2016-02-12',   7727.08,    7740.93,     6.94,        13.85),
    ('2016-02-13',   3.02,       12.22,       6.94,         9.2),
    ('2016-02-14',   12.22,      20.73,       6.94,         8.51),
    ('2016-02-15',   20.73,      37.04,       6.94,        16.31),
    ('2016-02-16',   37.04,      52.56,       7,           15.52),
    ('2016-02-17',   52.56,      67.82,       7,           15.26),
    ('2016-02-18',   67.82,      83.66,       7,           15.84),
    ('2016-02-19',   83.66,      98.77,       7,           15.11),
    ('2016-02-20',   98.77,      108.37,      7,           9.61),
    --Extra data
    ('2016-02-21',   120,        150,         6.94,       30),
    ('2016-02-22',   150,        180,         6.94,       30),
    ('2016-02-24',   150,        180,         7,          30),
    ('2016-02-25',   180,        200,         7,          30);


WITH A AS(
    SELECT *,
        CASE WHEN 
          StartValue < LAG(StartValue) OVER (PARTITION BY Multiplier ORDER BY LogDate) 
          OR DATEADD(DAY, -1, LogDate) > LAG(LogDate) OVER (PARTITION BY Multiplier ORDER BY LogDate)
         THEN 1 ELSE 0 END AS grp
    FROM #Test
)
,B AS(
    SELECT *, 
    SUM(grp) OVER (PARTITION BY A.Multiplier ORDER BY A.LogDate ROWS UNBOUNDED PRECEDING) SUM FROM A
)
,C AS (
    SELECT *,
    DENSE_RANK() OVER (ORDER BY (CONVERT(VARCHAR(8), Multiplier)+' '+CONVERT(VARCHAR(1),sum))) rnk
    FROM B
)
SELECT MIN(LogDate) LogDateMin
    ,MAX(LogDate) LogDateMax
    ,MIN(StartValue) StartValue
    ,MAX(EndValue) EndValue
    ,MIN(Multiplier) Multiplier
    ,SUM(DiffValue) SumOfDiffValue
 FROM C GROUP BY Rnk


LogDateMin LogDateMax StartValue                              EndValue                                Multiplier                              SumOfDiffValue
---------- ---------- --------------------------------------- --------------------------------------- --------------------------------------- ---------------------------------------
2016-02-08 2016-02-12 7661.25                                 7740.93                                 6.94                                    79.68
2016-02-13 2016-02-15 3.02                                    37.04                                   6.94                                    34.02
2016-02-21 2016-02-22 120.00                                  180.00                                  6.94                                    60.00
2016-02-16 2016-02-20 37.04                                   108.37                                  7.00                                    71.34
2016-02-24 2016-02-25 150.00                                  200.00                                  7.00                                    60.00