ROLLUP to calculate Month total for all Categories

  • Good Afternoon,

    I have built a table with four columns:  Period(Month), Category, Numerator, Denominator. Period(Month) is an Alias name, and Numerator and Denominator are calculated columns:

    SELECT

    ,convert(NVARCHAR,Date,3) as 'Period(Month)'

    ,[Category]

    , sum([X]) as 'Numerator'

    ,SUM([Y]) as 'Denominator'

    FROM Tabl1

    GROUP BY [Category], convert(NVARCHAR,Date,3),CAST(Date AS date)

    ORDER BY convert(NVARCHAR,Date,3) desc

    Now I want an additional record added at the end of all records for each 'Period(Month)' containing the total Numerators and Denominators for that month (but must NOT be broken down by Category totals within each month).

    I assume that I need ROLLUP function for this purpose, but I was having problems ROLLUP in achieving the desired end result caused by 1) The Totals were being tallied for each Category for each month and 2) The 'Aliased' columns.

    Kindly advise.

     

     

     

     

    • This topic was modified 4 weeks, 1 days ago by Reh23.
    • This topic was modified 4 weeks, 1 days ago by Reh23.
    • This topic was modified 4 weeks, 1 days ago by Reh23.
    • This topic was modified 4 weeks, 1 days ago by Reh23.
    • This topic was modified 4 weeks ago by Reh23.
  • Thanks for posting your issue and hopefully someone will answer soon.

    This is an automated bump to increase visibility of your question.

  • Please provide some sample data and expected results in an easily consumable way. Like an sql script we can execute in a sandbox

  • Thanks...Please find attached...

    Attachments:
    You must be logged in to view attached files.
  • Most of us aren't going to open an XLS from you. What you should do is convert the XLS into a create table statement with inserts and then give a test result of what you're looking for.

    As an idea, you might use ROLLUP in a CTE and then select all but the items you don't want.

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply