1
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

4

1 回答 1

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  
于 2012-07-06T01:18:34.717 回答