У меня есть следующий код, который я уже доволен результатами
SELECT AirlineStatementID, SegmentFrom, SegmentTo, Class, TravelDate, AirlineCode
FROM
(SELECT AirlineStatementID
,[SegmentFrom1]
,[SegmentFrom2]
,[SegmentFrom3]
,[SegmentFrom4]
,[SegmentTo1]
,[SegmentTo2]
,[SegmentTo3]
,[SegmentTo4]
,[Class1]
,[Class2]
,[Class3]
,[Class4]
,[TravelDate1]
,[TravelDate2]
,[TravelDate3]
,[TravelDate4]
,[AirlineCode1]
,[AirlineCode2]
,[AirlineCode3]
,[AirlineCode4]
FROM AirlineStatements) p
UNPIVOT
(SegmentFrom FOR V IN (SegmentFrom1, SegmentFrom2, SegmentFrom3, SegmentFrom4)) as A
UNPIVOT
(SegmentTo FOR W IN (SegmentTo1, SegmentTo2, SegmentTo3, SegmentTo4)) as B
UNPIVOT
(Class FOR X IN (Class1, Class2, Class3, Class4)) as C
UNPIVOT
(TravelDate FOR Y IN (TravelDate1, TravelDate2, TravelDate3, TravelDate4)) as D
UNPIVOT
(AirlineCode FOR Z IN (AirlineCode1, AirlineCode2, AirlineCode3, AirlineCode4)) as E
Результаты в базе данных будут выглядеть так
AirlineStatementID | SegmentFrom | SegmentTo | Class | TravelDate | AirlineCode
1063 | WLG | CHC | B | 2010-09-01 00:00:00.000 | NZ
1063 | WLG | CHC | B | 1800-01-01 00:00:00.000 | NZ
1063 | WLG | CHC | B | 1800-01-01 00:00:00.000 | NZ
1063 | WLG | CHC | B | 1800-01-01 00:00:00.000 | NZ
1064 | WLG | CHC | M | 2010-09-15 00:00:00.000 | NZ
1064 | WLG | CHC | M | 1800-01-01 00:00:00.000 | NZ
1064 | WLG | CHC | M | 1800-01-01 00:00:00.000 | NZ
1064 | WLG | CHC | M | 1800-01-01 00:00:00.000 | NZ
1065 | WLG | CHC | Y | 2010-08-25 00:00:00.000 | NZ
1065 | WLG | CHC | Y | 1800-01-01 00:00:00.000 | NZ
1065 | WLG | CHC | Y | 1800-01-01 00:00:00.000 | NZ
1065 | WLG | CHC | Y | 1800-01-01 00:00:00.000 | NZ
Мне нужен номер строки в столбце, чтобы он отображался следующим образом
AirlineStatementID | SegmentFrom | SegmentTo | Class | TravelDate | AirlineCode | Sequence
1063 | WLG | CHC | B | 2010-09-01 00:00:00.000 | NZ | 1
1063 | WLG | CHC | B | 1800-01-01 00:00:00.000 | NZ | 2
1063 | WLG | CHC | B | 1800-01-01 00:00:00.000 | NZ | 3
1063 | WLG | CHC | B | 1800-01-01 00:00:00.000 | NZ | 4
1064 | WLG | CHC | M | 2010-09-15 00:00:00.000 | NZ | 1
1064 | WLG | CHC | M | 1800-01-01 00:00:00.000 | NZ | 2
1064 | WLG | CHC | M | 1800-01-01 00:00:00.000 | NZ | 3
1064 | WLG | CHC | M | 1800-01-01 00:00:00.000 | NZ | 4
1065 | WLG | CHC | Y | 2010-08-25 00:00:00.000 | NZ | 1
1065 | WLG | CHC | Y | 1800-01-01 00:00:00.000 | NZ | 2
1065 | WLG | CHC | Y | 1800-01-01 00:00:00.000 | NZ | 3
1065 | WLG | CHC | Y | 1800-01-01 00:00:00.000 | NZ | 4
Может ли кто-нибудь мне помочь