-- awesomeなsqlの処理はこれを参考にしてみる
-- https://github.com/ghmagazine/awesomebook/tree/master/preprocess
-- part1: VARIANCE関数にtotal_priceを指定し、分散値を算出
-- * COALESCE関数によって、分散値がNULLのときは0に変換
-- * データ数が2件以上の場合は、STDDEV関数にtotal_priceを指定し、標準偏差値を算出
SELECT
hotel_id,
COALESCE(VARIANCE(total_price), 0) AS price_var,
COALESCE(STDDEV(total_price), 0) AS price_std
FROM work.reserve_tb
GROUP BY hotel_id
-- part2
SELECT
ROUND(total_price, -3) AS total_price_round
FROM work.reserve_tb
GROUP BY total_price_round
ORDER BY COUNT(*) DESC
LIMIT 1
-- part3: ランキング
SELECT
*,
-- ROW_NUMBERで順位を取得
-- PARTITION by customer_idで顧客ごとに順位を取得するよう設定
-- ORDER BY reserve_datetimeで順位を予約日時の古い順に設定
ROW_NUMBER()
OVER (PARTITION BY customer_id ORDER BY reserve_datetime) AS log_no
FROM work.reserve_tb