Top N products type in every month - BIGQUERY

71 views Asked by At

I have to create a TOP 10 of gross revenue/country/month with the statement :

dimensions:

  • date (month)
  • site_country
  • product type

metric: gross_revenue (only top 10 products type per country/month)

I have tried a big query program below between 2 tables :

SELECT FORMAT_DATE("%Y-%m",t0.order_create_date) AS month, t0.site_country AS country, p0.product_type AS product, SUM(t0.item_sale_price) AS gross_revenue

FROM transactions t0 LEFT JOIN products p0 ON t0.item_id = p0.item_id where rn <= 10 ORDER BY month ASC LIMIT

I also tried this:

enter image description here

1

There are 1 answers

7
Nickname_used On
select *
from(
select *, ROW_NUMBER() OVER (PARTITION BY country ORDER BY gross_revenue DESC) AS rn
from(
SELECT FORMAT_DATE("%Y-%m",t0.order_create_date) AS month,
       t0.site_country AS country,
       p0.product_type AS product,
       SUM(t0.item_sale_price) AD gross_revenue
FROM transactions t0
  LEFT JOIN products p0 ON t0.item_id = p0.item_id)x )xx
  where rn <= 10