I am trying to link a few tables. In most of the tables my info is not time based. But in one of my tables it is.
In this "time based" table I need to fetch the price based on time and date—meaning the price will be "valid" after a certain time defined in the DB. So stuff like sales on a certain date could be accomplished.
And this needs to work with all the LEFT JOIN
ed tables.
The SQL that works without the time clause is below:
SELECT
conf_mno.mcc AS mcc,
conf_mno.mnc AS mnc,
conf_mno.operator_name AS operator_name,
conf_countries.country_prefix AS country_prefix,
conf_countries.country_name AS country_name,
conf_mno_prefix.prefix AS operator_prefix,
conf_mno_price.currency AS currency,
conf_mno_price.price AS price
FROM conf_mno
LEFT JOIN conf_mno_prefix ON conf_mno.mcc = conf_mno_prefix.mcc AND conf_mno.mnc = conf_mno_prefix.mnc
LEFT JOIN conf_countries ON conf_mno.mcc = conf_countries.mcc
LEFT JOIN conf_mno_price ON conf_mno.mcc = conf_mno_price.mcc AND conf_mno.mnc = conf_mno_price.mnc
GROUP BY mcc, mnc, operator_name, country_prefix, country_name
However this only fetches the price which is at the top of the table. And I need it to fetch the one on the bottom by using WHERE conf_mno_price.valid_timestamp <= UTC_TIMESTAMP()
and ORDER BY conf_mno_price.valid_timestamp DESC
in order to get the one at the bottom.
I've tried a few approaches where I'm using a subquery of SELECT
within SELECT
and adding this...it does fetch the last and valid price of the first operator however it applies it to all the other operators.
conf_countries
+-------------------------------------------+
| mcc | country_prefix | country_name |
+-------+------------------+----------------+
| 222 | 39 | Italy |
+-------------------------------------------+
conf_mno
+---------------------------------+
| mcc | mnc | operator_name |
+-------+-------+-----------------+
| 222 | 01 | Telekom1 |
+---------------------------------+
| 222 | 02 | Telekom2 |
+---------------------------------+
| 222 | 03 | Telekom3 |
+---------------------------------+
| 222 | 04 | Telekom4 |
+---------------------------------+
| 222 | 07 | Telekom7 |
+---------------------------------+
| 222 | 10 | Telekom10 |
+---------------------------------+
conf_mno_prefix
+--------------------------+
| mcc | mnc | prefix |
+--------------------------+
| 222 | 01 | 70 |
+--------------------------+
| 222 | 01 | 71 |
+--------------------------+
| 222 | 01 | 73 |
+--------------------------+
| 222 | 04 | 78 |
+--------------------------+
| 222 | 07 | 79 |
+--------------------------+
conf_mno_price
+-------+-------+------------+---------+-----------------------+
| mcc | mnc | currency | price | valid_timestamp |
+-------+-------+------------+---------+-----------------------+
| 222 | 01 | EUR | 0.020 | 2016-11-11 00:00:00 |
+-------+-------+------------+---------+-----------------------+
| 222 | 01 | EUR | 0.010 | 2016-11-20 00:00:00 |
+-------+-------+------------+---------+-----------------------+
| 222 | 01 | EUR | 0.015 | 2016-12-03 00:00:00 |
+-------+-------+------------+---------+-----------------------+
| 222 | 01 | EUR | 0.020 | 2016-12-05 00:00:00 |
+-------+-------+------------+---------+-----------------------+
| 222 | 01 | EUR | 0.019 | 2016-12-10 00:00:00 |
+-------+-------+------------+---------+-----------------------+
| 222 | 04 | EUR | 0.025 | 2016-11-21 00:00:00 |
+-------+-------+------------+---------+-----------------------+
mcc being the county id and mnc is the operator code within that country
The output should be something like this:
stdClass Object ( [mcc] => 222 [mnc] => 1 [operator_name] => Telekom1 [country_prefix] => 39 [country_name] => Italy [operator_prefix] => 70, 71, 73 [currency] => EUR [price] => 0.010)
stdClass Object ( [mcc] => 222 [mnc] => 4 [operator_name] => Telekom4 [country_prefix] => 39 [country_name] => Italy [operator_prefix] => 78 [currency] => EUR [price] => 0.025)
0.010 being the last valid price for operator 1 and 0.025 being the last valid price for operator 4 — assuming the current date is 2016-12-02 00:01:00
What would be a way to get all the info linked info like country name and operator name and prefixes of the operators as well as the latest price for each operator?
Is this even possible?
I can easily solve this by doing a second query, but as this is something which is intended to run all the time and it will be taxing on my server so trying to avoid it.
Sample data for conf_mno_price
:
INSERT INTO `conf_mno_price` (`unq_id`, `mcc`, `mnc`, `currency`, `price`, `valid_timestamp`, `created_timestamp`, `modified_timestamp`) VALUES
(1, 222, 1, 'EUR', '0.01000000', '2016-08-28 22:00:00', '2016-12-03 18:52:26', '2016-12-03 18:52:26'),
(2, 222, 2, 'EUR', '0.00800000', '2016-08-28 22:00:00', '2016-12-03 18:52:57', '2016-12-03 18:52:57'),
(3, 222, 4, 'EUR', '0.01100000', '2016-08-28 23:00:00', '2016-12-03 18:59:38', '2016-12-03 18:59:38'),
(4, 222, 3, 'EUR', '0.01200000', '2016-08-28 22:00:00', '2016-12-03 18:53:07', '2016-12-03 18:53:07'),
(5, 222, 1, 'EUR', '0.01600000', '2016-09-04 22:00:00', '2016-12-03 18:54:11', '2016-12-03 18:54:11'),
(6, 222, 2, 'EUR', '0.01000000', '2016-09-04 22:00:00', '2016-12-03 18:54:27', '2016-12-03 18:54:27'),
(7, 222, 1, 'EUR', '0.00800000', '2016-09-11 22:00:00', '2016-12-03 18:55:31', '2016-12-03 18:55:31'),
(8, 222, 4, 'EUR', '0.01100000', '2016-09-18 22:00:00', '2016-12-03 18:56:17', '2016-12-03 18:56:17'),
(9, 222, 2, 'EUR', '0.00900000', '2016-09-25 22:00:00', '2016-12-03 18:56:54', '2016-12-03 18:56:54'),
(10, 222, 3, 'EUR', '0.00800000', '2016-09-25 22:00:00', '2016-12-03 18:57:08', '2016-12-03 18:57:08'),
(11, 222, 1, 'EUR', '0.00700000', '2016-10-02 22:00:00', '2016-12-03 18:57:54', '2016-12-03 18:57:54'),
(12, 222, 3, 'EUR', '0.00600000', '2016-10-30 23:00:00', '2016-12-03 19:03:12', '2016-12-03 19:03:12'),
(13, 222, 4, 'EUR', '0.01000000', '2016-10-30 23:00:00', '2016-12-03 18:59:38', '2016-12-03 18:59:38');