SQL中连续重复列数据的其他列值的总和

时间:2018-03-15 19:49:38

标签: sql-server

我只需要为连续列的数据添加持续时间值。假设在此表中执行数据3次,每次执行3次,2次和1次。所以我想按照状态列的连续性添加持续时间列的数据3次。 我在这里需要执行的只显示1次而不是连续3次,并且再次显示第2次执行的相同规则,显示2次,因此它还将显示1次持续时间列值的累积和。因此,每个迭代行将带有持续时间值的总和并显示为单行

CREATE TABLE Tools
    ([ID] int, [StartDatetime] varchar, [EndDatetime] varchar, [duration] int,  [durationDatetime] varchar, [state] varchar(50), [stateCode] int)
;

INSERT INTO Tools
    ([ID], [StartDatetime], [EndDatetime], [duration],  [durationDatetime], [state], [stateCode])
VALUES

(1367, '2018-03-15 10:02:52', '2018-03-15T10:19:32.7000000', 100, '1900-01-01T00:16:39.7800000', 'Execute', 6),
(1367, '2018-03-15 10:02:52', '2018-03-15T10:19:32.7000000', 900, '1900-01-01T00:16:39.7800000', 'Execute', 6),
(1367, '2018-03-15 10:02:52', '2018-03-15T10:19:32.7000000', 400, '1900-01-01T00:16:39.7800000', 'Execute', 6),
(1367, '2018-03-15 10:02:52', '2018-03-15T10:19:32.7000000', 1000, '1900-01-01T00:16:39.7800000', 'other', 2),
(1367, '2018-03-15 10:02:52', '2018-03-15T10:19:32.7000000', 50, '1900-01-01T00:16:39.7800000', 'Execute', 6),
(1367, '2018-03-15 10:02:52', '2018-03-15T10:19:32.7000000', 52, '1900-01-01T00:16:39.7800000', 'Execute', 6),
(1367, '2018-03-15 10:02:52', '2018-03-15T10:19:32.7000000', 8, '1900-01-01T00:16:39.7800000', 'other', 2),
(1367, '2018-03-15 10:02:52', '2018-03-15T10:19:32.7000000', 4, '1900-01-01T00:16:39.7800000', 'other', 2),
(1367, '2018-03-15 10:02:52', '2018-03-15T10:19:32.7000000', 740, '1900-01-01T00:16:39.7800000', 'Execute', 6)

enter image description here

2 个答案:

答案 0 :(得分:1)

您可以尝试一下,我也遇到了这种情况,并使用此代码段找到了解决方案。

  select grp, value, min(id), max(id), count(*) as cnt
    from (select t.*, (row_number() over (order by id) - row_number() over partition by value order by id)
         ) as grp
    from table t
   ) t
  group by grp, value;

答案 1 :(得分:0)

我已经添加了一个KID列(标识),因为Sean Lange已经指出您需要订单。

CREATE TABLE Tools
(
    [KID] int identity,
    [ID] int, 
    [StartDatetime] varchar(50), 
    [EndDatetime] varchar(50), 
    [duration] int,  
    [durationDatetime] varchar(50), 
    [state] varchar(50), 
    [stateCode] int
);
GO
INSERT INTO Tools ([ID], [StartDatetime], [EndDatetime], [duration],  [durationDatetime], [state], [stateCode]) VALUES
(1367, '2018-03-15 10:02:52', '2018-03-15T10:19:32.7000000', 100, '1900-01-01T00:16:39.7800000', 'Execute', 6),
(1367, '2018-03-15 10:02:52', '2018-03-15T10:19:32.7000000', 900, '1900-01-01T00:16:39.7800000', 'Execute', 6),
(1367, '2018-03-15 10:02:52', '2018-03-15T10:19:32.7000000', 400, '1900-01-01T00:16:39.7800000', 'Execute', 6),
(1367, '2018-03-15 10:02:52', '2018-03-15T10:19:32.7000000', 1000, '1900-01-01T00:16:39.7800000', 'other', 2),
(1367, '2018-03-15 10:02:52', '2018-03-15T10:19:32.7000000', 50, '1900-01-01T00:16:39.7800000', 'Execute', 6),
(1367, '2018-03-15 10:02:52', '2018-03-15T10:19:32.7000000', 52, '1900-01-01T00:16:39.7800000', 'Execute', 6),
(1367, '2018-03-15 10:02:52', '2018-03-15T10:19:32.7000000', 8, '1900-01-01T00:16:39.7800000', 'other', 2),
(1367, '2018-03-15 10:02:52', '2018-03-15T10:19:32.7000000', 4, '1900-01-01T00:16:39.7800000', 'other', 2),
(1367, '2018-03-15 10:02:52', '2018-03-15T10:19:32.7000000', 740, '1900-01-01T00:16:39.7800000', 'Execute', 6);
GO
;with r as
(
    select * 
           ,case when coalesce(lag(state) over (partition by ID order by KID), '') <> state then 1 end as reset
    from   tools
), g as
 (
     select *, sum(reset) over (order by KID) as grp
     from r
     where state = 'Execute'
 )
 select ID, StartDatetime, EndDatetime, duration, durationDatetime, state, stateCode, 
        sum(duration) over (partition by grp order by KID) CumulativeSum
 from g
GO
  ID | StartDatetime       | EndDatetime                 | duration | durationDatetime            | state   | stateCode | CumulativeSum
---: | :------------------ | :-------------------------- | -------: | :-------------------------- | :------ | --------: | ------------:
1367 | 2018-03-15 10:02:52 | 2018-03-15T10:19:32.7000000 |      100 | 1900-01-01T00:16:39.7800000 | Execute |         6 |           100
1367 | 2018-03-15 10:02:52 | 2018-03-15T10:19:32.7000000 |      900 | 1900-01-01T00:16:39.7800000 | Execute |         6 |          1000
1367 | 2018-03-15 10:02:52 | 2018-03-15T10:19:32.7000000 |      400 | 1900-01-01T00:16:39.7800000 | Execute |         6 |          1400
1367 | 2018-03-15 10:02:52 | 2018-03-15T10:19:32.7000000 |       50 | 1900-01-01T00:16:39.7800000 | Execute |         6 |            50
1367 | 2018-03-15 10:02:52 | 2018-03-15T10:19:32.7000000 |       52 | 1900-01-01T00:16:39.7800000 | Execute |         6 |           102
1367 | 2018-03-15 10:02:52 | 2018-03-15T10:19:32.7000000 |      740 | 1900-01-01T00:16:39.7800000 | Execute |         6 |           740

Warning: Null value is eliminated by an aggregate or other SET operation.

dbfiddle here