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 10But on all my tests, the number of bytes for user1@test.net is being summed
user1 20
user2 10username开发者_开发问答 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, MarcelloYour 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
精彩评论