Results 1 to 2 of 2

Thread: Calculating Subtotals Within Groups of Data Using UNION

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Member Transformer's Avatar
    Join Date
    Mar 2012
    Posts
    91
    Rep Power
    13

    Lightbulb Calculating Subtotals Within Groups of Data Using UNION

    UsefulGyaan Has Posted the Following On 05-24-2013 10:04 AM:

    Problem Statement: There is a table named Employee which has department wise employee details as given below. We want to find out gender-wise subtotals and a grand total for each department. Dept Employee_ID Gender IT E123 M IT E124 M IT E125 M IT E126 M IT E127 F IT E128 F HR E129 M HR […]





    Calculating Subtotals Within Groups of Data Using UNION

  2. #2
    Administrator Excel Fox's Avatar
    Join Date
    Mar 2011
    Posts
    1,402
    Rep Power
    10
    The sub-totals appear below each group only because "T...." is larger than "M" or "F". For the sake of the purists, adding an order by would be more apt.

    PHP Code:
    SELECT [CUSTABLE].[Dept], [CUSTABLE].[Gender], [CUSTABLE].[Pop]
    FROM
    (SELECT [Dept], As [Order], [Gender], Count(*)  as Pop
    FROM 
    [Employee]  GROUP BY [Dept], [Gender]
    UNION SELECT [Dept], As [Order], "Total"Count(*) as Pop
    FROM 
    [Employee]  GROUP BY [Dept]) CUSTABLE
    ORDER BY 
    [CUSTABLE].[Dept], [CUSTABLE].[Order]; 
    A dream is not something you see when you are asleep, but something you strive for when you are awake.

    It's usually a bad idea to say that something can't be done.

    The difference between dream and aim, is that one requires soundless sleep to see and the other requires sleepless efforts to achieve

    Join us at Facebook

Similar Threads

  1. Calculating Ratio Within Groups using Self Join
    By Transformer in forum Tips, Tricks & Downloads (No Questions)
    Replies: 0
    Last Post: 05-21-2013, 07:52 PM
  2. Effective way of calculating the difference
    By zzzqinzzz in forum Excel Help
    Replies: 0
    Last Post: 12-13-2012, 03:39 PM
  3. VBA Code to extract subtotals
    By Howardc in forum Excel Help
    Replies: 2
    Last Post: 12-02-2012, 01:15 PM
  4. Email merge unique messages to groups and individuals
    By RagingWahoo in forum Excel Help
    Replies: 3
    Last Post: 10-14-2012, 11:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •