我有两个表记录了月度津贴和OT津贴,这两个表都有Department列和Track列 我需要所有部门的报告,但要与跟踪列“ DL”,“ IDL”分开
我只能执行一个跟踪,但不能在一个查询中与跟踪列结合 这是我的代码
conn.Open
Monthly_Table
;WITH sums AS
(
SELECT
sum(Monthly.Pay) as Total_Monthly,OT.MealAllowance_OT,Monthly.Dept_Id,Monthly.Dept_Name
FROM
[dbo].[SPCM_TX_MonthlyAllowance] as Monthly
left join ( select Period,sum(MealAllowance_OT) MealAllowance_OT,Dept_Id
from [dbo].[SPCM_Cal_OTLog]
where Track = 'IDL'
group by Period,Dept_Id) OT
on Monthly.Dept_Id = OT.Dept_Id
where Monthly.Track = 'IDL'
GROUP BY
Monthly.Dept_Id, Monthly.Dept_Name,MealAllowance_OT,Monthly.Track
)
SELECT
Dept_Id,Dept_Name,Total_Monthly,MealAllowance_OT,Total_Monthly+MealAllowance_OT AS 'GrandTotal'
FROM
sums
order by GrandTotal desc
OT_Table
--------------------------------------------------------------------
EMP_ID | Track | Period | Pay | Dept_Id | Dept_Name |
--------------------------------------------------------------------
1 DL 2019-10-10 400 1A A
2 IDL 2019-10-10 400 1A A
3 DL 2019-10-10 400 1B B
4 IDL 2019-10-10 400 1B B
--------------------------------------------------------------------