Question: I have the below table

![Current Table](https://i.stack.imgur.com/NjaEm.png)

i want to aggregate and rollup the data to be displayed per below screenshot.

How do i go about this, is it possible to exactly rollup this way?

![Required Output](https://i.stack.imgur.com/mhW5E.png)

Answer: 
I would use `grouping sets`:

```
select fruit, type, sum(amount), sum(percent)
from t
group by grouping sets ( (fruit, type), (fruit) );

```
You can use the `ROLLUP` and then necessary conditions to omit the extra generated rows as follows:

```
SQL> -- Sample data
SQL> WITH DATAA (FRUIT, TYPE, AMOUNT, PERCENT) AS
  2  (
  3  SELECT 'Apple', 'Green', 10017, 17 FROM DUAL UNION ALL
  4  SELECT 'Orange', 'Green', 10016, 16 FROM DUAL UNION ALL
  5  SELECT 'Papaya', 'Yellow', 10014, 14 FROM DUAL UNION ALL
  6  SELECT 'Papaya', 'Blue', 10005, 5 FROM DUAL UNION ALL
  7  SELECT 'Papaya', 'Green', 10012, 12 FROM DUAL
  8  )
  9  -- Your query starts from here
 10  SELECT *
 11  FROM (
 12          SELECT FRUIT, TYPE, AMOUNT, SUM(PERCENT) AS PERCENT
 13            FROM DATAA
 14          GROUP BY ROLLUP(FRUIT, TYPE, AMOUNT)
 15       )
 16  WHERE ( FRUIT IS NOT NULL AND TYPE IS NOT NULL AND AMOUNT IS NOT NULL )
 17      OR ( FRUIT IS NOT NULL AND TYPE IS NULL AND AMOUNT IS NULL )
 18  ORDER BY FRUIT, TYPE DESC NULLS LAST;

FRUIT  TYPE       AMOUNT    PERCENT
------ ------ ---------- ----------
Apple  Green       10017         17
Apple                            17
Orange Green       10016         16
Orange                           16
Papaya Yellow      10014         14
Papaya Green       10012         12
Papaya Blue        10005          5
Papaya                           31

8 rows selected.

SQL>

```