id | userid | total_points_spent
1 | 1 | 10
2 | 2 | 15
3 | 2 | 50
4 | 3 | 5
5 | 1 | 15
根据上表,我首先要删除userid
的重复项,保留最大total_points_spent
的行,如下所示:
id | userid | total_points_spent
3 | 2 | 50
4 | 3 | 5
5 | 1 | 15
然后我想总结total_points_spent
的值,这将是最简单的部分,从而产生70
。
答案 0 :(得分:2)
我不确定“删除”是指删除或选择。以下是分别仅选择max totalpointspend记录的查询。
SELECT tblA.*
FROM ( SELECT userid, MAX(totalpointspend) AS maxtotal
FROM tblA
GROUP BY userid ) AS dt
INNER JOIN tblA
ON tblA.userid = dt.userid
AND tblA.totalpointspend = dt.maxtotal
ORDER BY tblA.userid