google 大查询中的平均和分组

Averaging and Grouping In google Big Query

我有 table,如 google 大查询所示:

我只想做以下事情:

  1. 计算类别明智的总售出单位

  2. 计算品类平均售价

考虑以下方法

select 'category' type, category name, count(1) units_sold, sum(sale_price) total_sale, round(avg(sale_price), 2) average_selling_price 
from your_table group by category
union all
select * from (
  select 'product' type, product name, count(1) units_sold, sum(sale_price) total_sale, round(avg(sale_price), 2) average_selling_price 
  from your_table group by product
  order by total_sale desc limit 10
)
union all
select * from (
  select 'order_date' type, '' || order_date name, count(1) units_sold, sum(sale_price) total_sale, round(avg(sale_price), 2) average_selling_price 
  from your_table group by order_date
  order by total_sale desc limit 5
)
order by type     

如果应用于 sample/dummy 数据 - 输出将如下所示