Questions tagged [rollup]

The ROLLUP operator is useful in generating reports that contain subtotals and totals. The ROLLUP operator generates a result set that is similar to the result sets generated by the CUBE operator

The ROLLUP operator is useful in generating reports that contain subtotals and totals. The ROLLUP operator generates a result set that is similar to the result sets generated by the CUBE operator. For more information, see Summarizing Data Using CUBE.

Following are the specific differences between CUBE and ROLLUP:

  • CUBE generates a result set that shows aggregates for all combinations of values in the selected columns.
  • ROLLUP generates a result set that shows aggregates for a hierarchy of values in the selected columns.

For example, a simple table Inventory contains the following:

Item                 Color                Quantity                   
-------------------- -------------------- -------------------------- 
Table                Blue                 124                        
Table                Red                  223                        
Chair                Blue                 101                        
Chair                Red                  210 

The next query

SELECT CASE WHEN (GROUPING(Item) = 1) THEN 'ALL'
            ELSE ISNULL(Item, 'UNKNOWN')
       END AS Item,
       CASE WHEN (GROUPING(Color) = 1) THEN 'ALL'
            ELSE ISNULL(Color, 'UNKNOWN')
       END AS Color,
       SUM(Quantity) AS QtySum
FROM Inventory
GROUP BY Item, Color WITH ROLLUP 

generates the following subtotal report:

Item                 Color                QtySum                     
-------------------- -------------------- -------------------------- 
Chair                Blue                 101.00                     
Chair                Red                  210.00                     
Chair                ALL                  311.00                     
Table                Blue                 124.00                     
Table                Red                  223.00                     
Table                ALL                  347.00                     
ALL                  ALL                  658.00   

If the ROLLUP keyword in the query is changed to CUBE, the CUBE result set is the same, except these two additional rows are returned at the end:

ALL                  Blue                 225.00                     
ALL                  Red                  433.00  

The CUBE operation generated rows for possible combinations of values from both Item and Color. For example, not only does CUBE report all possible combinations of Color values combined with the Item value Chair (Red, Blue, and Red + Blue), it also reports all possible combinations of Item values combined with the Color value Red (Chair, Table, and Chair + Table).

For each value in the columns on the right in the GROUP BY clause, the ROLLUP operation does not report all possible combinations of values from the column, or columns, on the left. For example, ROLLUP does not report all the possible combinations of Item values for each Color value.

The result set of a ROLLUP operation has functionality similar to that returned by a COMPUTE BY. However, ROLLUP has the following advantages:

  • ROLLUP returns a single result set while COMPUTE BY returns multiple result sets that increase the complexity of application code.
  • ROLLUP can be used in a server cursor while COMPUTE BY cannot.
  • The query optimizer can sometimes generate more efficient execution plans for ROLLUP than it can for COMPUTE BY.
1420 questions
-1
votes
2 answers

Convert a running total oracle sql query to a final total

I have the following query that works and gets me the total that I need. However, i need to change it so that it just displays the final total instead of every transaction leading up to the total. I have tried converting it by group by rollup, group…
moore1emu
  • 476
  • 8
  • 27
-1
votes
2 answers

Mssql summary row using ROLLUP

I'm trying to generate a summary row using ROLLUP grouping, Here is my query SELECT nic as NIC,branch_id,SUM(as_share),SUM(as_deposit) as as_deposit,SUM(as_credits) as as_credits,SUM(as_fixed) as as_fixed,SUM(as_ira) as as_ira,SUM(as_saviya) as…
Achira Shamal
  • 527
  • 1
  • 5
  • 18
-1
votes
1 answer

Rollup Creating Undefined Variable

I am trying to use a grunt rollup plugin, grunt-rollup, to compile two es6 files into Javascript that can be run from node and eventually the browser. Currently, it compiles, but rollup seems to be creating an undefined variable from one of my class…
Jon Doe
  • 2,172
  • 1
  • 18
  • 35
-1
votes
1 answer

Why does using with rollup give me different results when inside pivot are using sum and pivot and left join?

I have 3 tables Here the location http://rextester.com/PED43367 I failed in with roll up, can some one giving me the way? the output i want is : enter image description here The result is ok, but I can't make rollup with that Thanks for your Help
user8124226
  • 104
  • 12
-1
votes
2 answers

mysql sum(column2 - column1) with rollup

i need some help with my mysql query . I have one table that look like : And then i want to select with this query : select id, class, defaut, input, round(input - defaut) test from table1 group by class, id with rollup I want the output like :…
Mas Harjo
  • 73
  • 9
-1
votes
1 answer

TSQL query history table field change count

I need to find an efficient query to count the number of field changes per history table entry. Each time any data value changes in the Customer Table, a copy of the record is entered into the history table. I would like a way to count the numbers…
Wendi
  • 49
  • 1
  • 10
-1
votes
1 answer

SQL rollup two columns error

i am having error in SQL saying: Msg 213, Level 16, State 1, Line 7 Column name or number of supplied values does not match table definition. Code: CREATE TABLE temp ( kolA varchar(255), kolB varchar(255) ); INSERT temp…
tryingHard
  • 1,794
  • 4
  • 35
  • 74
-1
votes
1 answer

To calculate the percentage of items for various item types using SQL Server 2012

My table is as follows ITEM_CREATION_DATE ITEM_ID ITEM_TYPEID ITEM_FOLDER 2008-04-04 00:00:00.000 ITEM01 12 Y 2008-12-14 00:00:00.000 ITEM04 13 Y 2008-09-24 00:00:00.000 ITEM01 13 Y 2009-12-04 00:00:00.000 ITEM01 12 NULL 2009-04-09…
user1077595
  • 13
  • 1
  • 10
-2
votes
1 answer

Client-side typescript: how do I remove circular dependencies from the factory method pattern?

I am using the factory method pattern in some of my code. The problem is, some of those instances also use the same factory method pattern. This creates circular dependencies and I can't think of a way of removing them. Let me give an example: //…
Daniel Kaplan
  • 62,768
  • 50
  • 234
  • 356
-2
votes
1 answer

Applying Functions to several variables, by a number variables

The data I have looks as follows: Week_ID County State date ZCTA T_mean_F Precipitation holiday Units 523 Carroll Iowa 01/01/2010 51401 5.669194 0 1 0 523 Carroll Iowa 01/01/2010 51430 5.757368 0…
1 2 3
94
95