简体   繁体   English

MySQL与总和不同

[英]Mysql distinct with sum

I am having issues to SUM the bytes column using distinct on messageid. 我在使用messageid上不同的字节列求和时遇到问题。 Based on the sample table I need to get the following result: 根据样本表,我需要得到以下结果:

user1 10 (I need to use substring_index to remove the domain from the user) user1 10(我需要使用substring_index从用户中删除域)
user2 10 用户2 10

But on all my tests, the number of bytes for user1@test.net is being summed 但是在我所有的测试中,user1@test.net的字节数是相加的

user1 20 用户1 20
user2 10 用户2 10

username          messageid                                              bytes  
user1@test.net  FD5414C0828B0C498DD655CDA90FFCA83D2D088D67@test.net    10   
user1@test.net  FD5414C0828B0C498DD655CDA90FFCA83D2D088D67@test.net    10   
user2@test.net  XX5414C0828B0C498DD655CDA90FFCA83D2D088D77@test2.net   5    
user2@test.net  YY5414C0828B0C498DD655CDA90FFCA83D2D088D77@test2.net   5

Any idea? 任何想法?

Thanks in advance for your time and help. 在此先感谢您的时间和帮助。
Cheers, 干杯,
Marcello 马切洛

Your data has duplicate records which you need to get rid of. 您的数据有重复的记录,您需要删除它们。 You can use the query below - it has an inner select which gets rid of the duplicates, and then it sums up all the bytes according to user. 您可以使用下面的查询-它具有内部选择,可以消除重复项,然后根据用户对所有字节求和。

I did not go into the substring issue as I assume you handled it already (comment if you want me to add it...) 我没有讨论过子字符串问题,因为我假设您已经处理过了(如果要我添加它,请注意...)

SELECT 
  t.username, SUM(t.bytes)
FROM
( SELECT username, messageid, bytes 
  FROM my_table
  GROUP BY username, messageid) as t
GROUP BY t.username

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

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