Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 complex query needs help

Author  Topic 

alhakimi
Starting Member

23 Posts

Posted - 2013-11-22 : 16:56:51
Dear friends,

I have the following table has the passengers informations example:

passenger Id| Reservation #| Name| Flight date | Flight Number | departure city|Arrival City|Last Modified date | Purchase date
100001 | 10001 | Mike| 10/10/2013 | RJ100 | AMM | DEL |10/10/2013 15:10 | 08/10/2013
100002 | 10001 |Jason| 10/10/2013 | RJ100 | AMM | DEL |10/10/2013 15:10 | 08/10/2013
100003 | 10001 |amit| 10/11/2013 | RJ100 | AMM | DEL |10/10/2013 15:10 | 08/10/2013
100004 | 10002 | jack| 12/11/2013 | RJ200 | Del | AMM |11/11/2013 20:00 | 1/11/2013
100004 | 10002 | Jack| 13/11/2013 | RJ300 | AMM | CAI |11/11/2013 01:00 | 1/11/2013

what i want is to have some details from this table in to another view to looks like the below table where there is some time passengers traveling on the same reservation # i want them in new field(All of them in one field separated by , or |) and another field shows the number of passengers and i want to know the time difference between the purchase date and flight date also some time same passenger goes from example from first city to the second city with stay in middle city(transit city) ex. a-b-c so i want the B city to be in different field some time i could have two transit cities like (a-b-c-d) so i want b and in this field:
i also need the first departure city and last arrival city to be instead of departure city column and arrival city column like below:
PassengerID|reservation#|Name|pax in same res#|# of pax|differnece btw fltdate and purchasedate| transit city|
100001 |10001 | Mike|Jason,amit |3 | 2 days | |
100002 |10001 | Jason|Mike,amit |3 | 2 days | |
100003 |10001 | Amit|Jason,Mike |3 | 2 days | |
100004 | 10002 | jack| |1 | 11 days | AMM |



fltdate |flight# |depcity|arrcity|lastModdate |purchase date
10/10/2013|RJ100 |AMM |DEL |10/10/2013 15:10|08/10/2013
10/10/2013|RJ100 |AMM |DEL |10/10/2013 15:10|08/10/2013
12/11/2013|RJ200,RJ300 |DEL |CAI |11/11/2013 20:00|1/11/2013


please let me know if you have any clarification.

thank you and B. Regards

Alhakimi

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-11-23 : 03:27:06
[code]
SELECT PassengerID,[reservation#],Name,
STUFF((SELECT ',' + Name FROM Table WHERE [reservation#] = t.[reservation#] AND Name <> t.Name 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.[Arrival City]
FROM Table t1
JOIN Table t2
ON t1.PassengerID = t2.PassengerID
AND t1.[reservation#] = t2.[reservation#]
AND t1.[Arrival City] = t2.[departure city]
WHERE t1.PassengerID = t.PassengerID
AND t1.[reservation#] = t.[reservation#]
FOR XML PATH('')
),1,1,'') AS TransitCity,
[Flight date],
STUFF((SELECT ',' + [Flight Number]
FROM Table
WHERE PassengerID = t.PassengerID
AND [reservation#] = t.[reservation#]
FOR XML PATH('')
),1,1,'') AS FLightNo,
(SELECT TOP 1 [departure city]
FROM Table
WHERE PassengerID = t.PassengerID
AND [reservation#] = t.[reservation#]
ORDER BY [Flight date] ASC) AS DepartCity,
(SELECT TOP 1 [arrival city]
FROM Table
WHERE PassengerID = t.PassengerID
AND [reservation#] = t.[reservation#]
ORDER BY [Flight date] DESC) AS ArrivalCity
[Last Modified date],
[Purchase Date]
FROM (SELECT PassengerID,[reservation#],Name,MAX([Flight date]) AS [Flight date],MAX([Purchase Date]) AS [Purchase Date],MAX([Last Modified date]) AS [Last Modified date]
FROM Table
GROUP BY PassengerID,[reservation#],Name) t
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

waterduck
Aged Yak Warrior

982 Posts

Posted - 2013-11-25 : 05:05:00
quote:
Originally posted by visakh16


...AND [reservation#] = t.[reservation#]
ORDER BY [Flight date] DESC) AS ArrivalCity,
[Last Modified date],
...


Go to Top of Page

alhakimi
Starting Member

23 Posts

Posted - 2013-12-07 : 15:48:41
Dears

sorry for late response , i tried to run your query but got some dificultied i couldn't run it with the query you provided and was having dificulties to adjust your query in my orginal table structure as i provided you with short number of columns , here you may find below the complete structure and correct columns name , appriciates if you can re organize the query based on the correct columns name and complete columns.

to be clear i need the query to include the columns here along with the new data in the new column mentioned in my first post.


SELECT [Pax ID], Reservation, Surname, [First Name], [Pax Type], Phone, Mobile, Email, Passport,
[Fare Class], [Flight Date], [Board (Departure Airport)], [Off (Arrival Airport)], [Flight Number], [Original Booking Date],
[Leg Status], [Last Mod Date], [Booking Origin (How flight booked)], [Travel Agency Name], [Province of Travel Agent],
[User Base (Closest Airport to Travel Agent)], [lng_Res_Segments_Id_Nmbr (Unique ID; not of interest)], [Flight Status],
lng_Res_Pax_Group_Id_Nmbr, RSDesc, Sumpayment, sumcharge
FROM dbo.[passengerstable]

Thank you and B. Regards

Rashed

Go to Top of Page
   

- Advertisement -