解答
186
SQL文(クエリ)
WITH master AS ( SELECT quantity , revenue , revenue / quantity AS unit_revenue , cost FROM sample.sales JOIN sample.products USING (product_id) ) SELECT SUM(profit) AS sum_profit , SUM(profit) / SUM(revenue) AS profit_rate FROM ( SELECT quantity , (unit_revenue - cost) * quantity AS profit , revenue FROM master )