如何解决来自两个select语句的结果总和
试试这个
SELECT perceptionistID, SSNLastFour, SUM(CommissionPay) CommissionPay,
SUM(PTOPay) PTOPay, SUM(HolidayPay) HolidayPay, SUM(Overtime) Overtime, SUM(TotalPay) TotalPay
FROM
(
SELECT perceptionistID, SSNLastFour, CommissionPay,
PTOPay, HolidayPay, Overtime, TotalPay
FROM [dbo].fncalculateCommissionForWeekOf(@MondayOfCurrentWeek)
UNION ALL
-- Need to get the following week's data and sum the two together
SELECT perceptionistID, SSNLastFour, CommissionPay,
PTOPay, HolidayPay, Overtime, TotalPay
FROM [dbo].fncalculateCommissionForWeekOf(@MondayOfFollowingWeek)
) t
GROUP BY perceptionistID, SSNLastFour
解决方法
我们的员工每周获得佣金,由于奖金结构,我必须计算两个单独的星期,然后将两个星期相加。
我有以下SQL语句,它获得两个单独的星期结果
SELECT PerceptionistID,SSNLastFour,CommissionPay,PTOPay,HolidayPay,Overtime,TotalPay
FROM [dbo].fnCalculateCommissionForWeekOf(@MondayOfCurrentWeek)
UNION
-- Need to get the following week's data and sum the two together
SELECT PerceptionistID,TotalPay
FROM [dbo].fnCalculateCommissionForWeekOf(@MondayOfFollowingWeek)
这为我获取了我需要的数据,但我想将两个结果合并到一个表中,这些表具有相同的列,但是将某些列加在一起(CommissionPay,PTOPay,HolidayPay,Overtime,TotalPay)。做这个的最好方式是什么?我正在使用SQL
Server 2008 R2。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。