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
1 answer

Roll Up with multiple selects and join

I have the following query: select b.giorno , b.totali, b.unici, a.duplicati, round((a.duplicati/b.totali)*100) as percentuale from ( select count(count) as duplicati, c.day as day from ( select …
Sandro Antonucci
  • 1,683
  • 5
  • 29
  • 59
0
votes
1 answer

How to aggregate and roll up values from child to parent in Elastic Search

I am a newbie to Elastic Search and I am trying to find out how to handle the scenario briefed here. I am having a schema where a document may contain data such as { "country":"US", "zone": "East", "cluster": "Cluster1", "time_taken":…
MatterixN
  • 1
  • 2
0
votes
1 answer

how to implement rollup dimensions in SSAS

I have read in several DWH books about the rollup dimensions. For example when you have sales at month level and stock inventory at day level. The month dimension is a rollup dimension of the standard date dimension used for the stock inventory. My…
rodbs
  • 185
  • 1
  • 4
  • 12
0
votes
1 answer

How do I count distinct values in d3 nest rollup?

I'm using d3.nest.rollup to count leaves in my data. The data comes from csv and is converted to json by d3. The csv looks like this: Color,ID,Animal Green,1,Dog Red,2,Cat Red,3,Cat Red,3,Dog Note that ID #3 has 2 different animals, but I want to…
Pavel
  • 53
  • 1
  • 2
  • 8
0
votes
1 answer

Data warehouse rollup and grouping sets, which to use?

I have learned rollup, cube & grouping sets but one thing confuses me is how do I know which to use. For example, if I need to find the sale for each month in 2006 by region & by manager the two queries follow SELECT month, region, sales_mgr,…
0
votes
0 answers

OLAP difference between ORACLE and SQL Server

I'm beginner in OLAP manipulation in ORACLE. What is difference of OLAP between SQL Server and ORACLE? Information I've understood about OLAP: In ORACLE, OLAP is represented logically in data warehouse by relational table with join. We can use…
minh-hieu.pham
  • 1,029
  • 2
  • 12
  • 21
0
votes
1 answer

Multiple Column Summary in Teradata

First question here, hope you can help. I have a table with 75+ columns, first is an ID, the rest are just 1 or null flags Example: UNIQUE_IDENT, SERVICE_FLAG, P1_PREFA, P2_PREFB, etc etc >> P75_PREF75 987651651, 1, 1, 1, …
0
votes
1 answer

SQL: Ranking Sections separately of a Rollup over multiple columns

I try to do a Rollup over multiple columns and then apply a ranking on each stage/section of the rollup process. The result should look somewhat like the following: | ColA | ColB | ColC | RankingCriteria | Ranking…
Frank Wittich
  • 139
  • 3
  • 11
0
votes
1 answer

SQL Server - rollup with non-aggregated figure

I am calculating a % completeness figure for a number of sites, with each site having a designated 'Base' figure. I've used the code below to work out each site's individual completeness but I'm struggling to get an overall sum of the 'Base'…
GullitsMullet
  • 348
  • 2
  • 8
  • 24
0
votes
2 answers

oracle sql adding extra row

I have data like this col1 col2 col3 col3 ABC COMPANY OH, Akron - 3636 Copley Rd $50.00 MMO64606 ABC COMPANY OH, Akron - 3636 Copley Rd $200.00 MMO64606 ABC…
nitish rao
  • 21
  • 2
0
votes
1 answer

How to ROLLUP multiple columns that are joined SELECT statements into a single combined row?

I am making a view from the table [Data].[FCNP_Log] that has counts of several different attributes for several different 'Production Areas'. What I need is an additional row that says 'All' under the column 'Production Area' and has a total of all…
wedsa5
  • 39
  • 8
0
votes
1 answer

ROLLUP on only 1 column in mysql

I understand that we can use ROLLUP to get the total sum in group by query. However, is it possible to have more than 1 group by columns in group by query, but ROLLUP can be applied to ONLY ONE column ? eg, default ROLLUP behaviour: SELECT year,…
Saurabh Verma
  • 6,328
  • 12
  • 52
  • 84
0
votes
2 answers

Subtotal (rollup?) values only from 2nd column

I'm having difficulty pinning down the proper syntax to get a total I need from Oracle 11g. I need to produce a total based on values in column 2, independent of the values in column 1. I've tried several types of rollup, cube, grouping sets, but…
Brian Brock
  • 357
  • 2
  • 3
  • 18
0
votes
1 answer

mysql subquery to sum like checknumbers

I have two tables, one with id and concatenations of fields I'm checking for duplicates, and another with id and all other assorted data, one item being AmountPaid. The below query: Select i.id, i.CheckNumber, AmountPaid from HS i where i.id in…
caro
  • 863
  • 3
  • 15
  • 36
0
votes
0 answers

SQL Pivot and Total ROLLUP

Please help me understand how to implement the ROLLUP on this pivot table. I have been looking over several of the other written solutions to my requirement, but I seem to be missing something when I apply them to my situation. SQL is not my…