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

Using Rollup with SQL produces double results

I am running a query to try and obtain some metrics by day of week as well as a total number. I am using ROLLUP to obtain a total average for the week. However, when I do so I see that the results are doubled. The numbers are the same and I verified…
user3120266
  • 395
  • 2
  • 7
  • 22
0
votes
2 answers

Only get the subtotal of all the items

I'm trying to move the sum value to the right instead of it being on the bottom. Currently, I have this query: SELECT if(product is NULL, 'Total', product) as Product, total_items as Total, SUM(total_items) as Subtotal FROM items WHERE inv =…
hocuspocus31
  • 183
  • 1
  • 10
0
votes
1 answer

Grouping in a SQL Pivot

I've managed to create a pivot table in MS SQL and I'd like to group the annual columns by the GrpID with rollup. I understand as much that there's an aggregate clause which should be adhered to, but can't get my head around it. SELECT * FROM( …
Stu412
  • 251
  • 1
  • 3
  • 11
0
votes
3 answers

Two level grouping in sql query

I have three tables that will be used in the query: Departments: DepartmentID - PK DepartmentName Earnings: EarningID - PK PersonID EarningValue People: PersonID - PK DepartmentID Surname Name I need to make a query that will return 4 columns: -…
Thunder Blade
  • 127
  • 15
0
votes
1 answer

Get the sum per row based on the user

Just want to ask for help on how to get the sum per row based on the user? I'm trying to get the cost of the project by multiplying the hours worked of the employee and then compute it to the employee's rate. I have 3 tables: Employee, Hours,…
hocuspocus31
  • 183
  • 1
  • 10
0
votes
1 answer

Convert SQL computer to ROLL UP sql server 2012 since its no longer support compute

I have db like this: For meaning: LOP: class SINHVIEN: student KHOA: department MONHOC: subject DIEMTHI: mark I want to do the query to list ALL the CLASS and the number of CLASS belong to each DEPARTMENT SELECT khoa.makhoa,tenkhoa,malop,tenlop FROM…
Andiana
  • 1,912
  • 5
  • 37
  • 73
0
votes
1 answer

Rollup - Filter rows by subtotal amounts sql oracle

Supposing I have a table and query: consisting of population for a given country in a given continent for a given year. i want to return countries avg(population) & the continents avg population if the country's population is greater than the…
runningbirds
  • 6,235
  • 13
  • 55
  • 94
0
votes
0 answers

MySQL : Ambiguous Column with ifnull rollup

I'm getting this error when i want to run a View, but I don't know why: [23000][1052] Column 'name' in field list is ambiguous Query: create view prefix_group_rating AS SELECT r.rating_date …
elarichi.y
  • 57
  • 8
0
votes
1 answer

How to do a rollup of one table in SQL Server and have child tables rolled up to refer correctly to the rolled up parent rows in SQL Server

We have some tables in our database that pretty much grow infinitely so we are trying to create a task that will rollup old data on a periodic basis (probably daily) and remove the records that are contained in the rollup. This is reasonably easy…
Dorky Engineer
  • 1,054
  • 2
  • 7
  • 9
0
votes
2 answers

mysql: ROLLUP on TOP, SUM(FINANCIAL AMOUNT), then order FINANCIAL AMOUNT column by DATETIME DESC

Table 1 CREATE TABLE tblFinancials ( pkFinancialID int(11) NOT NULL AUTO_INCREMENT, fkUserID int(11), fkProfileID int(11), fkFinancialEntryID int(11), FinancialAmount decimal(10,2), FinancialDateTime datetime ); pkFinancialID fkUserID fkProfileID…
0
votes
1 answer

SSAS Time state rollup

I have the following situation (in SSAS): https://i.stack.imgur.com/VzkDS.jpg Cube with two dimensions (Time / Alpha) and one measure (X) Columns A - G is the result of the cube, with column G as the total sum. How to get the average (like column…
Mizzle
  • 13
  • 1
  • 4
0
votes
2 answers

How to fill blank while using GROUP BY WITH ROLLUP in MySQL

I have a table with id, desc, quantity. I used GROUP BY WITH ROLLUP to get the subTotal in the result. Here is SQL Fiddle link In the result i want to know how to make the "description" column blank for the row created by roll up In my actual…
Rao
  • 2,902
  • 14
  • 52
  • 70
0
votes
2 answers

SQL Server :Recursive CTE? Creating Report From an Organisation Structure

I hope someone can help me. I want to have a Report with calculations for an organisation structure. CREATE TABLE Organistation ( KeySubCompany NUMERIC(12, 0) , O1 VARCHAR(255) , O2 VARCHAR(255) , O3 VARCHAR(255) , O4…
WhoisIt
  • 123
  • 2
  • 10
0
votes
2 answers

Solr roll up query

I have a specific query with SOLR that I cannot seem to find a solution for. I have an index full of products and sku's. A product has multiple sku's and every sku has 1 product. I want to perform a search against my SKU's only, group by the parent…
0
votes
2 answers

Hierarchical query rollup Rollup

I have the following table: parent_id child_id child_class 1 2 1 1 3 1 1 4 2 2 5 2 2 6 2 Parent_id represents a folder id. Child id represents either a child folder (where child_class=1) or child file (where…
user2965499
  • 29
  • 1
  • 4