sql - Get the highest odds from the last update -
i have these tables in postgresql database:
bookmakers
----------------------- | id | name | ----------------------- | 1 | unibet | ----------------------- | 2 | 888 | ----------------------- odds
--------------------------------------------------------------------- | id | odds_type | odds_index | bookmaker_id | created_at | --------------------------------------------------------------------- | 1 | 1 | 1.55 | 1 | 2012-06-02 10:30 | --------------------------------------------------------------------- | 2 | 2 | 3.22 | 2 | 2012-06-02 10:30 | --------------------------------------------------------------------- | 3 | x | 3.00 | 1 | 2012-06-02 10:30 | --------------------------------------------------------------------- | 4 | 2 | 1.25 | 1 | 2012-05-27 09:30 | --------------------------------------------------------------------- | 5 | 1 | 2.30 | 2 | 2012-05-27 09:30 | --------------------------------------------------------------------- | 6 | x | 2.00 | 2 | 2012-05-27 09:30 | --------------------------------------------------------------------- what trying query following:
give me 1/x/2 odds latest update (created_at) bookmakers , last update, give me highest odds each odds_type ('1', '2', 'x').
on website display them as:
best odds right now: 1 | x | 2 -------------------- 2.30 | 3.00 | 3.22 i have first latest, because odds update yesterday no longer valid. last update, have - in case - 2 odds 2 different bookmakers, need best 1 type '1','2','x'.
pseudo sql like:
select max(odds_index) odds_type = '1' order created_at desc, odds_index desc but doesn't work, because latest odds (and not highest/best latest)
i hope i'm making sense.
subqueries rescue!
select o1.odds_type, max(o1.odds_index) odds o1 inner join (select odds_type, max(created_at) created_at odds group odds_type) o2 on o1.odds_type = o2.odds_type , o1.created_at = o2.created_at group o1.odds_type sqlfiddle: http://sqlfiddle.com/#!3/47df4/3
Comments
Post a Comment