SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 complex query needs help
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

alhakimi
Starting Member

Yemen
23 Posts

Posted - 11/22/2013 :  16:56:51  Show Profile  Reply with Quote
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

India
52317 Posts

Posted - 11/23/2013 :  03:27:06  Show Profile  Reply with Quote

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


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs

Edited by - visakh16 on 11/23/2013 03:42:57
Go to Top of Page

waterduck
Aged Yak Warrior

Malaysia
961 Posts

Posted - 11/25/2013 :  05:05:00  Show Profile  Reply with Quote
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

Yemen
23 Posts

Posted - 12/07/2013 :  15:48:41  Show Profile  Reply with Quote
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


Edited by - alhakimi on 12/07/2013 15:50:25
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000