我有以下存储过程:
select
employeename,
ISNULL(SUM(CASE WHEN datepart(mm,cs.scheduledate) = 1 THEN cs.amount ELSE 0 END), 0) as january,
ISNULL(SUM(CASE WHEN datepart(mm,cs.scheduledate) = 2 THEN cs.amount ELSE 0 END), 0) as february,
ISNULL(SUM(CASE WHEN datepart(mm,cs.scheduledate) = 3 THEN cs.amount ELSE 0 END), 0) as march,
ISNULL(SUM(CASE WHEN datepart(mm,cs.scheduledate) = 4 THEN cs.amount ELSE 0 END), 0) as april,
ISNULL(SUM(CASE WHEN datepart(mm,cs.scheduledate) = 5 THEN cs.amount ELSE 0 END), 0) as may,
ISNULL(SUM(CASE WHEN datepart(mm,cs.scheduledate) = 6 THEN cs.amount ELSE 0 END), 0) as june,
ISNULL(SUM(CASE WHEN datepart(mm,cs.scheduledate) = 7 THEN cs.amount ELSE 0 END), 0) as july,
ISNULL(SUM(CASE WHEN datepart(mm,cs.scheduledate) = 8 THEN cs.amount ELSE 0 END), 0) as august,
ISNULL(SUM(CASE WHEN datepart(mm,cs.scheduledate) = 9 THEN cs.amount ELSE 0 END), 0) as september,
ISNULL(SUM(CASE WHEN datepart(mm,cs.scheduledate) = 10 THEN cs.amount ELSE 0 END), 0) as october,
ISNULL(SUM(CASE WHEN datepart(mm,cs.scheduledate) = 11 THEN cs.amount ELSE 0 END), 0) as november,
ISNULL(SUM(CASE WHEN datepart(mm,cs.scheduledate) = 12 THEN cs.amount ELSE 0 END), 0) as december,
ISNULL(SUM(cs.amount),0) as a_cds
from
statements cs
join
employee mc on mc.employeeid = cs.employeeid
where
cs.chargetype = @chargetype
-- if @accumulate = 1 then SUM all statement amounts from old months
and ((datepart(mm, cs.scheduledate) <= @month and @accumulate = 1)
or (datepart(mm, cs.scheduledate) = @month and @accumulate = 0))
-- if selection is Biweekly (Q) then filter by that
and ((@chargetype = 'Q' and @biweeklypart = 1 and day(cs.scheduledate) = 15) or
(@chargetype = 'Q' and @biweeklypart = 2 and day(cs.scheduledate) <> 15) or
(@chargetype <> 'Q')
)
group by
employeename
使用此SP但Accumulate
过滤器,一切都很好用。当@chargetype
不是'问'时,仅适用。
让我们看看以下示例数据:
对于员工15:
我应该获得所有先前声明记录的SUM
,包括02/28/2018。如上所述,这在@chargetype ='M'时工作正常。
有任何线索吗?