简体   繁体   English

在我的SQL中使用Distinct和Sum

[英]Using Distinct and Sum in my sql

below is the table i have created, my goal is to remove the duplicate results then add the value up. 下面是我创建的表,我的目标是删除重复的结果,然后将值加起来。

|  username   |   value
|    Bobby    |     5
|    Bobby    |     5
|    Bobby    |     7

This is the result I am looking for 这是我正在寻找的结果

|  username   |   value
|    Bobby    |     12

How can I achieve this? 我该如何实现?

Try this 尝试这个

select username, sum(value) from 
(select distinct username,value from mytbl) as tmp  group by username
SELECT SUM(DISTINCT value) FROM TableName Group By UserName

please try this 请尝试这个

 select username,sum(value) from 
 (
   select username,value from tbl group by username,value
 )data
  group by name

Here's simpler way 这是更简单的方法

SELECT distinct(username), sum(distinct value) AS Value from temp1

It works for sure 它肯定有效

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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