解答

194

SQL文(クエリ)

WITH master AS (
  SELECT
    product_category
    , revenue
    , cost * quantity AS cost_x_qty
  FROM
    sample.sales
    JOIN sample.products
    USING
      (product_id)
)
SELECT
  product_category
  , SUM(profit) AS profit
  , SUM(profit) / SUM(revenue) AS profit_rate
FROM
  (
    SELECT
      product_category
      , revenue
      , revenue - cost_x_qty AS profit
    FROM
      master
  )
GROUP BY
  product_category
ORDER BY
  3 DESC

結果テーブル

結果テーブル

サポートページに戻る