简体   繁体   中英

Query Travel Data for Certain Pattern (VBA / SQL / MS Access)

I'm in need of some help with a Certain SQL Query. I have data that details trains and their routes. It includes the below data:

  1. Train#
  2. Departure#
  3. Departure City
  4. Departure State
  5. Arrival City
  6. Arrival State
  7. Date/Time

I need to find instances in which a Train# has traveled from City to City to Out of State. Keep in mind, that doing more cities inbetween (3 Cities and then an out of State for example) is also just as useful.

Example:

Train Starts in Chicago Departs to Chesterfield, IL
Departs Chesterfield, IL and Arrives at Springfield, IL
Departs Springfield, IL and Arrives at St. Louis Missouri

So in 3 lines Of Data You would See:

35, 500, Chicago, IL, Chesterfield, IL, 1/1/2014
35, 501, Chesterfield, IL, SpringField, IL, 1/1/2014
35, 502, SpringField, IL, St.Louis, MS, 1/1/2014

What would be the best way to detect instances like this in my data? Maybe have a Group By Train# to show a COUNT of how many departures they have with the departure matching the arrival of another?

PLEASE NOTE: The Data is going to consist of thousands of lines of data; Consisting of multiple departures from multiple Train#'s over the course of many many many days straight. Obviously we don't want to consider a departure on 1/1/2014 to a departure on 1/22/2014 to be a connection, for example. A connection would need to be within 1-2 days of another to even be considered.

The following will give you all the instances when a train travels out of state:

SELECT *
FROM YourTableName
WHERE [Departure State] <> [Arrival State]

Your syntax may be slightly different depending on your flavor of SQL.

EDIT: With the updated question, now I am not clear what you are asking. From the original question, I gathered that you wanted to know when a train crossed into another state. You have that information within each row- if the Departure State is different from the Arrival State, clearly the state crossed a state line. So there is no need to compare to other rows. Apparently you are asking something else- more data and expected results would help give a better picture.

I'm pretty sure this will work. Basically, you want to sum the number of times Arrival State and Departure State are the same and compare that to the number of entries for a given train. You want at least 2 records where the states match, and you want more total records than matching records (which would indicate at least one out-of-state record).

SELECT Train#, Sum(CASE WHEN [Departure State] = [Arrival State] THEN 1 ELSE 0) AS SameState, Sum(1) as TotRecords
FROM YourTableName
Group By Train#
HAVING Sum(CASE WHEN [Departure State] = [Arrival State] THEN 1 ELSE 0) >= 2
AND Sum(1) >= 3
AND Sum(CASE WHEN [Departure State] = [Arrival State] THEN 1 ELSE 0) < Sum(1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM