📅  最后修改于: 2022-03-11 15:05:15.079000             🧑  作者: Mango
declare
@val int = 0;
select
@val = CASE
WHEN MONTH([DATE]) = MONTH(getdate()) AND
YEAR([DATE]) = YEAR(getdate())
THEN SUM(PROFIT)
OVER (PARTITION BY [Name],
MONTH([DATE]), YEAR([DATE]))
ELSE 0
END
FROM table_name
WHERE [Name] = @Name AND
MONTH([DATE]) = @Month AND
YEAR([DATE]) = @Year
print @val