SQL检索最近的记录

时间:2017-10-14 21:16:14

标签: sql

我想检索每个detailsID的最新得分(不包括空)(按日期排序)的TOPIC 1分数(这里只有详细IDID 2和3,因此只返回两个结果)

4 个答案:

答案 0 :(得分:0)

Topic 1 Scores个详细信息ID GROUP BY主题1分数中摆脱,怎么办?

答案 1 :(得分:0)

假设SQL Server:

SELECT  
    ROW_NUMBER() OVER (PARTITION BY detailsID ORDER BY Date DESC) AS RowNumber,
    detailsID, Date, Topic 1 Scores
FROM 
    Information.scores

答案 2 :(得分:0)

使用子查询获取最大值,然后加入它。

SELECT a.detailsID,`Topic 1 Scores`, a.Date 
FROM Information.scores AS a
JOIN (SELECT detailsID, MAX(Date) "MaxDate"
      FROM Information.scores
      WHERE `Topic 1 Scores` IS NOT NULL
      GROUP BY detailsID) Maxes
ON a.detailsID = Maxes.detailsID
   AND a.Date = Maxes.MaxDate
WHERE `Topic 1 Scores` IS NOT NULL

答案 3 :(得分:-1)

尝试

SELECT detailsID,`Topic 1 Scores`, MAX(Date) as "Date" GROUP BY "Date"