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
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.
Most helpful comment
You need something like that:
or the same without extra column:
P.S. Google group or stackoverflow is better place for those type of questions.