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
0
votes
3 answers

MS Dynamics CRM 2011 Rollup 14 caused Generic SQL Error

We're a new Dynamics CRM shop. We have loaded Accounts, Contacts and Notes from our old system, so there's not much on the system yet. After applying Rollup 14 last night, we log on this morning to a "SQL Server Error" screen. The same result…
Doug
  • 21
  • 3
0
votes
1 answer

Sql - Does the sum of some rows is content between A and B?

I'm currently trying to find, if the sum of some rows is content between A and B. For example, I need a surface between 100 m² and 105 m², so the request should add all the rows until the sum is content between 100 m² and 105 m², and try all the…
0
votes
0 answers

Compile error when trying to make rollup apex trigger

Been amending a piece of code to suit my needs but it won't compile. My naive eyes cannot see the error of my ways: trigger doRollup on Time_Record__c (after insert, after update, after delete, after undelete) { // List of parent record ids to…
44f
  • 101
  • 1
0
votes
0 answers

MySQL rollup ..

I have this SQL code create schema if not exists test; use test; drop table if exists contas; create table if not exists contas ( id_conta integer not null AUTO_INCREMENT, tipo enum('D', 'C') not null, valor decimal(10,2) not null, …
Haro Ken
  • 41
  • 1
  • 5
0
votes
1 answer

Minor tweaking of code for excel "Rollup"

I have the code from here: http://msdn.microsoft.com/en-us/libr...ice.12%29.aspx for making a Rollup (Merging Data from Multiple Workbooks into a Summary Workbook in Excel) it works great but I need some help in tweaking it. I need the destination…
xyz
  • 2,253
  • 10
  • 46
  • 68
0
votes
1 answer

Result column widths with derived tables and rollup

Consider these mysql queries: create temporary table t (a tinyint); insert into t values (1),(1),(2); select * from t; -- +------+ -- | a | -- +------+ -- | 1 | -- | 1 | -- | 2 | -- +------+ select c,count(*) from (select a,count(*) c…
msh210
  • 256
  • 6
  • 23
0
votes
2 answers

SQL Rollup last 4 weeks total

I have a table which I want to get the previous four weeks Order total in a query. But I want to return it with a SELECT (A total of the row's previous 4 weeks Order1 column - if they exist) PurchasingID Order1 Date …
Colbs
  • 587
  • 10
  • 25
0
votes
4 answers

Need to add "rollup" of entire dataset to bottom of DataGrid that is paged

In ASP.NET 3.5 I have a datagrid that is bound to a somwehat dynamic datatable. The datatable is created by taking three different tables returned from a dataset and combining them to create one table. It goes like this. The first datatable…
SpaceCowboy74
  • 1,367
  • 1
  • 23
  • 46
0
votes
3 answers

Showing rollup in last line only

This is needed to work in both SQL Server 2005 and 2008 without having to adjust the compatibility level (if it can be done) Select CASE GROUPING([Store ID]) WHEN 1 THEN '[Store ID]' ELSE [Store ID] END [Store ID], CASE GROUPING([Cashier])…
JohnZ
  • 382
  • 2
  • 8
  • 20
0
votes
1 answer

How can I translate this unscalable query into a window function for dynamic columns?

I have a query that I use to generate the total order amount for customers and group them into columns by month alongside another column that represents total order amount. Here's the schema: temp=# \d+ customers; Table…
Mahmoud Abdelkader
  • 23,011
  • 5
  • 41
  • 54
0
votes
1 answer

Mysql set label to Rollup or a similar GROUPING function like sql server

I have a problem with the ROLLUP, I have rows with null values, and the ROLLUP also returns null, how do I difference between the null values of the ROLLUP and the null values of the row? The null in the rows exist because the column (group_name) is…
fracedo
  • 127
  • 3
  • 13
0
votes
1 answer

Group by Highest Total Month

I am looking to order the MySQL statement below by the Module name (as it currently is) BUT only include the Modules that have the top 50 as far as the COUNT for all module_types included in the date range is concerned. Any ideas for how I would go…
Eishman
  • 125
  • 2
  • 3
  • 9
0
votes
1 answer

Multiple Groupings inside a case statement & ordering

I'm currently working on an aggregation stored proc and my final select looks like this: SELECT CASE WHEN GROUPING(Custodian) = 1 THEN 'Grand Total' ELSE Custodian END AS Custodian , …
chrissy p
  • 823
  • 2
  • 20
  • 46
0
votes
2 answers

help with oracle sql rollup

I've written a query to collect some data to display in an auto-updating box & whisker graph in excel. I'd like to use rollup to create summary rows for each type of train_line (PF and MJ) to include in the excel chart. Can I do that using…
Thomas ODell
0
votes
1 answer

Exclude columns from MySQL SELECT WITH ROLLUP?

Following on from this question I posed earlier, say I want to add ID to the query: SELECT client, ID, job_type, SUM(actual_value_fee) FROM jo2details GROUP BY client, job_type WITH ROLLUP but I don't want MySQL to try to total the ID column as it's…
user114671
  • 532
  • 1
  • 9
  • 27