SQL for price difference calculation -
i've got 2 tables i'm trying grab data from. first 'titles' table, represents product information (name, unique id, etc). second 'prices' table collects price information various currencies (each product can have multiple historic entries in prices table).
i've written long-winded sql statement grab latest price changes across products, there issues more experienced users able me out with:
                select                      t.id,                      t.name,                      t.type,                      p.value,                     (select                          value                                               prices                                               prices.id = p.id ,                          prices.country='us' ,                          prices.timestamp < p.timestamp                     order                          prices.timestamp desc                      limit 1) last_value                                     prices p                 inner join                     titles t                 on                     t.row_id = p.id                                     p.country = 'us' ,                     (select                          value                                               prices                                               prices.id = p.id ,                          prices.country='us' ,                          prices.timestamp < p.timestamp                     order                          prices.timestamp desc                      limit 1) not null                 group                     t.id                 order                     p.timestamp desc,                     last_value desc                 limit 0, 25"   the first issue i've run that, while query works, titles appear multiple times in same listing. while expected, i'd ideally latest price change displayed title. solve this, tried grouping titles 'id' (note the: group t.id above). unfortunately, while i'd expect group respect order (which orders latest price changes in desc order), results seem remove latest changes , show group'd titles earlier price values.
secondly, there better way grab last price of each item (currently grab current value, , run subquery grab 'last_value' - represents value before current price change). @ moment run 2 subqueries: 1 grab second last known price, , again ensure previous price exists in database (otherwise there's no point in listing title having price change).
any appreciated!
how this:
select titles.id, titles.name, titles.type, prices.value, max(prices.timestamp) titles, prices prices.row_id = titles.id , prices.country='us';   mind you, don't have mysql installed couldn't try query.
[edit:] think won't work 'cause it'll display last price entered items because it'll choose highest timestamp prices table, maybe group do, i'm sleepy , can't think straight;
[edit2:] how this: (select max(report_run_date) maxdate, report_name report_history group report_name) maxresults
select titles.id, titles.name, titles.type, prices.value,     (select max(prices.timestamp) maxtimestamp prices group prices.row_id) titles, prices prices.row_id = titles.id , prices.country='us';      
Comments
Post a Comment