Oracle PL/SQL: SQL ROLLUP

SQL ROLLUP

You can use WITH ROLLUP to generate a summary row for each group.

Example 1 of ROLLUP

Suppose we have the following table - Sales:


EmpIdYrSales
1200512000.00
1200618000.00
1200725000.00
2200515000.00
220066000.00
3200620000.00
3200724000.00


A simple Group by results in:

SELECT Yr, SUM(Sales) AS Sales
FROM Sales
GROUP BY Yr

YrSales
200527000.00
200644000.00
200749000.00

A simple group by with ROLLUP results in:

SELECT Yr, SUM(Sales) AS Sales
FROM Sales
GROUP BY Yr WITH ROLLUP

YrSales
200527000.00
200644000.00
200749000.00
NULL120000.00

Another example of group by with ROLLUP:

SELECT EmpId, Yr, SUM(Sales) AS Sales
FROM Sales
GROUP BY EmpId, Yr WITH ROLLUP

EmpIdYrSales
1200512000.00
1200618000.00
1200725000.00
1NULL55000.00
2200515000.00
220066000.00
2NULL21000.00
3200620000.00
3200724000.00
3NULL44000.00
NULLNULL120000.00

No comments:

Post a Comment

Please Provide your feedback here