Я пытался получить из моей таблицы файл XML в определенном формате.
Моя таблица следующая:
IF OBJECT_ID('TripHistoryTest','U') IS NOT NULL DROP TABLE TripHistoryTest
create table TripHistoryTest
(
[BarCode] nvarchar(100),
[PassengerName] nvarchar(100),
[SegmentType] char(1),
[Flight1_From] nvarchar(100),
[Flight1_FromTime] nvarchar(100),
[Flight1_To] nvarchar(100),
[Flight1_ToTime] nvarchar(100),
[Flight2_From] nvarchar(100),
[Flight2_FromTime] nvarchar(100),
[Flight2_To] nvarchar(100),
[Flight2_ToTime] nvarchar(100),
[Flight3_From] nvarchar(100),
[Flight3_FromTime] nvarchar(100),
[Flight3_To] nvarchar(100),
[Flight3_ToTime] nvarchar(100),
[Flight4_From] nvarchar(100),
[Flight4_FromTime] nvarchar(100),
[Flight4_To] nvarchar(100),
[Flight4_ToTime] nvarchar(100),
[Hotel_From] nvarchar(100),
[Hotel_FromTime] nvarchar(100),
[Hotel_To] nvarchar(100),
[Hotel_ToTime] nvarchar(100),
[Train_From] nvarchar(100),
[Train_FromTime] nvarchar(100),
[Train_To] nvarchar(100),
[Train_ToTime] nvarchar(100),
[Car_From] nvarchar(100),
[Car_FromTime] nvarchar(100),
[Car_To] nvarchar(100),
[Car_ToTime] nvarchar(100)
)
insert into TripHistoryTest
(
[BarCode],
[PassengerName],
[SegmentType],
[Flight1_From],
[Flight1_FromTime] ,
[Flight1_To] ,
[Flight1_ToTime] ,
[Flight2_From],
[Flight2_FromTime],
[Flight2_To],
[Flight2_ToTime],
[Flight3_From],
[Flight3_FromTime],
[Flight3_To],
[Flight3_ToTime],
[Flight4_From],
[Flight4_FromTime],
[Flight4_To],
[Flight4_ToTime],
[Hotel_From],
[Hotel_FromTime],
[Hotel_To],
[Hotel_ToTime],
[Train_From] ,
[Train_FromTime] ,
[Train_To] ,
[Train_ToTime] ,
[Car_From],
[Car_FromTime],
[Car_To],
[Car_ToTime]
)
select
'BR666999', 'KETYM PORTA', 'F',
'MILAN', '06:00', 'NAPOLI', '07:00',
'NAPOLI', '08:00', 'BUDAPEST', '10:00',
'BUDAPEST', '11:00', 'CRAIOVA', '13:00',
'CRAIOVA', '14:00', 'ROMA', '16:00',
'','','','',
'','','','',
'','','',''
UNION ALL
select
'BR666999', 'KETYM PORTA', 'F',
'ROMA', '17:00', 'TORINO', '18:00',
'TORINO', '19:00', 'MADRID', '20:00',
'','','','',
'','','','',
'','','','',
'','','','',
'','','',''
UNION ALL
select
'BR666999', 'KETYM PORTA', 'F',
'MADRID', '21:00', 'MANILA', '23:00',
'','','','',
'','','','',
'','','','','','','','','','','','','','','',''
UNION ALL
select
'BR666999', 'KETYM PORTA', 'T',
'', '', '', '',
'', '', '', '',
'', '', '', '',
'', '', '', '',
'','','','',
'MANILA','23:30','CALBAYOG','23:40',
'','','',''
UNION ALL
select
'BR666999', 'KETYM PORTA', 'H',
'', '', '', '',
'', '', '', '',
'', '', '', '',
'', '', '', '',
'LASTHOPEHOTEL','23:50','LASTHOPEHOTEL','06:00',
'','','','',
'','','',''
UNION ALL
select
'BR666999', 'KETYM PORTA', 'C',
'', '', '', '',
'', '', '', '',
'', '', '', '',
'', '', '', '',
'','','','',
'','','','',
'CALBAYOG','07:00','MARIANA TRENCH',''
Я имитирую то, что у меня есть в системе сейчас, поэтому вот несколько заметок:
SegmentType:
F: Рейс
T: Поезд
C: Автомобиль
H: Отель
Я сделал запрос, используя XML PATH, но безуспешно, вот мой вывод:
select
-- ###############
-- ### BARCODE ###
-- ###############
A.[BarCode],
-- ################
-- ### TRAVELER ###
-- ################
(
select distinct
B.[PassengerName]
from
TripHistoryTest B
where 1=1
and A.[BarCode] = B.[BarCode]
group by B.[PassengerName]
for xml path ('Passenger'), root('PassengerList'), TYPE
),
-- ###################
-- ### AIR SEGMENT ###
-- ###################
(
select distinct
case when C.[Flight1_From] = '' then null else C.[Flight1_From] end as "AirSeg/Flight_From",
case when C.[Flight1_From] = '' then null else C.[Flight1_FromTime] end as "AirSeg/Flight_FromTime",
case when C.[Flight1_To] = '' then null else C.[Flight1_To] end as "AirSeg/Flight_To",
case when C.[Flight1_To] = '' then null else C.[Flight1_ToTime] end as "AirSeg/Flight_ToTime",
'',
case when C.[Flight2_From] = '' then null else C.[Flight2_From] end as "AirSeg/Flight_From",
case when C.[Flight2_From] = '' then null else C.[Flight2_FromTime] end as "AirSeg/Flight_FromTime",
case when C.[Flight2_To] = '' then null else C.[Flight2_To] end as "AirSeg/Flight_To",
case when C.[Flight2_To] = '' then null else C.[Flight2_ToTime] end as "AirSeg/Flight_ToTime",
'',
case when C.[Flight3_From] = '' then null else C.[Flight3_From] end as "AirSeg/Flight_From",
case when C.[Flight3_From] = '' then null else C.[Flight3_FromTime] end as "AirSeg/Flight_FromTime",
case when C.[Flight3_To] = '' then null else C.[Flight3_To] end as "AirSeg/Flight_To",
case when C.[Flight3_To] = '' then null else C.[Flight3_ToTime] end as "AirSeg/Flight_ToTime",
'',
case when C.[Flight4_From] = '' then null else C.[Flight4_From] end as "AirSeg/Flight_From",
case when C.[Flight4_From] = '' then null else C.[Flight4_FromTime] end as "AirSeg/Flight_FromTime",
case when C.[Flight4_To] = '' then null else C.[Flight4_To] end as "AirSeg/Flight_To",
case when C.[Flight4_To] = '' then null else C.[Flight4_ToTime] end as "AirSeg/Flight_ToTime"
from
TripHistoryTest C
where 1=1
and A.[BarCode] = C.[BarCode]
and C.[SegmentType] = 'F'
group by
C.[Flight1_From], C.[Flight1_FromTime], C.[Flight2_From], C.[Flight2_FromTime], C.[Flight3_From], C.[Flight3_FromTime],
C.[Flight4_From], C.[Flight4_FromTime], C.[Flight1_To], C.[Flight1_ToTime], C.[Flight2_To], C.[Flight2_ToTime], C.[Flight3_To], C.[Flight3_ToTime],
C.[Flight4_To], C.[Flight4_ToTime]
for xml path ('Segment_List'), TYPE
)
from
TripHistoryTest A
group by
A.[BarCode], A.[PassengerName], A.[Flight1_From], A.[Flight1_FromTime], A.[Flight2_From], A.[Flight2_FromTime], A.[Flight3_From], A.[Flight3_FromTime],
A.[Flight4_From], A.[Flight4_FromTime], A.[Flight1_To], A.[Flight1_ToTime], A.[Flight2_To], A.[Flight2_ToTime], A.[Flight3_To], A.[Flight3_ToTime],
A.[Flight4_To], A.[Flight4_ToTime]
order by A.[Flight1_FromTime] asc
for xml path ('BARCODE'), root ('BARCODE_List')
Мой окончательный вывод был:
<BARCODE_List>
<BARCODE>
<BarCode>BR666999</BarCode>
<PassengerList>
<Passenger>
<PassengerName>KETYM PORTA</PassengerName>
</Passenger>
</PassengerList>
<Segment_List>
<AirSeg>
<Flight_From>MADRID</Flight_From>
<Flight_FromTime>21:00</Flight_FromTime>
<Flight_To>MANILA</Flight_To>
<Flight_ToTime>23:00</Flight_ToTime>
</AirSeg>
</Segment_List>
<Segment_List>
<AirSeg>
<Flight_From>MILAN</Flight_From>
<Flight_FromTime>06:00</Flight_FromTime>
<Flight_To>NAPOLI</Flight_To>
<Flight_ToTime>07:00</Flight_ToTime>
</AirSeg>
<AirSeg>
<Flight_From>NAPOLI</Flight_From>
<Flight_FromTime>08:00</Flight_FromTime>
<Flight_To>BUDAPEST</Flight_To>
<Flight_ToTime>10:00</Flight_ToTime>
</AirSeg>
<AirSeg>
<Flight_From>BUDAPEST</Flight_From>
<Flight_FromTime>11:00</Flight_FromTime>
<Flight_To>CRAIOVA</Flight_To>
<Flight_ToTime>13:00</Flight_ToTime>
</AirSeg>
<AirSeg>
<Flight_From>CRAIOVA</Flight_From>
<Flight_FromTime>14:00</Flight_FromTime>
<Flight_To>ROMA</Flight_To>
<Flight_ToTime>16:00</Flight_ToTime>
</AirSeg>
</Segment_List>
<Segment_List>
<AirSeg>
<Flight_From>ROMA</Flight_From>
<Flight_FromTime>17:00</Flight_FromTime>
<Flight_To>TORINO</Flight_To>
<Flight_ToTime>18:00</Flight_ToTime>
</AirSeg>
<AirSeg>
<Flight_From>TORINO</Flight_From>
<Flight_FromTime>19:00</Flight_FromTime>
<Flight_To>MADRID</Flight_To>
<Flight_ToTime>20:00</Flight_ToTime>
</AirSeg>
</Segment_List>
</BARCODE>
<BARCODE>
<BarCode>BR666999</BarCode>
<PassengerList>
<Passenger>
<PassengerName>KETYM PORTA</PassengerName>
</Passenger>
</PassengerList>
<Segment_List>
<AirSeg>
<Flight_From>MADRID</Flight_From>
<Flight_FromTime>21:00</Flight_FromTime>
<Flight_To>MANILA</Flight_To>
<Flight_ToTime>23:00</Flight_ToTime>
</AirSeg>
</Segment_List>
<Segment_List>
<AirSeg>
<Flight_From>MILAN</Flight_From>
<Flight_FromTime>06:00</Flight_FromTime>
<Flight_To>NAPOLI</Flight_To>
<Flight_ToTime>07:00</Flight_ToTime>
</AirSeg>
<AirSeg>
<Flight_From>NAPOLI</Flight_From>
<Flight_FromTime>08:00</Flight_FromTime>
<Flight_To>BUDAPEST</Flight_To>
<Flight_ToTime>10:00</Flight_ToTime>
</AirSeg>
<AirSeg>
<Flight_From>BUDAPEST</Flight_From>
<Flight_FromTime>11:00</Flight_FromTime>
<Flight_To>CRAIOVA</Flight_To>
<Flight_ToTime>13:00</Flight_ToTime>
</AirSeg>
<AirSeg>
<Flight_From>CRAIOVA</Flight_From>
<Flight_FromTime>14:00</Flight_FromTime>
<Flight_To>ROMA</Flight_To>
<Flight_ToTime>16:00</Flight_ToTime>
</AirSeg>
</Segment_List>
<Segment_List>
<AirSeg>
<Flight_From>ROMA</Flight_From>
<Flight_FromTime>17:00</Flight_FromTime>
<Flight_To>TORINO</Flight_To>
<Flight_ToTime>18:00</Flight_ToTime>
</AirSeg>
<AirSeg>
<Flight_From>TORINO</Flight_From>
<Flight_FromTime>19:00</Flight_FromTime>
<Flight_To>MADRID</Flight_To>
<Flight_ToTime>20:00</Flight_ToTime>
</AirSeg>
</Segment_List>
</BARCODE>
<BARCODE>
<BarCode>BR666999</BarCode>
<PassengerList>
<Passenger>
<PassengerName>KETYM PORTA</PassengerName>
</Passenger>
</PassengerList>
<Segment_List>
<AirSeg>
<Flight_From>MADRID</Flight_From>
<Flight_FromTime>21:00</Flight_FromTime>
<Flight_To>MANILA</Flight_To>
<Flight_ToTime>23:00</Flight_ToTime>
</AirSeg>
</Segment_List>
<Segment_List>
<AirSeg>
<Flight_From>MILAN</Flight_From>
<Flight_FromTime>06:00</Flight_FromTime>
<Flight_To>NAPOLI</Flight_To>
<Flight_ToTime>07:00</Flight_ToTime>
</AirSeg>
<AirSeg>
<Flight_From>NAPOLI</Flight_From>
<Flight_FromTime>08:00</Flight_FromTime>
<Flight_To>BUDAPEST</Flight_To>
<Flight_ToTime>10:00</Flight_ToTime>
</AirSeg>
<AirSeg>
<Flight_From>BUDAPEST</Flight_From>
<Flight_FromTime>11:00</Flight_FromTime>
<Flight_To>CRAIOVA</Flight_To>
<Flight_ToTime>13:00</Flight_ToTime>
</AirSeg>
<AirSeg>
<Flight_From>CRAIOVA</Flight_From>
<Flight_FromTime>14:00</Flight_FromTime>
<Flight_To>ROMA</Flight_To>
<Flight_ToTime>16:00</Flight_ToTime>
</AirSeg>
</Segment_List>
<Segment_List>
<AirSeg>
<Flight_From>ROMA</Flight_From>
<Flight_FromTime>17:00</Flight_FromTime>
<Flight_To>TORINO</Flight_To>
<Flight_ToTime>18:00</Flight_ToTime>
</AirSeg>
<AirSeg>
<Flight_From>TORINO</Flight_From>
<Flight_FromTime>19:00</Flight_FromTime>
<Flight_To>MADRID</Flight_To>
<Flight_ToTime>20:00</Flight_ToTime>
</AirSeg>
</Segment_List>
</BARCODE>
<BARCODE>
<BarCode>BR666999</BarCode>
<PassengerList>
<Passenger>
<PassengerName>KETYM PORTA</PassengerName>
</Passenger>
</PassengerList>
<Segment_List>
<AirSeg>
<Flight_From>MADRID</Flight_From>
<Flight_FromTime>21:00</Flight_FromTime>
<Flight_To>MANILA</Flight_To>
<Flight_ToTime>23:00</Flight_ToTime>
</AirSeg>
</Segment_List>
<Segment_List>
<AirSeg>
<Flight_From>MILAN</Flight_From>
<Flight_FromTime>06:00</Flight_FromTime>
<Flight_To>NAPOLI</Flight_To>
<Flight_ToTime>07:00</Flight_ToTime>
</AirSeg>
<AirSeg>
<Flight_From>NAPOLI</Flight_From>
<Flight_FromTime>08:00</Flight_FromTime>
<Flight_To>BUDAPEST</Flight_To>
<Flight_ToTime>10:00</Flight_ToTime>
</AirSeg>
<AirSeg>
<Flight_From>BUDAPEST</Flight_From>
<Flight_FromTime>11:00</Flight_FromTime>
<Flight_To>CRAIOVA</Flight_To>
<Flight_ToTime>13:00</Flight_ToTime>
</AirSeg>
<AirSeg>
<Flight_From>CRAIOVA</Flight_From>
<Flight_FromTime>14:00</Flight_FromTime>
<Flight_To>ROMA</Flight_To>
<Flight_ToTime>16:00</Flight_ToTime>
</AirSeg>
</Segment_List>
<Segment_List>
<AirSeg>
<Flight_From>ROMA</Flight_From>
<Flight_FromTime>17:00</Flight_FromTime>
<Flight_To>TORINO</Flight_To>
<Flight_ToTime>18:00</Flight_ToTime>
</AirSeg>
<AirSeg>
<Flight_From>TORINO</Flight_From>
<Flight_FromTime>19:00</Flight_FromTime>
<Flight_To>MADRID</Flight_To>
<Flight_ToTime>20:00</Flight_ToTime>
</AirSeg>
</Segment_List>
</BARCODE>
</BARCODE_List>
Однако, это не то, чего я ожидал, я хотел бы иметь что-то вроде этого:
<BARCODE_List>
<BARCODE>
<BarCode>BR666999</BarCode>
<PassengerList>
<Passenger>
<PassengerName>KETYM PORTA</PassengerName>
</Passenger>
</PassengerList>
<Segment_List>
<AirSeg>
<Flight_From>MILAN</Flight_From>
<Flight_FromTime>06:00</Flight_FromTime>
<Flight_To>NAPOLI</Flight_To>
<Flight_ToTime>07:00</Flight_ToTime>
</AirSeg>
<AirSeg>
<Flight_From>NAPOLI</Flight_From>
<Flight_FromTime>08:00</Flight_FromTime>
<Flight_To>BUDAPEST</Flight_To>
<Flight_ToTime>10:00</Flight_ToTime>
</AirSeg>
<AirSeg>
<Flight_From>BUDAPEST</Flight_From>
<Flight_FromTime>11:00</Flight_FromTime>
<Flight_To>CRAIOVA</Flight_To>
<Flight_ToTime>13:00</Flight_ToTime>
</AirSeg>
<AirSeg>
<Flight_From>CRAIOVA</Flight_From>
<Flight_FromTime>14:00</Flight_FromTime>
<Flight_To>ROMA</Flight_To>
<Flight_ToTime>16:00</Flight_ToTime>
</AirSeg>
<AirSeg>
<Flight_From>ROMA</Flight_From>
<Flight_FromTime>17:00</Flight_FromTime>
<Flight_To>TORINO</Flight_To>
<Flight_ToTime>18:00</Flight_ToTime>
</AirSeg>
<AirSeg>
<Flight_From>TORINO</Flight_From>
<Flight_FromTime>19:00</Flight_FromTime>
<Flight_To>MADRID</Flight_To>
<Flight_ToTime>20:00</Flight_ToTime>
</AirSeg>
<AirSeg>
<Flight_From>MADRID</Flight_From>
<Flight_FromTime>21:00</Flight_FromTime>
<Flight_To>MANILA</Flight_To>
<Flight_ToTime>23:00</Flight_ToTime>
</AirSeg>
</Segment_List>
</BARCODE>
</BARCODE_List>
Есть ли простой способ сделать это? Очевидно, что я хотел бы включить сегмент Hotel, Car и Train, поскольку они являются важной информацией.