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

Saba/SCORM 2004 3rd Edition 'Attempt Status' Suspended

A SCORM Question regarding how I invoke Rollup (macro?) in Saba. I have a SCORM manifest which contains two SCOs. The second is only available when the first is completed by having a preConditionRule (similar to the SCORM/Rustici Golf…
JPC
  • 71
  • 1
  • 9
0
votes
1 answer

SQL Server 2008 - Error Order by case

I'm getting the following error messages: Msg 8120, Level 16, State 1, Line 1 Column 'customers.member_category' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause. Msg 104, Level 16,…
HL8
  • 1,369
  • 17
  • 35
  • 49
0
votes
1 answer

Averaged Historical Data from Xively feed API

The xively (Cosm) web interface issues the following function for averaged historical datapoints // For averaged historical…
rjha94
  • 4,292
  • 3
  • 30
  • 37
0
votes
2 answers

T-SQL Group By on Irregular Time Frames

I have a SQL statement that works perfectly if want my SUMmations for each and every hour (SQL Server 2008). The DATEPART(HOUR, DATE_TIME) is doing all the wonderful work for me. SELECT SUM(case STATION_ID when 'S-WELDCHK' then 1 else 0 end) as…
kingchris
  • 1,677
  • 22
  • 29
0
votes
1 answer

Oracle ROLLUP - Introductory explanation?

I've searched (but can't find) a very basic/introductory explanation of Oracle 11g's ROLLUP. From the queries below, it appears that I'm supplied subtotals for any expressions not included in the ROLLUP clause. Is this correct? I'd appreciate a…
Jeff Levine
  • 2,083
  • 9
  • 30
  • 38
0
votes
1 answer

How do I calc a total row using a PIVOT table, without UNION, ROLLUP or CUBE?

Can someone help out with calculating a total row at the bottom of this PIVOT table please? select *, [Drug1] + [Drug2] + [Drug3] + [Drug4] + [Drug5] as [Total] from (Select [id], [drug], [Diagnosis] from DrugDiagnosis …
user2633679
  • 3
  • 1
  • 4
0
votes
0 answers

How to rollup counting number of groups

ROLLUP allows to aggregate across multiple levels of grouping as if I UNIONed multiple simple SELECT statements. But I want to be able to aggregate results of lower level of grouping as if I used nested SELECT statements or chain of CTEs dependent…
alpav
  • 2,972
  • 3
  • 37
  • 47
0
votes
1 answer

I need assistance with a query using group by with rollup

I am trying to display the book order data showing the year of the order in the first column, the month in the second. Plus, display totals by month and by year and grand totals. Also, display messages "Yearly Total" and "Grand Total" instead of the…
0
votes
2 answers

How to number lines, with total at end?

My current sql: select s.dcid, substr(s.lastfirst,0,3), to_char(a.att_date, 'mm/dd/yyyy'), a.periodid, p.name, a.attendance_codeid, ac.att_code, count(*) from students s join attendance a on s.id = a.studentid join period p on a.periodid =…
Brian Brock
  • 357
  • 2
  • 3
  • 18
0
votes
2 answers

Group By Rollup suppress extra line of data

I'm on an Oracle 11g database. Query: SELECT s.id, s.grade_level, ((1990+substr(sg.termid,1,2))||'-'||(1991+substr(sg.termid,1,2))) as "School Year", SUM(sg.earnedcrhrs) as "Credits" FROM students s JOIN storedgrades sg ON s.id =…
Brian Brock
  • 357
  • 2
  • 3
  • 18
0
votes
1 answer

SQL Is it possible to replace values in a Rollup?

I have a result set that mainly consists of columns of values. However, the left most column contains text which I'm using to effectively label each row. When I ROLLUP, the values total, which is great. The text in my 'No KS2' column rolls up to a…
Matt
  • 263
  • 2
  • 10
  • 35
0
votes
1 answer

How to use rollup in SQL in select groupby

Im trying to group by Source then Date, then Status and get Counts for each. This is working in my code: SELECT sourceCode as "Source", to_char(myTimestamp, 'YYYY-MM-DD') as "Date", statusCode as "Status", count(*) as "Count" FROM…
Kairan
  • 5,342
  • 27
  • 65
  • 104
0
votes
1 answer

How to control aggregation in Analytic Grid - Dashboard Designer

I have an analytic grid created in Dashboard Designer for SharePoint 2010 as shown below. The percentage values are shown in month wise and when they are rolled up to a year, they are summed up. I would like to have it averaged instead. I don't…
Merin Nakarmi
  • 3,148
  • 3
  • 35
  • 42
0
votes
1 answer

ROLLUP, CUBE or GROUPING SETS

Please refer to the link to http://sqlfiddle.com/#!3/1b16f/7 for the schema details. I have used dynamic pivot query to get the month on month details. Now, I am trying to get the sub total for "transactional" and "Customer" and the grand total at…
Youbaraj Sharma
  • 1,295
  • 3
  • 17
  • 34
0
votes
0 answers

Oracle 10g ROLLUP and TO_CHAR function

I have 2 queries: select zam_klt_id,zam_order_date, count(*) as sum from orders group by rollup (zam_order_date,zam_klt_id); which produce output: ZAM_KLT_ID ZAM_order_date SUM ---------- ------------------- ---------- 1002 98/03/13 …