У меня есть такая таблица:
+--------+--------+--------+------------+
| 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]), Maximum Id и group по 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», но он не работал