SQL MIN MAX组的需要帮助,并进行汇总

问题描述 投票:0回答:1

我有这样一张桌子:

+--------+--------+--------+------------+
| 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  |
+--------+--------+--------+------------+

我正在尝试按ALTERATION值分组,在Minimum(FROM),maximum([TO]),最大Id旁边并按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”,但没有用

sql sql-server tsql sql-server-2008 gaps-and-islands
1个回答
0
投票

这是一个空白问题,您希望将holeidalteration相同的“相邻”行分组在一起。

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

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 |改造:------- | :----- | :---------0.00 | 132.60 | AA-LT-1132.60 | 171.28 | ARG-1-MSI171.28 | 216.80 | AA-LT-1216.80 | 232.60 | ARG-2-Kaol232.60 | 256.90 | ARG-1-MSI256.90 | 265.70 | ARG-2-Kaol265.70 | 290.10 | ARG-1-MSI290.10 | 294.85 | ARG-2-Kaol294.85 | 325.00 | ARG-1-MSI325.00 | 332.10 | ARG-2-Kaol332.10 | 382.70 | ARG-1-MSI382.70 | 396.10 | ARG-2-Kaol396.10 | 416.20 | ARG-1-MSI

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

© www.soinside.com 2019 - 2024. All rights reserved.