0

I have query results shown below. The last row is the result from With Rollup.

Product    Total
5300          1
5600         13
5400         66
5700        200
NULL        280

Is there a way to use the Rollup data (280) in a calculation to find the percentage of the total? So I get this:

Product    Total     %
5300          1     3%
5600         13     5%
5400         66    22%
5700        200    72%
NULL        280    
Tug Strongly
  • 187
  • 2
  • 5
  • 18

2 Answers2

0

Should be easy with a CTE. Now depend on how strict are on your output.

SQL FIDDLE DEMO

WITH total as (
     SELECT Total
     FROM sumarize
     WHERE Product IS NULL
 )
 SELECT S.*, (S.Total * 1.0 / t.Total)*100 as porcent
 FROM sumarize S, total t
Juan Carlos Oropeza
  • 47,252
  • 12
  • 78
  • 118
0

Use a windowing function like this

SELECT (Total/(SUM(Total) OVER(PARTITION BY Product)))*100 AS Percent
Hogan
  • 69,564
  • 10
  • 76
  • 117
  • I'm not sure if that is the right way. First i couldnt copy the line on my fiddle to try it. And the denominator should be always the same, so not sure if calculate the `SUM() over partition` will give you that. – Juan Carlos Oropeza Sep 09 '15 at 19:33
  • ok, so you don't want to partition it? then just use `SUM(total) OVER ()` – Hogan Sep 09 '15 at 19:36