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
2 answers

Data across multiple years.

I need to show the following results in multiple years at once. So, rather than running this report for 2014, then 2015, and finally 2016 I would like to run it one time for all three years (@startDate & @endDate). Not certain if a rollup would…
0
votes
2 answers

Raw data before each group by

Which queries should I execute in order to show raw data before every group by row? Is there a way to do that? User ReceiptNo Price ----- ---------- ------ A 876 100 A 877 50 **150** B 960 …
kk_nou
  • 103
  • 1
  • 10
0
votes
3 answers

npm link and rollupjs cannot find es6 module

I'm using rollupjs with babel, node-resolve and commonjs plugins. It works fine as I can import foo from "foo" to import foo from a npm-package called foo. I now want to contribute to a dependency and test it using npm link. So I cloned the…
ohcibi
  • 2,518
  • 3
  • 24
  • 47
0
votes
2 answers

Sum of a specific column in SQL Select Statement

I am creating following SQL Table SELECT Nr, nu_betrag_wert AS Amount FROM (SELECT DISTINCT processid, nu_betrag_wert, Nr FROM NU_WERTE WHERE Nr <> '(Nr)' AND nu_betrag_wert is NOT NULL) AS nu_betrag to get the a table of…
0
votes
3 answers

Can BigQuery drop subtotals rows WHEN responding to ROLLUP query

When I query BQ with ROLLUP by potentially large set of grouping criteria field (in this case campaign_group_id) for example: SELECT campaign_group_id AS campaign_group_id, DATE(DATE_ADD(TIME, 3, 'HOUR')) AS DAY, SUM(impressions) AS…
Andrew
  • 3
  • 3
0
votes
2 answers

get sum of columns in next column sql

Cruzer
  • 143
  • 1
  • 10
0
votes
1 answer

Is it possible to use two .babelrc files?

I have a project that runs both a build using rollup and a build using browserify for two different outputs. Now, they are both located in the same root dir and I have separate gulp-tasks running for both of them. My problem is that my browserify…
Maffelu
  • 2,018
  • 4
  • 24
  • 35
0
votes
1 answer

Getting the sum from a UNION ALL result set

I have a SQL query that takes several data points from each day across a window of time in a UNION ALL query, and then sums that data for each day in the result set. What I want to add to the result set is a total at the bottom of ALL the days in…
Lyssa
  • 3
  • 2
0
votes
2 answers

Add conditional group identifier using rollup functions

I have a data frame that has sub sequences (groups of rows) and the condition to identify these sub sequences is to watch for a surge in the column diff. This is what the data looks like : > dput(test) structure(list(vid = structure(c(1L, 1L, 1L,…
Nir Regev
  • 135
  • 1
  • 2
  • 7
0
votes
0 answers

Getting order totals and sorting them using 1 query

I have the following but I am not getting the correct totals because GROUP BY does not add the group user id's total revenue. SELECT users.id, Count(orders.id) AS total_orders, SUM(item_orders.item_price * item_orders.quantity) AS…
sschueller
  • 533
  • 1
  • 6
  • 18
0
votes
1 answer

Get monthy and yearly count value from mm/dd/yyyy in SQL Server using Rollup

I have a database table as shown below with over 100000 records: I am trying to implement rollup query on this so that I can retrieve records for a particular item and display the quantity in a monthly basis. I have Over 100000 records with 15…
Captain Red
  • 1,171
  • 2
  • 16
  • 27
0
votes
3 answers

How to display products under Category in sql in a table

I have the following table: where the products are in different categories and i am excepting the output: like product and its cost need to be displayed under category(For category cost value i want to display total products cost) .I tried…
chandra
  • 13
  • 1
  • 3
0
votes
2 answers

GROUP BY GROUPING SETS

I am writing Oracle SQL query; where i need to compute subtotals along with my normal query results. For this i decided to use GROUP BY Extension (GROUPING SETS) Due to privacy issues i am not sharing actual query. Instead i am writing below an…
user3729199
  • 85
  • 2
  • 8
0
votes
0 answers

MySQL rollup with Pearson's R

I'm using MySQL & PHP to calculate Pearson's R measuring the relationship over a number of years between political donations and tenders received by a particular business. I've run into trouble with the MySQL query that is feeding values into the…
user3470715
  • 25
  • 1
  • 5
0
votes
2 answers

Sum rows using ROLLUP

I have written a query that produces the following results. The query looks like SELECT rlrDivision.strLangue1 AS Division, COUNT(p.pkPoste) AS NumbersToAdd, SUM(p.iNbPostes) AS MoreNumbersToAdd FROM poste p LEFT JOIN …
00robinette
  • 497
  • 5
  • 16
PcodeTCodeTNameCCodeRateTotal=sum(rates of all PCodes)
123452002001220008000