Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In the example below <code>calendar</code> is a simple utility table holding all dates that could ever be required. Personally, I'd most likely handle the missing values and, certainly, the pivoting at the application level, for ease of scalability.</p> <pre><code>DROP TABLE IF EXISTS fares; CREATE TABLE fares ( vehicle_id VARCHAR(12) NOT NULL , fare_start DATE NOT NULL , fare_end DATE NULL , fare DECIMAL(5,2) NOT NULL , PRIMARY KEY(vehicle_id,fare_start) ); INSERT INTO fares VALUES ('BAL2018','2013-01-21','2013-01-26',555.00), ('BAL2018','2013-01-27','2013-01-28',111.00), ('BAL2019','2013-01-26','2013-01-27',211.00), ('BAL2020','2013-01-26','2013-01-27',545.00); DROP TABLE IF EXISTS vehicles; CREATE TABLE vehicles (vehicle_id VARCHAR(12) NOT NULL PRIMARY KEY ,description VARCHAR(12) NOT NULL ); INSERT INTO vehicles VALUES ('BAL2018','vehicle01'), ('BAL2019','vehicle02'), ('BAL2020','vehicle03'); SELECT * FROM calendar WHERE dt BETWEEN '2013-01-20' AND '2013-01-28'; +------------+ | dt | +------------+ | 2013-01-20 | | 2013-01-21 | | 2013-01-22 | | 2013-01-23 | | 2013-01-24 | | 2013-01-25 | | 2013-01-26 | | 2013-01-27 | | 2013-01-28 | +------------+ SELECT c.dt,v.vehicle_id,f.fare_start,f.fare_end,f.fare FROM calendar c JOIN vehicles v LEFT JOIN fares f ON f.vehicle_id = v.vehicle_id AND c.dt BETWEEN f.fare_start AND f.fare_end WHERE c.dt BETWEEN '2013-01-20' AND '2013-01-28'; +------------+------------+------------+------------+--------+ | dt | vehicle_id | fare_start | fare_end | fare | +------------+------------+------------+------------+--------+ | 2013-01-20 | BAL2018 | NULL | NULL | NULL | | 2013-01-20 | BAL2019 | NULL | NULL | NULL | | 2013-01-20 | BAL2020 | NULL | NULL | NULL | | 2013-01-21 | BAL2018 | 2013-01-21 | 2013-01-26 | 555.00 | | 2013-01-21 | BAL2019 | NULL | NULL | NULL | | 2013-01-21 | BAL2020 | NULL | NULL | NULL | | 2013-01-22 | BAL2018 | 2013-01-21 | 2013-01-26 | 555.00 | | 2013-01-22 | BAL2019 | NULL | NULL | NULL | | 2013-01-22 | BAL2020 | NULL | NULL | NULL | | 2013-01-23 | BAL2018 | 2013-01-21 | 2013-01-26 | 555.00 | | 2013-01-23 | BAL2019 | NULL | NULL | NULL | | 2013-01-23 | BAL2020 | NULL | NULL | NULL | | 2013-01-24 | BAL2018 | 2013-01-21 | 2013-01-26 | 555.00 | | 2013-01-24 | BAL2019 | NULL | NULL | NULL | | 2013-01-24 | BAL2020 | NULL | NULL | NULL | | 2013-01-25 | BAL2018 | 2013-01-21 | 2013-01-26 | 555.00 | | 2013-01-25 | BAL2019 | NULL | NULL | NULL | | 2013-01-25 | BAL2020 | NULL | NULL | NULL | | 2013-01-26 | BAL2018 | 2013-01-21 | 2013-01-26 | 555.00 | | 2013-01-26 | BAL2019 | 2013-01-26 | 2013-01-27 | 211.00 | | 2013-01-26 | BAL2020 | 2013-01-26 | 2013-01-27 | 545.00 | | 2013-01-27 | BAL2018 | 2013-01-27 | 2013-01-28 | 111.00 | | 2013-01-27 | BAL2019 | 2013-01-26 | 2013-01-27 | 211.00 | | 2013-01-27 | BAL2020 | 2013-01-26 | 2013-01-27 | 545.00 | | 2013-01-28 | BAL2018 | 2013-01-27 | 2013-01-28 | 111.00 | | 2013-01-28 | BAL2019 | NULL | NULL | NULL | | 2013-01-28 | BAL2020 | NULL | NULL | NULL | +------------+------------+------------+------------+--------+ </code></pre> <p>27 rows in set (0.00 sec)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload