SQL Server:GROUP BY多个相似/匹配行的段

时间:2016-12-02 10:12:39

标签: sql sql-server

我需要能够分段进行group by。因此,在下面的示例中,有4个组代表了一段旅程。

SELECT id, mode, distance 
FROM TravelLog;

输出:

-----------------------------------------------------------------------------
    id   mode       distance
-----------------------------------------------------------------------------
    1    on_foot    0.1
    2    on_foot    0.2
    3    on_foot    0.4
    4    on_foot    0.6
    5    on_foot    0.7
    6    on_foot    0.8
    7    in_vehicle 0.9
    8    in_vehicle 2.0
    9    in_vehicle 2.5
    10   in_vehicle 3.0
    11   in_vehicle 3.5
    12   in_vehicle 4.0
    13   in_vehicle 4.5
    14   on_foot    4.6
    15   on_foot    4.7
    16   on_foot    4.8
    17   on_foot    4.9
    18   in_vehicle 5.5
    19   in_vehicle 6.0
    20   in_vehicle 6.5
-----------------------------------------------------------------------------

期望的结果:

-----------------------------------------------------------------------------
    start_id   end_id   start_distance   end_distance    mode
-----------------------------------------------------------------------------
    1          6        0.1              0.8             on_foot
    7          13       0.9              4.5             in_vehicle
    14         17       4.6              4.9             on_foot
    18         20       5.5              6.5             in_vehicle
-----------------------------------------------------------------------------

1 个答案:

答案 0 :(得分:2)

您需要识别相邻“模式”的组。一种方法使用行号的差异:

select min(id) as start_id, max(id) as end_id,
       min(distance) as start_distance, max(distance) as end_distance,
       mode
from (select t.*,
             row_number() over (order by id) as seqnum,
             row_number() over (partition by mode order by id) as seqnum_m
      from t
     ) t
group by mode, (seqnum - seqnum_m);

了解行数差异的原因有点棘手。如果你运行子查询并盯着结果,你可能会发现差异识别组的原因。