Efficient use of Sum in query within the same id

noname :

I am learning sql query.

Here is my table simplified

╔════╦══════════════╦══════════════╦════════╦
║ id ║ user_id      ║ department_id║ salary ║
╠════╬══════════════╬══════════════╬════════╬
║  1 ║ 1            ║    3         ║  100   ║
║  2 ║ 2            ║    3         ║   50   ║
║  3 ║ 1            ║    3         ║   30   ║
║  4 ║ 2            ║    3         ║   20   ║
║  5 ║ 2            ║    3         ║   20   ║
╚════╩══════════════╩══════════════╩════════╩

Here is what I want to have below

╦══════════════╦══════════════╦════════╦
║ user_id      ║ department_id║ salary ║
╠══════════════╬══════════════╬════════╬
║ 1            ║    3         ║  130   ║
║ 2            ║    3         ║   90   ║
╚══════════════╩══════════════╩════════╩

I want to add user salary if they are in the same department.

I have nooo ideas how to start.

Does anyone have good feedback that I can start with?

Thank you in advance

Nicole Douglas :

Try this query:

Select user_id, department_id, sum(salary) from table

Group by user_id, department_id

In the select clause, you have the columns you wish to select and your group by clause contains the grouping.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=291258&siteId=1