建立暫時資料表
找出在各自產業中(相對於其他 Fortune 500 公司)利潤位於前 20% 的 Fortune 500 公司。
要達成這點,先用下列方式計算各產業利潤的第 80 百分位數:
percentile_disc(fraction)
WITHIN GROUP (ORDER BY sort_expression)
並將結果儲存在一個暫時資料表中。
接著將 fortune500 與該暫時資料表做連接,選出利潤大於第 80 百分位門檻的公司。
本練習屬於課程
SQL 中的探索式資料分析
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
-- To clear table if it already exists; fill in name of temp table
DROP TABLE IF EXISTS ___;
-- Create the temporary table
___ ___ ___ ___ AS
-- Select the two columns you need; alias as needed
SELECT ___,
___(___) ___ (___) AS ___
-- What table are you getting the data from?
___ ___
-- What do you need to group by?
___ ___ ___;
-- See what you created: select all columns and rows from the table you created
SELECT *
FROM ___;