简体   繁体   English

查询特定模式的旅行数据(VBA / SQL / MS Access)

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

I'm in need of some help with a Certain SQL Query. 我需要某些SQL查询的帮助。 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. 我需要查找其中Train#从一个城市到另一个城市到州外的旅行的实例。 Keep in mind, that doing more cities inbetween (3 Cities and then an out of State for example) is also just as useful. 请记住,在两个城市之间做更多的城市(例如3个城市,然后是州外)也同样有用。

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: 因此,在3行数据中,您将看到:

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. 连续许多天都由多次Train#出发。 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. 显然,例如,我们不想将2014年1月1日至2014年2月22日的偏离视为联系。 A connection would need to be within 1-2 days of another to even be considered. 一个连接将需要相隔1-2天,甚至可以考虑。

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. 您的语法可能会略有不同,具体取决于您的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). 您需要至少2条状态匹配的记录,并且您想要的总记录多于匹配记录(这表示至少有1条状态外记录)。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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