Clickhouse: Clickhouse: Percent to Total

Created on 9 Nov 2017  路  1Comment  路  Source: ClickHouse/ClickHouse

We have to port our script which calculates "Percent to total".

Product,Sales
P1, 100
P2, 200
P3, 150
P4, 50

We are looking at writing a script which can populate
Product, Sales, PercenttoTotal
P1, 100, 20%
P2, 200, 40%
P3, 150, 30%
P4, 50, 10%

Is there a quick way via various functions in Clickhouse using which we can accomplish this. Thanks in advance

question

Most helpful comment

You need something like that:

SELECT 
   productid,
   count() as per_product_count,
   ( SELECT count() from prod_sales ) as total_count,
   per_product_count * 100 /total_count as percent_to_total
FROM prod_sales
GROUP BY productid

or the same without extra column:

SELECT 
   productid,
   count() as per_product_count,
   per_product_count * 100 / ( SELECT count() from prod_sales ) as percent_to_total
FROM prod_sales
GROUP BY productid

P.S. Google group or stackoverflow is better place for those type of questions.

>All comments

You need something like that:

SELECT 
   productid,
   count() as per_product_count,
   ( SELECT count() from prod_sales ) as total_count,
   per_product_count * 100 /total_count as percent_to_total
FROM prod_sales
GROUP BY productid

or the same without extra column:

SELECT 
   productid,
   count() as per_product_count,
   per_product_count * 100 / ( SELECT count() from prod_sales ) as percent_to_total
FROM prod_sales
GROUP BY productid

P.S. Google group or stackoverflow is better place for those type of questions.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

SaltTan picture SaltTan  路  3Comments

vixa2012 picture vixa2012  路  3Comments

bseng picture bseng  路  3Comments

jimmykuo picture jimmykuo  路  3Comments

jangorecki picture jangorecki  路  3Comments