0
select distinct userid, SUM(device)  
from table 
where userid != '' 
group by userid, device;

 userid  | sum 
---------+-----
 a       |  12
 b       |   1
 c       |  10
 c       |  78

why there is a duplicate userid without no sum calculated. All devices have integer values

1
  • is column "device" of a numerical type? Commented Mar 16, 2021 at 0:48

1 Answer 1

3

Because you are grouping by both userid and device. Remove the device from the GROUP BY.

Your query should look like:

select userid, SUM (device)  
from table 
where userid != '' 
group by userid;

Incidentally, I removed DISTINCT since you are already aggregating.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.