仅显示重复行的最新日期

时间:2016-10-31 14:07:32

标签: mysql sql

我试图在使用必发的历史数据开始足球比赛之前获得最新赔率。我的数据库如下所示:

Current DB

现在,Betfair的历史数据记录了一天多次的赔率,因此有大量重复数据。到目前为止,我已经设法在启动前获得所选择的正确分数市场的所有赔率。

正如您所看到的,有重复的记录,但我希望只能获得每个正确分数的最新(最长)时间。我已经突出显示了我希望最终查询仅输出的行。

我的SQL语句如下:

SELECT "SCHEDULED_OFF", "FULL_DESCRIPTION", "EVENT", 
   "SELECTION_ID", "SELECTION", "ODDS", "WIN_FLAG", "LATEST_TAKEN"
FROM "bfinf_other_161017to161023_161026123339"
WHERE "FULL_DESCRIPTION" LIKE 'English Soccer/English Premier League%' 
    AND "EVENT" = "Correct Score" 
    AND "LATEST_TAKEN" < "SCHEDULED_OFF"
ORDER BY "FULL_DESCRIPTION" ASC, "SELECTION" ASC, "LATEST_TAKEN" DESC

我已尝试过GROUP BY&#34; FULL_DESCRIPTION&#34;和&#34;选择&#34;但似乎没有发生任何事情。

非常感谢任何帮助。

迈克尔

2 个答案:

答案 0 :(得分:0)

您可以使用基于in子句和subselect的过滤器,最多LATEST_TAKENSELECTION_ID

SELECT `SCHEDULED_OFF`, `FULL_DESCRIPTION`, `EVENT`, `SELECTION_ID`, `SELECTION`, `ODDS`, `WIN_FLAG`, `LATEST_TAKEN`
FROM `bfinf_other_161017to161023_161026123339`
WHERE `FULL_DESCRIPTION` LIKE 'English Soccer/English Premier League%' 
AND `EVENT` = `Correct Score` 
AND `LATEST_TAKEN` IN  ( select MAX(LATEST_TAKEN`  
                       from `bfinf_other_161017to161023_161026123339` 
                       group by `SELECTION_ID`)
ORDER BY `FULL_DESCRIPTION` ASC, `SELECTION` ASC, `LATEST_TAKEN` DESC

不要使用双引号(如果你需要使用反引号)

答案 1 :(得分:0)

添加Where子句谓词,将输出限制为具有最新日期时间的行。使用子查询确定每个选择的最新日期时间。

SELECT "SCHEDULED_OFF", "FULL_DESCRIPTION", "EVENT", 
    "SELECTION_ID", "SELECTION", "ODDS", "WIN_FLAG", "LATEST_TAKEN"
FROM "bfinf_other_161017to161023_161026123339" a
WHERE "FULL_DESCRIPTION" LIKE 'English Soccer/English Premier League%' 
   AND "EVENT" = "Correct Score" 
   AND "LATEST_TAKEN" < "SCHEDULED_OFF"
   and LATEST_TAKEN = 
      (Select Max(LATEST_TAKEN) 
       From bfinf_other_161017to161023_161026123339
       Where Selection_Id = a.Selection_Id)
ORDER BY "FULL_DESCRIPTION" ASC, "SELECTION" ASC, "LATEST_TAKEN" DESC