Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6730753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:26:47+00:00 2026-05-26T10:26:47+00:00

I need to calculate subtotals in a separate line, but I cannot use ROLLUP

  • 0

I need to calculate subtotals in a separate line, but I cannot use ROLLUP or CUBE (because not allowed). I use SQL server 2008.
There is an other way to get follow results? thanks in advance!

SELECT deptno, empno, SUM (sal)
2 FROM emp_test
3 GROUP BY ROLLUP (deptno, empno);

DEPTNO      EMPNO   SUM(SAL)

    10       7782      20000
    10       7839      10000
    10                 30000
    20       7566       5950
    20       7788       6000
    20       7902       6000
    20                 17950
    30       7698      20000
    30       7699      20000
    30                 40000
                       87950
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T10:26:47+00:00Added an answer on May 26, 2026 at 10:26 am

    You might want to try is using the GROUPING() function on the column. This function returns 1 if it is part of the GROUPING SETS subtotals, and 0 if it is a regular column. http://technet.microsoft.com/en-us/library/ms178544(SQL.90).aspx

    I included the sample data I tested with. Remove the first WITH emp_test AS () when you use the select statement.

    My test data:

    WITH emp_test AS
    (
       SELECT 10 AS DEPTNO, 7782 AS EMPNO, 20000 AS sal
       UNION ALL SELECT 10, 7839, 10000
       UNION ALL SELECT 20, 7566, 5950
       UNION ALL SELECT 20, 7788, 6000
    )
    

    Actual query below:

    SELECT deptno, empno
       , CASE
          WHEN GROUPING(empNo) = 1 THEN null
          ELSE SUM(sal)
         END as sum_salary
       , CASE
          WHEN GROUPING(empno) = 1 THEN SUM(sal)
          ELSE NULL
         END AS SubTotal
    FROM emp_test
    GROUP BY GROUPING SETS (DeptNo, Empno), (DeptNo)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to calculate date difference using flowing query but it does not work.
We need to calculate driving distances for records in a SQL Server database, so
I need to calculate the number of FULL month in SQL, i.e. 2009-04-16 to
I need to calculate Tanh-1 in C# (and Sinh-1 and Cosh-1) I did not
I need calculate a thing. but my formula sentence has occur some problem. TextView
I need to calculate a machine id for computers running MacOS , but I
i need calculate normal of 6 faces from cube. The cube is moved with
Need to calculate optimum ulimit and fs.file-max values according to my own server needs.
I need to calculate precision and recall value in lucene and I use this
I need to calculate 4.0468564224e-33 value. But unfortunately I am getting a result of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.