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.
| Author |
Topic |
|
stharish
Starting Member
43 Posts |
Posted - 2011-12-15 : 14:30:30
|
| I created a table which displays flights with the following columns: TripNumber, AccountName, Faretype, DepartureState, ArrivalState, DepartureDate, DepartureTime, DepartureAirportCode, ArrivalAirportCode, and ArrivalTime. I used a where statement to filter the results to display only:1. Flights where the DepartureState is in network and ArrivalState is out of networkOR2. Flights where the ArrivalState is in network and the DepartureState is out of network I need to filter the report further to only display flights where:1. Within 48 hours of an arrival there are no departures from the same state2. Within 48 hours of a departure there are no arrivals into the same stateAny ideas how I can do this?Thanks |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
stharish
Starting Member
43 Posts |
Posted - 2011-12-15 : 15:49:36
|
| Thanks for replying. Our network is defined by state. States CA, NV, AZ, TX, LA, OK are considered "in network". Basically I have a list of passenger occurpied flights that are arriving or departing from states that are out of network.I need to filter them further to exclude arrival/departures to and from the same state within 48 hours.For example: If a flight arrives CO (Colorado) on 12/15 and another flight departs CO on 12/17 I would want to exclude both flights for the list.If a flight departs CO on 12/17 and no other flight arrives CO on 12/5 or 12/16 I want to include the flight.We want to know in advance when we need to position aircraft out of network to pick up passengers AND we want to know when aircraft are dropping off passengers out of network. We want to market these positioning flights but not if there are other passenger occupied flights already bringing the aircraft to the departure/arrival state.I hope this all makes sense.Below query describes actual fields.SELECTTRIPS.TripNumber,ACCT.AccountName,CONVERT(Datetime, LEGS.DepartureTime) as [DATE],LEGS.ArrivalTime,ORIG.AirportICAOCode as [ORIG],DEST.AirportICAOCode as [DEST],ORIGSTATE.StateAbbreviation as [ORIGSTATE],DESTSTATE.StateAbbreviation as [DESTSTATE],PRI.LookupValue as [FARETYPE]FROM tm_Legs as LEGSINNER JOIN tm_Trips as TRIPS ON TRIPS.TripID=LEGS.TripIDINNER JOIN tl_Airports as ORIG ON ORIG.AirportID=LEGS.OriginAirportIDINNER JOIN tl_Airports as DEST ON DEST.AirportID=LEGS.DestinationAirportIDINNER JOIN tm_Accounts as ACCT ON ACCT.AccountID=TRIPS.AccountIDLEFT OUTER JOIN tl_States as ORIGSTATE ON ORIGSTATE.StateID=ORIG.StateIDLEFT OUTER JOIN tl_States as DESTSTATE ON DESTSTATE.StateID=DEST.StateIDINNER JOIN tl_Countries as ORIGCOUNTRY ON ORIGCOUNTRY.COUNTRYID=ORIG.CountryIDINNER JOIN tl_Countries as DESTCOUNTRY ON DESTCOUNTRY.COUNTRYID=DEST.CountryIDINNER JOIN tl_Lookups as PRI ON PRI.LookupID = LEGS.PriorityTypeLookupID and PRI.LookupTypeID = 82 WHERE LEGS.StatusTypeID <>13--Destination out of networkAND (ORIGSTATE.StateID IN (17,15,41,57,49,31) and DESTSTATE.StateID NOT IN (17,15,41,57,49,31))--Origin out of networkOR (DESTSTATE.StateID in (17,15,41,57,49,31) and ORIGSTate.StateID NOT IN (17,15,41,57,49,31))--Origin and destination out of networkOR (ORIGSTATE.StateID NOT IN (17,15,41,57,49,31) and DESTSTATE.StateID NOT IN (17,15,41,57,49,31))--Origin or destination out of countryOR (ORIGCOUNTRY.CountryID IN (40,150) or DESTCOUNTRY.CountryID IN (40,150))ORDER BY LEGS.DepartureTime ASC |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-12-16 : 01:08:53
|
| please explain us which column in your table will store the network related information and would help us in identifying the states within a network. Easiest thing would be to post some sample data from your tables and give the output based on that. Refer to link in Bretts signature for tips on how to post relevant details------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
stharish
Starting Member
43 Posts |
Posted - 2011-12-23 : 14:29:51
|
| ORIGSTATE --origin stateDESTSTATE --destination stateLEGS.DepartureTime --leg departure timeLEGS.ArrivalTime --leg arrival timeBasically I just need to compare these four values in filtering the results.Hope this helps. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-12-23 : 23:50:39
|
| why not put the values in a table and then use join with that rather then using a series of IN conditions?Also for your second part ( checking whether any other planes take off within 48 hours), you need to use a subquery using APPLY operator. if you can let us know the table structues with some sample data, then we can come up with a solution.With current form, its difficult to make out what are fields in each tables and which fields have what type of data etc------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|