我有下面的PostgreSQL表,这是一个巨大的表的小样本
employee
emp_id | salary | rating | increment_year
--------------------------------------
28 3000 1 2023
25 2000 2 2023
30 1500 1 2021
28 1000 3 2022
32 1200 1 2023
23 1550 1 2023
30 2500 3 2022
30 3000 2 2023
我想要执行一个PostgreSQL查询
group by emp_id
,返回emp_id, sum(salary), get rating value based on latest increment_year, latest increment_year that was selected to get rating value
查询结果示例
emp_id | sum_salary | selected_rating_on_latest_increment_year | selected_increment_year_to_get_rating
------------------------------------------------------------------------------------------------------
28 4000 1 2023
value goes on for every emp_id
由于某些预先存在的情况,我必须使用group by.所以,如果可能的话,分组进行是我必须做的.