Mysql – Track price changes on a daily basis

MySQL

I am trying to track price changes on a daily basis. I have 2 tables Stores and Prices.

Table Stores columns:

  • s_id
  • company
  • address
  • city
  • state
  • zip
  • phone

Table Prices columns:

  • ref
  • storeid
  • price
  • mgr
  • datetime

NOTE: storeid in Prices is a reference to s_id in Stores

What I would like to do is run a query to find all prices from yesterday and compare them to today to see if they have changed. Please understand that I am filing in for the database admin for any advice you can offer.

Best Answer

Would be something like:

select  p1.ref, p1.storeid, p1.price, p2.price
from    prices p1 
join prices p2 on p1.ref = p2.ref
where   trunc(p1.datetime) = trunc(sysdate)
and     trunc(p2.datetime) = trunc(sysdate-1)
and     p1.price <> p2.price;

You can add Stores to get store information if you need. I do not know which DB you use so I gave a solution for Oracle. The trunc function gives you the date part of a date.