利用 2022/11 捷運 進出入資料來做 sql 資料分析練習
使用的資料庫為 Postgresql
基本欄位有
日期 , 進站, 出站 , 人次
相對應 創立的欄位是 “date” ,“in” ,“out” ,people_count
- 總共有幾筆資料
select count(*) count from public."mrt-tran"
count |
---|
8921430 |
- 每日每趟旅程 總人數
SELECT "date" ,"in" ,"out" ,sum(people_count) sum_count
FROM public."mrt-tran" group by "date" ,"in" ,"out" order by sum_count desc
- 當月進站抵達最高人次
select
cc.gg ,
bb."out" ,
cc.ss
from
(
select
aa."in" gg,
max(aa.s) ss
from
(
select
"in",
"out" ,
sum(people_count) s
from
public."mrt-tran"
group by
"in",
"out") aa
group by
aa."in") cc
left join (
select
"in",
"out" ,
sum(people_count) s
from
public."mrt-tran" group by
"in",
"out"
) bb on
cc.ss = bb.s
- 因為數據量大 如果一直查詢 重算 每次都會花很久時間 所以postgresql有個功能叫做物化視圖 適合放冷資料 不會需要最新資料 的東西
- 根據每月統計 乘客量 成立一張 物化視圖
CREATE MATERIALIZED VIEW "mrt-monthly-count" AS SELECT TO_CHAR( de :: DATE, 'yyyy-mm') de,sum(cc) cc from "mrt-tran" group by TO_CHAR( de :: DATE, 'yyyy-mm') order by cc
- 根據每天 統計每站的人數 拿掉 小時因子
CREATE MATERIALIZED VIEW "mrt_dayliy_cc" as select de ,fin, fout,sum(cc) cc from "mrt-tran" group by de ,fin, fout
- 看進站 人數 由多至少
select fin,sum(cc)cc from mrt_dayliy_cc mdc group by fin order by cc desc
ref
http://www.postgres.cn/docs/9.5/rules-materializedviews.html