store case result sql 1

store case result sql

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

Here is what the above code is Doing:
1. Declaring a variable @val and setting it to 0.
2. Selecting the SUM(PROFIT) for the current month and year.
3. Printing the value of @val.

Similar Posts