SQL: How to get the total payment for each month and its monthly total

You seem to want:

SELECT MONTH(<datecol>),
       (CASE WHEN MONTH(<datecol>) IS NULL AND Brand IS NULL
             THEN COALESCE(Brand, 'Grand Total')
             WHEN Brand IS NULL THEN 'Month Total'
             ELSE Brand
        END) AS Brand,
       SUM(Price) as TotalAmount
FROM Purchase_Items
GROUP BY ROLLUP (MONTH(<datecol>), Brand)

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top