Romain Granger
Aug 17, 2022

--

Hi Juan,

I am not sure to fully understand your question, as what defines the top 10 items ?

But you could do as follow:

1- Sum the revenue per item (product name) instead of product categories

2- Compute the share of the total, ordered from the most share to the least share ( descending)

3- Limit 10, to return the items that generated the most revenue and have the biggest revenue share

Hope it helps.

Here is the query:

WITH

revenue_product AS (

SELECT

product_name,

ROUND(SUM(product_revenue),0) AS revenue_per_product

FROM

`datastic.variables.base_table`

GROUP BY

1)

-- Main Query

SELECT

*,

ROUND(SAFE_DIVIDE(revenue_per_product,

SUM(revenue_per_product) OVER())*100,2) AS share_revenue

FROM

revenue_product

ORDER BY

share_revenue DESC

LIMIT

10

--

--

Romain Granger
Romain Granger

Written by Romain Granger

📊 Data Scientist | SQL, BigQuery, Python | Follow me to learn about SQL and data-related topics. Thank you so much for your support!

No responses yet