开发者

Mysql distinct with sum

开发者 https://www.devze.com 2023-02-14 19:09 出处:网络
I am having issues to SUM the bytes column using distinct on messageid. Based on the sample table I need to get the following result:

I am having issues to SUM the bytes column using distinct on 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)

user2 10

But on all my tests, the number of bytes for user1@test.net is being summed

user1 20

user2 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
0

精彩评论

暂无评论...
验证码 换一张
取 消