Last active
August 29, 2015 14:13
-
-
Save padak/bde404006db8c89a0dbf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#preparing data | |
DROP TABLE IF EXISTS `calendar`; | |
CREATE TABLE `calendar` ( | |
`date` date NOT NULL, | |
PRIMARY KEY (`date`) | |
) ENGINE=InnoDB DEFAULT CHARSET=utf8; | |
INSERT INTO `calendar` (`date`) VALUES ('2015-01-01'),('2015-01-02'),('2015-01-03'),('2015-01-04'),('2015-01-05'),('2015-01-06'),('2015-01-07'),('2015-01-08'),('2015-01-09'),('2015-01-10'); | |
DROP TABLE IF EXISTS `prices`; | |
CREATE TABLE `prices` ( | |
`date` date NOT NULL, | |
`price` varchar(45) DEFAULT NULL, | |
PRIMARY KEY (`date`) | |
) ENGINE=InnoDB DEFAULT CHARSET=utf8; | |
INSERT INTO `prices` (`date`, `price`) VALUES ('2015-01-01','1'),('2015-01-02','2'),('2015-01-08','5'),('2015-01-10','6'); | |
#filling last 'dummy' row | |
INSERT INTO `prices` (`date`, `price`) | |
SELECT DATE_ADD(MAX(date), INTERVAL 1 DAY) as date, '0' AS price from `prices`; | |
#getting results | |
SELECT * FROM calendar ca | |
LEFT JOIN | |
(SELECT date AS fdate, price, MIN(ndate) tdate FROM | |
(SELECT c1.date, c1.price, c2.date AS ndate FROM prices AS c1 JOIN prices AS c2 ON c1.date < c2.date) AS subq | |
GROUP BY date, price | |
) AS subq2 | |
ON ca.date >= fdate AND ca.date < tdate; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment