简体   繁体   English

复杂SQL查询 - 连接表中的字段总和

[英]Complex SQL query - sum of field in joined table

SQL Fiddle: http://sqlfiddle.com/#!2/b46ea/2 SQL小提琴: http ://sqlfiddle.com/#!2/b46ea/2

Current SQL: 当前的SQL:

SELECT `vehicle_journey`.*,
    ADDTIME(departure, SEC_TO_TIME((1))) AS stopDepartureTime
FROM `vehicle_journey`
INNER JOIN `vehicle_journey_days`
    ON `vehicle_journey_days`.`journey_reference` = `vehicle_journey`.`reference`
INNER JOIN `journey_pattern`
    ON `journey_pattern`.`reference` = `vehicle_journey`.`journey_pattern_reference`
INNER JOIN `journey_pattern_timing_link`
    ON `journey_pattern_timing_link`.`section` = `journey_pattern`.`journey_pattern_section`
WHERE `journey_pattern_timing_link`.`stop` = '1900HA030193'
    AND `vehicle_journey_days`.`day` = 'MondayToFriday'

The final aim of this SQL statement is to get the arrival time of a bus based on its departure time in vehicle_journey and the run_time in journey_pattern_timing_link . 此SQL语句的最终目的是根据其在vehicle_journey departure时间和journey_pattern_timing_linkrun_time获取总线的到达时间。

The reason this is proving difficult and why I'm asking for help is because of the nature of the journey_pattern_timing_link table. 这是证明困难的原因以及我寻求帮助的原因是因为journey_pattern_timing_link表的性质。 The table is set up to provide the time in seconds it takes to get from one stop to the next (this is so that times are relative to each departure time). 该表被设置为提供从一个停止到下一个停止所需的时间(这是时间相对于每个启程时间)。

As you will see in the select statement I'm not far from it. 正如您将在select语句中看到的那样,我离它不远。 The 'ADDTIME' function is where the bus stop's departure time will be calculated. “ADDTIME”功能是计算公交车站出发时间的地方。 But, you will see a '1' in the value to add to the departure time. 但是,您会在值中看到“1”以添加到出发时间。 This is just a placeholder, essentially it's inside that SEC_TO_TIME function that I need to add up and work out the departure time. 这只是一个占位符,基本上它在SEC_TO_TIME函数内部,我需要加起来并计算出发时间。

But wait, what are we adding up? 但是等等,我们加起来了什么? Well this is what I meant about the nature of the journey_pattern_timing_link table. 这就是我对journey_pattern_timing_link表的本质的journey_pattern_timing_link What I need to do is get the total of all the run_time fields inside the journey_pattern_timing_link table where the ID is LESS than the one of the selected stop in the where statement AND the journey_pattern_timing_link . 我需要做的是获取journey_pattern_timing_link表中所有run_time字段的journey_pattern_timing_link ,其中ID少于where语句和journey_pattern_timing_link选定的stop之一。 section is equal to the one based on the join. section等于基于join的一个。

I understand this is a very long question and one that requires a lot of thought but I am well and truly stuck. 我知道这是一个非常长的问题,需要经过深思熟虑,但我很好并且真正陷入困境。 Looking at the SQL Fiddle's schema browser should help to understand the table structure. 查看SQL Fiddle的模式浏览器应该有助于理解表结构。

Try this, although SQL fiddle seems to struggle with it:- 尝试这个,虽然SQL小提琴似乎很难与它斗争: -

SELECT vehicle_journey.id, 
        vehicle_journey.journey_pattern_reference, 
        vehicle_journey.departure, 
        vehicle_journey.service_reference, 
        vehicle_journey.reference, 
        vehicle_journey.code, 
        vehicle_journey_days.day,
        TIME(ADDTIME(departure, SEC_TO_TIME(SUM(jptl2.run_time)))) AS stopDepartureTime
FROM vehicle_journey
INNER JOIN vehicle_journey_days ON vehicle_journey_days.journey_reference = vehicle_journey.reference
INNER JOIN journey_pattern ON journey_pattern.reference = vehicle_journey.journey_pattern_reference
INNER JOIN journey_pattern_timing_link ON journey_pattern_timing_link.section = journey_pattern.journey_pattern_section AND journey_pattern_timing_link.stop = '1900HA080102' 
INNER JOIN journey_pattern_timing_link  jptl2 ON jptl2.section = journey_pattern.journey_pattern_section AND jptl2.ID <= journey_pattern_timing_link.ID
WHERE vehicle_journey_days.day = 'Saturday'
GROUP BY vehicle_journey.id, vehicle_journey.journey_pattern_reference, vehicle_journey.departure, vehicle_journey.service_reference, vehicle_journey.reference, vehicle_journey.code

I think this does it. 我认为这样做。 It does a second join onto journey_pattern_timing_link to get all the stops that are up to and including the stop you are looking for, then sum their run_time and add it to departure : 它会在journey_pattern_timing_link进行第二次加入,以获得所有停靠点,包括您正在寻找的停靠点,然后汇总其run_time并将其添加到departure

SELECT 
  `vehicle_journey`.*,
  ADDTIME(departure,SEC_TO_TIME(SUM(`previous_stops`.`run_time`))) `duration`  
FROM `vehicle_journey`
INNER JOIN `vehicle_journey_days`
    ON `vehicle_journey_days`.`journey_reference` = `vehicle_journey`.`reference`
INNER JOIN `journey_pattern`
    ON `journey_pattern`.`reference` = `vehicle_journey`.`journey_pattern_reference`
INNER JOIN `journey_pattern_timing_link`
    ON `journey_pattern_timing_link`.`section` = `journey_pattern`.`journey_pattern_section`
LEFT JOIN `journey_pattern_timing_link` `previous_stops`
    ON `journey_pattern_timing_link`.`section` = `previous_stops`.`section`
    AND `journey_pattern_timing_link`.`id` >= `previous_stops`.`id`
WHERE `journey_pattern_timing_link`.`stop` = '1900HA080987'
    AND `vehicle_journey_days`.`day` = 'Saturday'
GROUP BY
  `vehicle_journey`.`id`,
  `vehicle_journey`.`journey_pattern_reference`,
  `vehicle_journey`.`departure`,
  `vehicle_journey`.`service_reference`,
  `vehicle_journey`.`reference`,
  `vehicle_journey`.`code`

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

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