解答

195

SQL文(クエリ)

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

結果テーブル

結果テーブル

サポートページに戻る