解答

196

SQL文(クエリ)

WITH master AS (
  SELECT
    *
    , IF(product_id = 2, date_time, NULL) AS date2
    , IF(product_id = 15, date_time, NULL) AS date15
  FROM
    sample.sales
)
SELECT
  user_id
FROM
  (
    SELECT
      user_id
      , MIN(date2) AS min_date2
      , MIN(date15) AS min_date15
    FROM
      master
    GROUP BY
      user_id
  )
WHERE
  min_date15 > min_date2
AND FORMAT_DATETIME("%Y", min_date2) = FORMAT_DATETIME("%Y", min_date15)
ORDER BY
  1

結果テーブル

結果テーブル

サポートページに戻る