Skip to content
Advertisement

Using ROLL UP/CUBE in conjunction with PIVOT [Oracle]

I have a table in Oracle that looks like this:

Now I want to make it like this:

Where the numbers in each cell corresponds to DISTINCT count of names of customers.

When I attempt to do this query:

I get this intermediate result:

When I use this as a subquery then do a pivot:

I do not get the expected result. Please include the use of ROLL UP/CUBE and PIVOT in the answer.

Advertisement

Answer

With grouping by rollup(year, month) you had no sums for same month from different years. So I used cube and slightly modified your query, please check it:

demo


Edit:

If in summing columns you need sums of distinct counts then make basic grouping at first, then use cube. And pivot at the end. Aggregating function in pivot is not important, because you have already counted values, one per each row / column.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement