简体   繁体   中英

SQL statement select most recent date and group per id

I am trying to retrieve the most recent 'topic 1 value' for each detailsID for the most recent date per detailsID.

I was thinking something along the lines of:

SELECT detailsID,`Topic 1 Scores`, MAX(Date) 
FROM Information.scores 
WHERE `Topic 1 Scores` IS NOT NULL 
GROUP BY `detailsID`,`Topic 1 Scores`

Is printing;

detailsID, Topic 1 Scores, MAX(Date)

2 0 26/09/2017

2 45 26/09/2017

2 100 26/09/2017

3 30 25/09/2017

3 80 14/10/2017

Rather than actually selecting the most recent date per detailsID?

A solution like this can be found in this answer in the linked question.

SELECT t1.detailsID, t1.`Topic 1 Scores`
FROM Information.scores AS t1
JOIN (SELECT detailsID, MAX(date) AS maxDate
      FROM Information.scores
      GROUP BY detailsID) AS t2 
ON t1.detailsID = t2.detailsID AND t1.date = t2.maxDate

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM