需要 SQL MIN MAX Group By 和 AGGREGATE 的帮助

Need Help for SQL MIN MAX Group By and AGGREGATE

我有一个这样的 table :

+--------+--------+--------+------------+
| HOLEID |  FROM  |   TO   | ALTERATION |
+--------+--------+--------+------------+
| HOLEV2 | 0      | 132.6  | AA-LT-1    |
| HOLEV2 | 132.6  | 171.28 | ARG-1-MSI  |
| HOLEV2 | 171.28 | 177.65 | AA-LT-1    |
| HOLEV2 | 177.65 | 178.8  | AA-LT-1    |
| HOLEV2 | 178.8  | 213.9  | AA-LT-1    |
| HOLEV2 | 213.9  | 214.8  | AA-LT-1    |
| HOLEV2 | 214.8  | 216.8  | AA-LT-1    |
| HOLEV2 | 216.8  | 219.4  | ARG-2-Kaol |
| HOLEV2 | 219.4  | 219.9  | ARG-2-Kaol |
| HOLEV2 | 219.9  | 220.3  | ARG-2-Kaol |
| HOLEV2 | 220.3  | 220.8  | ARG-2-Kaol |
| HOLEV2 | 220.8  | 232.6  | ARG-2-Kaol |
| HOLEV2 | 232.6  | 249.45 | ARG-1-MSI  |
| HOLEV2 | 249.45 | 256.9  | ARG-1-MSI  |
| HOLEV2 | 256.9  | 265.7  | ARG-2-Kaol |
| HOLEV2 | 265.7  | 290.1  | ARG-1-MSI  |
| HOLEV2 | 290.1  | 294.85 | ARG-2-Kaol |
| HOLEV2 | 294.85 | 308.4  | ARG-1-MSI  |
| HOLEV2 | 308.4  | 310.35 | ARG-1-MSI  |
| HOLEV2 | 310.35 | 325    | ARG-1-MSI  |
| HOLEV2 | 325    | 331.12 | ARG-2-Kaol |
| HOLEV2 | 331.12 | 332.1  | ARG-2-Kaol |
| HOLEV2 | 332.1  | 333.1  | ARG-1-MSI  |
| HOLEV2 | 333.1  | 333.6  | ARG-1-MSI  |
| HOLEV2 | 333.6  | 353.6  | ARG-1-MSI  |
| HOLEV2 | 353.6  | 368.55 | ARG-1-MSI  |
| HOLEV2 | 368.55 | 376.6  | ARG-1-MSI  |
| HOLEV2 | 376.6  | 382.7  | ARG-1-MSI  |
| HOLEV2 | 382.7  | 396.1  | ARG-2-Kaol |
| HOLEV2 | 396.1  | 416.2  | ARG-1-MSI  |
+--------+--------+--------+------------+

除了最小值(FROM)、最大值([TO])、最大 Id 和 ALTERATION 代码组之外,我正在尝试按 ALTERATION 值进行分组。

这是我的查询

SELECT MAX(Id) as Id,[HOLEID] as HOLEID,MIN([FROM]) AS [FROM],MAX([TO]) AS [TO],[ALTERATION] FROM (
SELECT  ROW_NUMBER() OVER(ORDER BY [FROM] ASC) AS Id,
[HOLEID]
      ,[FROM]
      ,[TO]
      ,[ALTERATION]
  FROM [dbo].[ALT]
  where [FROM] < 400
  )Z
  GROUP BY [ALTERATION],[HOLEID]

但是返回值不是我想要的:

+----+--------+-------+-----------+------------+
| Id | HOLEID | FROM  |    TO     | ALTERATION |
+----+--------+-------+-----------+------------+
|  7 | HOLEV2 | 0     |     216.8 | AA-LT-1    |
| 30 | HOLEV2 | 132.6 | 416.2     | ARG-1-MSI  |
| 29 | HOLEV2 | 216.8 | 396.1     | ARG-2-Kaol |
+----+--------+-------+-----------+------------+

我想成为这样的人:

+-------+---------+-----------+---------------+
| maxId | minFROM |   maxTO   |  ALTERATION   |
+-------+---------+-----------+---------------+
|     1 |    0    |  132.6    | AA-LT-1       |
|     2 | 132.6   | 171.28    | ARG-1-MSI     |
|     7 | 171.28  | 216.8     | AA-LT-1       |
|    12 | 216.8   | 232.6     | ARG-2-Kaol    |
|    14 | 232.6   | 256.9     | ARG-1-MSI     |
|    15 | 256.9   | 265.7     | ARG-2-Kaol    |
|    16 | 265.7   | 290.1     | ARG-1-MSI     |
|    17 | 290.1   | 294.85    | ARG-2-Kaol    |
|    20 | 294.85  | 325       |     ARG-1-MSI |
|    22 | 325     |     332.1 | ARG-2-Kaol    |
|    28 | 332.1   | 382.7     | ARG-1-MSI     |
|    29 | 382.7   | 396.1     | ARG-2-Kaol    |
|    30 | 396.1   | 416.2     | ARG-1-MSI     |
+-------+---------+-----------+---------------+

我也用过"SELECT DISTINCT"但是没用

这是一个间隙和孤岛问题,您希望将具有相同 holeidalteration 的 "adjacent" 行组合在一起。

这是使用 window 函数的方法:行号之间的差异可用于定义组。

select
    max(id) max_id,
    min([from]) min_from,
    max([to]) max_to,
    alteration
from (
    select 
        a.*,
        row_number() over(partition by holeid order by [from]) rn1,
        row_number() over(partition by holeid, alteration order by [from]) rn2
    from dbo.alt a
) t
group by holeid, alteration, rn1 - rn2
order by min_from

Demo on DB Fiddle:

min_from | max_to | alteration
:------- | :----- | :---------
0.00     | 132.60 | AA-LT-1   
132.60   | 171.28 | ARG-1-MSI 
171.28   | 216.80 | AA-LT-1   
216.80   | 232.60 | ARG-2-Kaol
232.60   | 256.90 | ARG-1-MSI 
256.90   | 265.70 | ARG-2-Kaol
265.70   | 290.10 | ARG-1-MSI 
290.10   | 294.85 | ARG-2-Kaol
294.85   | 325.00 | ARG-1-MSI 
325.00   | 332.10 | ARG-2-Kaol
332.10   | 382.70 | ARG-1-MSI 
382.70   | 396.10 | ARG-2-Kaol
396.10   | 416.20 | ARG-1-MSI 

注意:您的样本数据没有列 id,所以这不会出现在上面的结果中。