解答

190

SQL文(クエリ)

WITH master AS (
  SELECT
    order_id
    , quantity
    , revenue / quantity AS unit_revenue
    , cost
  FROM
    sample.sales
    JOIN sample.products
    USING
      (product_id)
)
SELECT
  order_id
  , SUM(profit) AS profit_by_order
FROM
  (
    SELECT
      order_id
      , quantity
      , (unit_revenue - cost) * quantity AS profit
    FROM
      master
  )
GROUP BY
  order_id
ORDER BY
  2 DESC
LIMIT 1

結果テーブル

結果テーブル

サポートページに戻る