在某些值之间进行分组

时间:2016-10-04 11:42:53

标签: sql-server tsql

我有以下数据:

Event       Event Start DateTime    Event End DateTime  edd     ed      Pr Avg
700073A     9-9-16 10:44            9-9-16 10:49        NULL    small   25
700073A     9-9-16 10:50            9-9-16 10:51        -1      small   22.5
201015A     9-2-16 18:20            9-2-16 18:22        NULL    small   38.73913043
201015A     9-2-16 18:33            9-2-16 18:36        -11     small   30.63150651
401002A     4-2-16 19:46            4-2-16 20:14        -23     big     6.07929595
401002A     4-2-16 20:58            4-2-16 21:07        -44     big     6.05086946
401002A     4-4-16 12:51            4-4-16 13:58        -2384   small   6.07990537
401002A     4-4-16 14:04            4-4-16 14:29        -6      small   6.09301376
401002A     4-4-16 14:38            4-4-16 15:09        -9      small   6.10506467
401002A     4-4-16 18:07            4-4-16 18:30        -178    big     6.08507328
401002A     4-5-16 17:39            4-5-16 18:17        -436    big     6.10344077
401002A     4-5-16 23:03            4-5-16 23:33        -286    small   6.06890278
401002A     4-5-16 23:34            4-5-16 23:43        -1      small   6.05866385

我想获得以下结果,请参见截图,

因此我需要按事件分组并按“事件开始日期时间”进行排序并获得最小“事件开始日期时间”和最大“事件结束日期时间”,其中ed ='small',如果是ed,它应该分组“大”与“大”数据之间的“小”保持不变的方式。

1 个答案:

答案 0 :(得分:0)

这将根据样本数据返回所需的输出:

declare @t table (Event char(7),  EventStartDateTime datetime,   EventEndDateTime datetime, edd int, ed char(5), PrAvg decimal(15,8))
insert @t values
('700073A', '9-9-16 10:44',    '9-9-16 10:49',    NULL, 'small',  25),
('700073A', '9-9-16 10:50',    '9-9-16 10:51',    -1,   'small',  22.5),
('201015A', '9-2-16 18:20',    '9-2-16 18:22',    NULL, 'small',  38.73913043),
('201015A', '9-2-16 18:33',    '9-2-16 18:36',    -11,  'small',  30.63150651),
('401002A', '4-2-16 19:46',    '4-2-16 20:14',    -23,  'big',  6.07929595),
('401002A', '4-2-16 20:58',    '4-2-16 21:07',    -44,  'big',  6.05086946),
('401002A', '4-4-16 12:51',    '4-4-16 13:58',    -2384,'small',  6.07990537),
('401002A', '4-4-16 14:04',    '4-4-16 14:29',    -6,   'small',  6.09301376),
('401002A', '4-4-16 14:38',    '4-4-16 15:09',    -9,   'small',  6.10506467),
('401002A', '4-4-16 18:07',    '4-4-16 18:30',    -178, 'big',  6.08507328),
('401002A', '4-5-16 17:39',    '4-5-16 18:17',    -436, 'big',  6.10344077),
('401002A', '4-5-16 23:03',    '4-5-16 23:33',    -286, 'small',   6.06890278),
('401002A', '4-5-16 23:34',    '4-5-16 23:43',    -1,   'small',   6.05866385)


;with x as (
select Event, EventStartDateTime, EventEndDateTime, PrAvg, ed,
lag(ed) over(partition by event order by EventStartDateTime) lag_ed,
case when lag(ed) over(partition by event order by EventStartDateTime) = ed then 0 else 1 end g_ed
from @t
),
y as (
select *, sum(g_ed) over(order by EventStartDateTime) g
from x
where ed = 'small'
)
select Event, min(EventStartDateTime), max(EventEndDateTime), avg(PrAvg)
from y
group by event, g

union all

select Event, EventStartDateTime, EventEndDateTime, PrAvg
from @t
where ed = 'big'

希望这有帮助。