I have the below SQL and what I'm trying to do is return the first entry found that is more then 30 days old for each set/store, at the moment the below SQL will give me everything that is 30 days old or more.
My issue is trying to return just one entry for each set/store, there aren't always entries for every day.
v_set_price_history
data below is the returned result from the below SQL at present.
CREATE VIEW `v_set_price_history` AS
SELECT `spr`.`spr_id` AS `vph_id`
, `spr`.`spr_set_id` AS `vph_set_id`
, `spr`.`spr_price` AS `vph_price`
, `spr`.`spr_str_id` AS `vph_store`
, DATE(`spr`.`spr_added`) AS `vph_date`
, DATE(`spr`.`spr_last_update`) AS `vph_update`
FROM `td_set_price` `spr`
LEFT JOIN `td_set` `set` ON `spr`.`spr_set_id` = `set`.`set_id`
WHERE `spr`.`spr_added`< DATE_SUB(NOW(), INTERVAL 30 DAY)
AND `spr`.`spr_id` = (
SELECT MAX(`spr2`.`spr_id`)
FROM `td_set_price` `spr2`
WHERE `spr2`.`spr_set_id` = `set`.`set_id`
AND DATE(`spr2`.`spr_added`) = DATE(`spr`.`spr_added`)
)
AND `spr`.`spr_set_id` IN (18, 19)
AND `spr`.`spr_status` = 1
ORDER BY `vph_date` DESC;
v_set_price_history
vph_id vph_set_id vph_price vph_store vph_date vph_update
7076 19 99.99 1 2016-05-17
6994 18 129.99 1 2016-05-05 2016-05-09
6201 19 114.99 1 2016-04-27 2016-05-05
5175 19 140.00 7 2016-04-03 2016-06-12
4219 19 112.00 7 2016-03-23 2016-03-29
3962 18 149.99 8 2016-03-21 2016-07-05
3040 19 140.00 7 2016-03-04 2016-03-22
8329 19 129.99 10 2016-02-28 2016-06-13
1744 19 129.99 2 2016-02-12 2016-03-22
1722 19 129.95 4 2016-02-10 2016-07-05
1436 18 134.99 2 2016-02-04
1149 19 129.99 4 2016-01-27 2016-03-18
430 18 123.49 2 2016-01-15 2016-03-18