前段时间我创建了一个脚本,其中包含我们拥有的员工数量和我们拥有的条款数量。当他们使用" Active"时非常简单。和"撤回"作为状态。现在他们更新了状态,包括"持续LOA"和"退休人员"作为附加状态。
我需要知道的是如何更新以下脚本以将这些包含到新参数中。持续的LOA将随着活跃而退出,而退休人员将随着撤回而下降。
MERGE dbo.HRIS_HC_SumTotals AS tgt
USING (
SELECT DATENAME(YEAR, GETDATE()) AS [Year],
SUM(CASE WHEN EmpStatusName = 'Active' THEN 1 ELSE 0 END) AS HistoricalHC,
SUM(CASE WHEN EmpStatusName = 'Withdrawn' AND TermYear = DATENAME(YEAR, GETDATE()) THEN 1 ELSE 0 END) AS NumbOfTermEE
FROM dbo.HRIS_HC_MstrTbl
WHERE ChangeStatus = 'Current'
AND EmpStatusName IN ('Active', 'Withdrawn')
) AS src ON src.[Year] = tgt.[Year]
WHEN MATCHED
THEN UPDATE
SET tgt.HistoricalHC = src.HistoricalHC,
tgt.NumbTermedEmp = src.NumbOfTermEE
WHEN NOT MATCHED BY TARGET
THEN INSERT (
[Year],
HistoricalHC,
NumbTermedEmp
)
VALUES (
src.[Year],
src.HistoricalHC,
src.NumbOfTermEE
);
答案 0 :(得分:0)
你不能只将2行代码更改为'in'语句:
SUM(CASE WHEN EmpStatusName in ('Active','Continuous LOA') THEN 1 ELSE 0 END) AS HistoricalHC,
SUM(CASE WHEN EmpStatusName in ('Withdrawn','Retiree') AND TermYear = DATENAME(YEAR, GETDATE()) THEN 1 ELSE 0 END) AS NumbOfTermEE
并更改Where语句
AND EmpStatusName IN ('Active', 'Withdrawn', 'Continuous LOA','Retiree')