简体   繁体   中英

Mysql distinct with sum

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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