English 中文(简体)
复杂 SQL 查询协助
原标题:Complex SQL query assistance
  • 时间:2012-05-28 12:51:04
  •  标签:
  • sql

我有一个 DB, 包含关于公共汽车到达/离开站点的信息。 在应用程序中, 我需要显示用户选择的站点的所有偏离 。

这意味着,我必须确定通过选定站点通过的每一条线。

SELECT DISTINCT LinePaths.TimetableID 
               ,Lines.LineName AS [Line]
               ,Timetable.Heading
               ,LinePaths.Departure
               ,Regime.Name AS [Regime]
FROM            LinePaths
INNER JOIN Timetables ON Timetables.TimetableID = LinePaths.TimetableID
INNER JOIN Lines ON Timetable.LineID = Lines.LineID
INNER JOIN Stations ON LinePaths.Station = Station.StationID
INNER JOIN Regimes ON Timetables.Regime = Regimes.RegimeID
WHERE Station = @Station

问题在于, 而不是 < code>Timetable。 标题 我需要显示公交车驶往的最后车站的名称。 这些是我手头的表格的图案 :

  • Stations - ID, Name
  • Lines - LineID, LineName
  • Timetable - TimetableID, LineID, Heading, Regime (Regime defines on which days the bus drives)
  • LinePaths - ID, TimetableID, SN, Arrival, Departure, StationID (The SN is a serial number that defines the order of stations on a certain paths (the bus will arrive to those stations in that particular order).)

所以, 而不是< code>Timetable。 标题 我需要有一个 < code> stations. Name , 而这里我谈到我的问题 。

该列必须代表公交车前往的最后车站(对于每行),所以我必须设法确定最后车站(对于通过所选车站的每行,我还必须确定最后车站(对于通过所选车站的每行),为此我必须使用 LinePaths 表格。在该表格中,数据按TimetableID 分组,以便 TimetableID 具有相同价值的条目由 SN 订购,该数值代表公交车到达车站的顺序(例如: xxxxx,11:20,9999 /code>)。 这意味着,根据时间表11 驱动的公交车将抵达8:15 至PASID ,该站是 第3次 < dcode > < dcode > < d/x > < dcode > 。

最后,我的问题是—— 我如何使用 SQL, 获取以下模式的数据:

  • FinalStationName,
  • Departure(from selected station),
  • Regime.
最佳回答

最有效的方式是有一个带有时记号的新的表格和最后台站的名称,然后与其他台站一起加入这个表格,但是,如果你真的需要获得这个数值而不需要额外的表格,您将新来计算每个时间表的最后台站。这个查询获得了每个时间表的编号和最后一个台站的名称。

SELECT li.TimetableID, s.Name
FROM LinePaths li INNER JOIN
    (SELECT TimetableID, max(SN) as SN FROM LinePaths) as aux
ON aux.TimetableID = li.TimetableID AND aux.SN = li.SN
INNER JOIN Stations s
ON s.ID = li.StationID

并且你可以把它当作一个子处理器, 通过时间表ID 连接到您的查询中。 这不是最有效的东西, 但是它应该有效 。

问题回答

暂无回答




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

难以执行 REGEXP_SUBSTR

I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域

SQL Query Shortcuts

What are some cool SQL shorthands that you know of? For example, something I learned today is you can specify to group by an index: SELECT col1, col2 FROM table GROUP BY 2 This will group by col2

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

Running numbers in SQL

I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0; How can i create a result set with a running number in the first row? So the result would look like this: 1 Name_1 2 ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...