我有以下问题。我需要获得具有以下数据的新报告:
1-乘客在同一预订号码一起旅行的列(预订),因此新栏目应包含与主要乘客一起旅行并以逗号分隔的所有乘客。并且每位乘客应该有自己的记录,并再次添加新列(乘客一起旅行)同一预订中的其他乘客。例如:
2-同一预订中的乘客数量号码将存储在名为(乘客人数)的栏目中
3-如果航班是通过连接点(过境城市)我想把连接城市放在一个叫做(过境城市)的新列中,如果有一个以上的过境城市,那么两个城市应该分开提到用逗号。例如,乘客从A到D旅行,在B过境一小时,有时在C过境3小时,因此过境城市应包括B,C。并且出发城市应该是A和到达城市应该D.所以一行将包含这个信息/乘客。
3-购买日期和航班日期之间的差异将存储在一个名为(购买和航班日期之间的差异)的新列中
4-抵达日期是新的要求,其中应包括第二航班的出发日期,例如预订的乘客从A到B,然后B到C,第一次航班的出发时间是在2013年12月22日和第二次航班是在2013年12月23日,所以抵达将是最后一班航班起飞日期。
这是表结构:
CREATE TABLE table1(
[Pax ID] [int] NULL,
[Reservation] [int] NULL,
[Surname] [varchar](40) NULL,
[First Name] [varchar](40) NULL,
[Pax Type] [char](1) NULL,
[Flight Date] [smalldatetime] NULL,
[Flight Number] [varchar](10) NULL,
[Board] [varchar](3) NULL,
[Off] [varchar](3) NULL,
[Original Booking Date] [smalldatetime] NULL,
[Last Mod Date] [smalldatetime] NULL
)
INSERT INTO table1([Pax ID],[Reservation],[Surname],[First Name],[Flight Date],[Flight Number],[Board],[Off],[Original Booking Date],[Last Mod Date])
SELECT '1558611','899842','SULIMAN','ALI','2/1/2013','FO151','RIY','SAH','1/1/2013','1/1/2013' UNION ALL
SELECT '1558612','899842','ALGANADI','HASAN','2/1/2013','FO151','RIY','SAH','1/1/2013','1/1/2013' UNION ALL
SELECT '1558613','899844','ALYAFEE','MOHMMED','1/1/2013','FO160','SAH','TAI','1/1/2013','1/1/2013' UNION ALL
SELECT '1558616','899847','ASSIRI','ahmed','2/1/2013','FO873','AHB','ADE','1/1/2013','1/1/2013' UNION ALL
SELECT '1558617','899847','ASSIRI','ahmed','4/1/2013','FO874','ADE','JED','1/1/2013','1/1/2013' UNION ALL
SELECT '1558628','899847','asiri','alin','2/1/2013','FO873','AHB','ADE','1/1/2013','1/1/2013' UNION ALL
SELECT '1558629','899847','asiri','alin','4/1/2013','FO874','ADE','JED','1/1/2013','1/1/2013' UNION ALL
SELECT '1558618','899848','ALAQWAA','EBRAHEEM','1/1/2013','FO173','HOD','SAH','1/1/2013','1/1/2013' UNION ALL
SELECT '1558621','899850','ALGELHM','HAMID','3/1/2013','FO196','SAH','AAY','1/1/2013','1/1/2013' UNION ALL
SELECT '1558622','899851','ASGHAR','AMER','1/1/2013','FO205','ADE','SAH','1/1/2013','1/1/2013' UNION ALL
SELECT '1558623','899852','ALHALILI','HAMZAH','2/1/2013','FO174','SAH','HOD','1/1/2013','1/1/2013' UNION ALL
SELECT '1558624','899852','ALJAHDARI','GHALIAH','2/1/2013','FO174','SAH','HOD','1/1/2013','1/1/2013' UNION ALL
SELECT '1558625','899853','ABDULLAH','ADEL','1/1/2013','FO173','HOD','SAH','1/1/2013','1/1/2013' UNION ALL
SELECT '1558626','899854','alasmari','mohammed','2/1/2013','FO873','AHB','ADE','1/1/2013','1/1/2013' UNION ALL
SELECT '1558627','899854','alasmari','mohammed','4/1/2013','FO874','ADE','AHB','1/1/2013','1/1/2013' UNION ALL
SELECT '1558631','899856','ALI','FAWAZ','3/1/2013','FO196','SAH','AAY','1/1/2013','1/1/2013' UNION ALL
SELECT '1558631','899856','ALI','FAWAZ','3/1/2013','FO197','AAY','TAI','1/1/2013','1/1/2013' UNION ALL
SELECT '1558631','899856','ALI','FAWAZ','4/1/2013','FO198','TAI','CAI','1/1/2013','1/1/2013' UNION ALL
SELECT '1558624','899851','ASSIRI','ahmed','1/4/2013','FO120','TAI','ADE','1/3/2013','1/3/2013' UNION ALL
SELECT '1558624','899851','ASSIRI','ahmed','4/5/2013','FO121','ADE','TAI','1/3/2013','1/3/2013'
这是我已经达到的目标
SELECT [Pax ID],[Reservation],[Surname],
STUFF((SELECT ',' + [Surname] FROM [TABLE1] WHERE [Reservation] = t.[Reservation] AND [Surname] <> t.[Surname] FOR XML PATH('')),1,1,'') AS [pax in same res#],
COUNT(1) OVER (PARTITION BY [Reservation]) AS [# of pax],
DATEDIFF(dd,[Purchase date],[Flight date]) AS [Diff flight date purchase date],
STUFF((SELECT DISTINCT ',' + t1.[Off]
FROM [TABLE1] t1
JOIN [TABLE1] t2
ON t1.[Pax ID] = t2.[Pax ID]
AND t1.[Reservation] = t2.[Reservation]
AND t1.[Off] = t2.[Board]
WHERE t1.[Pax ID] = t.[Pax ID]
AND t1.[Reservation] = t.[Reservation]
FOR XML PATH('')
),1,1,'') AS TransitCity,
[Flight date],
STUFF((SELECT ',' + [Flight Number]
FROM [TABLE1]
WHERE [Pax ID] = t.[Pax ID]
AND [Reservation] = t.[Reservation]
FOR XML PATH('')
),1,1,'') AS FLightNo,
(SELECT TOP 1 [Board]
FROM [TABLE1]
WHERE [Pax ID] = t.[Pax ID]
AND [Reservation] = t.[Reservation]
ORDER BY [Flight date] ASC) AS DepartCity,
(SELECT TOP 1 [Off]
FROM [TABLE1]
WHERE [Pax ID] = t.[Pax ID]
AND [Reservation] = t.[Reservation]
ORDER BY [Flight date] DESC) AS ArrivalCity,
[Last Mod Date],
[Purchase Date]
FROM (SELECT [Pax ID],[Reservation],[Surname],MAX([Flight Date]) AS [Flight date],MAX ([Original Booking Date]) AS [Purchase Date],MAX([Last Mod Date] ) AS [Last Mod Date]
FROM [TABLE1]
GROUP BY [Pax ID],[Reservation],[Surname]) t
需要输出
Pax ID Reservation Surname Firstname pax in same res# # of pax Diff flight date purchase date TransitCity Flight date FLightNo DepartCity ArrivalCity Last Modified date Purchase Date ARRIVAL DATE
1558611 899842 SULIMAN ALI ALGANADI HASAN 2 1 NULL 02/01/2013 0:00 FO151 RIY SAH 01/01/2013 0:00 01/01/2013 8:59 02/01/2013 0:00
1558612 899842 ALGANADI HASAN SULIMAN ALI 2 1 NULL 02/01/2013 0:00 FO151 RIY SAH 01/01/2013 0:00 01/01/2013 8:59 02/01/2013 0:00
1558613 899844 ALYAFEE MOHMMED NULL 1 2 NULL 03/01/2013 0:00 FO160 SAH TAI 01/01/2013 0:00 01/01/2013 9:00 03/01/2013 0:00
1558616 899847 ASSIRI ahmed ASIRI ALIN 2 1 ADE 02/01/2013 0:00 FO873,FO874 AHB JED 01/01/2013 0:00 01/01/2013 9:11 04/01/2013 0:00
1558628 899847 asiri alin ASSIRI AHMED 2 1 ADE 02/01/2013 0:00 FO873,FO874 AHB ADE 01/01/2013 0:00 01/01/2013 9:11 04/01/2013 0:00
1558626 899854 alasmari mohammed NULL 1 1 NULL 02/01/2013 0:00 FO873 AHB ADE 01/01/2013 0:00 01/01/2013 9:21 02/01/2013 0:00
1558627 899854 alasmari mohammed NULL 1 3 NULL 04/01/2013 0:00 FO874 ADE AHB 01/01/2013 0:00 01/01/2013 9:21 04/01/2013 0:00
1558631 899856 ALI FAWAZ NULL 1 2 AAY,TAI 03/01/2013 0:00 FO196,FO197,FO198 SAH CAI 01/01/2013 0:00 01/01/2013 9:22 04/01/2013 0:00
1558624 899851 ASSIRI ahmed NULL 1 30 NULL 01/04/2013 0:00 FO120 TAI ADE 01/03/2013 0:00 01/03/2013 9:11 01/04/2013 0:00
1558624 899851 ASSIRI ahmed NULL 1 33 NULL 04/05/2013 0:00 FO121 ADE TAI 01/03/2013 0:00 01/03/2013 9:11 04/05/2013 0:00
如果您需要任何进一步的详细信息,请与我们联系。
答案 0 :(得分:0)
检查一下 - SQL Fiddle
SELECT [Pax ID],[Reservation],[Surname],[First Name],
STUFF((SELECT distinct ',' + [Surname] + ' ' + [First Name] FROM [TABLE1] WHERE [Reservation] = t.[Reservation] AND [Surname] <> t.[Surname] FOR XML PATH('')),1,1,'') AS [pax in same res#],
COUNT(1) OVER (PARTITION BY [Reservation]) AS [# of pax],
DATEDIFF(dd,[Purchase date],[Flight date]) AS [Diff flight date purchase date],
STUFF((SELECT DISTINCT ',' + t1.[Off]
FROM [TABLE1] t1
JOIN [TABLE1] t2
ON t1.[Pax ID] = t2.[Pax ID]
AND t1.[Reservation] = t2.[Reservation]
AND t1.[Off] = t2.[Board]
WHERE t1.[Pax ID] = t.[Pax ID]
AND t1.[Reservation] = t.[Reservation]
FOR XML PATH('')
),1,1,'') AS TransitCity,
[Flight date],
STUFF((SELECT ',' + [Flight Number]
FROM [TABLE1]
WHERE [Pax ID] = t.[Pax ID]
AND [Reservation] = t.[Reservation]
FOR XML PATH('')
),1,1,'') AS FLightNo,
(SELECT TOP 1 [Board]
FROM [TABLE1]
WHERE [Pax ID] = t.[Pax ID]
AND [Reservation] = t.[Reservation]
ORDER BY [Flight date] ASC) AS DepartCity,
(SELECT TOP 1 [Off]
FROM [TABLE1]
WHERE [Pax ID] = t.[Pax ID]
AND [Reservation] = t.[Reservation]
ORDER BY [Flight date] DESC) AS ArrivalCity,
[Last Mod Date],
[Purchase Date]
FROM (SELECT [Pax ID],[Reservation],[Surname],[First Name],MAX([Flight Date]) AS [Flight date],MAX ([Original Booking Date]) AS [Purchase Date],MAX([Last Mod Date] ) AS [Last Mod Date]
FROM [TABLE1]
GROUP BY [Pax ID],[Reservation],[Surname],[First Name]) t