Get back the second latest date per ID instead of the latest

廉价感情. 提交于 2019-12-06 07:28:40

Using any other day/month than 1/1 will foul up the date string comparisons; you should use a date format like yyyy-mm-dd instead.

Assuming that you have working date comparisons, you can either remove all the maximum rows with a compound query, then group over the rest:

SELECT searchID, hit, MAX(time)
FROM (SELECT searchID, hit, time
      FROM results
      EXCEPT
      SELECT searchID, hit, MAX(time)
      FROM results
      GROUP BY searchID)
GROUP BY searchID

or you can check, before the grouping, that the time is not the largest time in the group:

SELECT searchID, hit, MAX(time)
FROM results
WHERE time < (SELECT MAX(time)
              FROM results AS r2
              WHERE r2.searchID = results.searchID)
GROUP BY searchID
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!