Technical
Another Way To Deal With Dups

My “smart” former co(maybe because he was so smart)-worker implemented data saving procedure which did not care whether data was previously saved, thus creating silly dupes if storing procedure was run twice for the same non-defined composite keys(date,hour). So this is what you’d get in plain yaml on selecting particular data.


- !ruby/object:BgDamRtPrice
attributes:
bg_location_id: "1" 
hour: "8" 
dam_price: "95.5" 
rt_price: "104.13" 
id: "35448" 
Date: 2008-05-10 00:00:00
- !ruby/object:BgDamRtPrice
attributes:
bg_location_id: "1" 
hour: "8" 
dam_price: "95.5" 
rt_price: "104.13" 
id: "35640" 
Date: 2008-05-10 00:00:00

The problem here is that I needed to get dam/rt price each day hourly, so the only unique key here was an auto incremeneted id ( thank G-d he added that) so my very good friend Alex Shipov
suggested really genios way of inner joining using max id which would make sure that only the second record of the same date and hour would be selected. Our Final querry looked as following


SELECT *
FROM bg_dam_rt_prices
INNER JOIN
(SELECT MAX(id) AS LimitId FROM bg_dam_rt_prices GROUP BY date, hour,bg_location_id) AS tblInner
ON bg_dam_rt_prices.id = tblInner.LimitId