SQL 服务器多分组

SQL Server Multiple Groupings

我被一个看似简单的问题难住了。 我们有以下 Table.

ID---   ---Income---       ---Years Offset---       ---Income By Offset---
 1          1000                 1                         NULL
 2           500                 1                         NULL
 3           400                 1                         NULL
 4            0                  1                         NULL
 5          2000                 2                         NULL
 6            0                  2                         NULL              
 7           400                 2                         NULL

我想弄清楚如何做的是通过 "Years Offset column" 对所有收入列求和并放在 "Income by Offset column." 的第一行如果Income by Offset 列的第 1 行值为 1900,第 5 行值为 2400,其余行保持不变。

我知道这听起来很简单。但是我已经尝试了 Window 函数、Row_number()、SELF 连接表,并且每个都解决了其中的一部分,但是我无法将它们放在一起。

提前致谢, 乔治

这应该return所需的结果集:

  SELECT ID, Income, [Years Offset],
         CASE WHEN ROW_NUMBER() OVER (PARTITION By [Years Offset] 
                                      ORDER BY ID) = 1
            THEN SUM(Income) OVER (PARTITION BY [Years Offset])
              ELSE NULL
         END AS [Income By Offset]
    FROM mytable

SUM 的窗口版本根据 [Years Offset] 计算 IncomeROW_NUMBER() 用于 return 此值仅用于每个 [Years Offset] 组的第一行。

Demo here

怎么样:

SQL Fiddle

MS SQL Server 2014 架构设置:

CREATE TABLE Income
(
  ID INT PRIMARY KEY,
  Income INT NOT NULL,
  YearsOffset Int NOT NULL,
  IncomeByOffset INT NULL
 )

 INSERT INTO Income (ID, Income, YearsOffset)
 VALUES (1,1000,1),
        (2,500,1),
        (3,400,1),
        (4,0,1),
        (5, 2000, 2),
        (6,0,2),
        (7,400,2)

查询 1:

UPDATE Income
  SET IncomeByOffset = I.IncomeByOffset
From 
(
  SELECT YearsOffset, SUM(Income) As IncomeByOffset, Min(ID) As MinId
  FROM Income
  GROUP BY YearsOffset
 ) I
 WHERE Income.YearsOffset = I.YearsOffset
   AND Income.Id = I.MinId

Results: 查询 2:

SELECT *
FROM Income;

Results:

| ID | Income | YearsOffset | IncomeByOffset |
|----|--------|-------------|----------------|
|  1 |   1000 |           1 |           1900 |
|  2 |    500 |           1 |         (null) |
|  3 |    400 |           1 |         (null) |
|  4 |      0 |           1 |         (null) |
|  5 |   2000 |           2 |           2400 |
|  6 |      0 |           2 |         (null) |
|  7 |    400 |           2 |         (null) |

你的我的版本Table

DECLARE @yourTable TABLE (ID INT,Income INT,[Years Offset] INT,[Income By Offset] INT NULL);

INSERT INTO @yourTable
VALUES  (1,1000,1,NULL),
        (2,500,1,NULL),
        (3,400,1,NULL),
        (4,0,1,NULL),
        (5,2000,2,NULL),
        (6,0,2,NULL),
        (7,400,2,NULL);

实际查询

SELECT  ID,
        Income,
        [Years Offset],
        CASE
            WHEN ID = MIN(ID) OVER (PARTITION BY [Years Offset])
                THEN SUM(Income) OVER (PARTITION BY [Years Offset])
            ELSE [Income By Offset]
        END AS [Income By Offset]
FROM @yourTable

结果

ID          Income      Years Offset Income By Offset
----------- ----------- ------------ ----------------
1           1000        1            1900
2           500         1            NULL
3           400         1            NULL
4           0           1            NULL
5           2000        2            2400
6           0           2            NULL
7           400         2            NULL