尝试比较当前年度佣金与上一年度的总和,但无法创建内部联接。如果thisYear更高,我会将growth_incentive字段更新为'。05'目前有类似的东西可以获取数据,但我感觉效率低下
SELECT a.emp_no, SUM(a.total_commission) AS ThisYearComm
,(select SUM(total_commission) AS LastYearComm
from tbl_comm_medmon_employees_stats
where emp_no = a.emp_no
and year(getdate())-1 = comm_year) as lastyear
FROM tbl_comm_medmon_employees_stats a
where year(getdate()) = a.comm_year
GROUP BY a.emp_no, a.comm_year
答案 0 :(得分:1)
如果我理解你的桌子,你就不需要加入了。尝试使用条件聚合,如下所示:
SELECT a.emp_no
, SUM(case when year(getdate()) = a.comm_year then a.total_commission end) AS ThisYearComm
, SUM(case when year(getdate()) - 1 = a.comm_year then a.total_commission end) AS LastYearComm
FROM tbl_comm_medmon_employees_stats a
where a.comm_year in (year(getdate()), year(getdate()) - 1)
GROUP BY a.emp_no
答案 1 :(得分:0)
您可以尝试使用条件SUM
代替
SELECT a.emp_no,
SUM(CASE WHEN year(getdate())-1 = a.comm_year THEN a.total_commission
ELSE 0
END) [LastYear],
SUM(CASE WHEN year(getdate()) = a.comm_year THEN a.total_commission
ELSE 0
END) [ThisYearComm]
FROM tbl_comm_medmon_employees_stats a
GROUP BY a.emp_no