Given a query like this,
CREATE TABLE foo(grp)
AS VALUES ('a'),('a'),('b'),('b'),('b'),('c'),('c'),('c'),('c'),('e');
I can use GROUP BY
to aggregate into a
,b
,c
,e
, but how can I return the different groups, and the percentage of the total table they represent?
a | 20%
b | 30%
c | 40%
e | 10%
Best Answer
First percentages are always in a range of
[0-1)
when stored on a computer. Rendering them in a range of a0-100
is usually a function of the display (app). From wikipedia,If you have for example a simple table.
All you must do is
Now if you want
percent
to be displayed like a string, you can easily do that too. Because it's mere visual display, I suggest not to do this in a database but we can; a function like this is simple and hides the obscuring details of how you want the number rendered, from the math that calculates it:And there we have it,