解答

145

SQL文(クエリ)

WITH master AS (
  SELECT
    user_id
    , COUNT(DISTINCT order_id) AS orders
  FROM
    sample.sales
  GROUP BY
    user_id
)
SELECT
  CASE gender
    WHEN 1 THEN "男性"
    WHEN 2 THEN "女性"
    ELSE NULL
  END AS gender_cat
  , SUM(orders) AS sum_orders
FROM
  master
  LEFT JOIN sample.customers
  USING
    (user_id)
GROUP BY
  gender_cat
ORDER BY
  sum_orders DESC

結果テーブル

結果テーブル

サポートページに戻る