Phân tích dữ liệu kinh doanh bằng SQL
Michel Semaan
Data Scientist
KPI
Lợi ích
KPI đăng ký: Đếm số đăng ký theo thời gian, thường theo tháng
Với Delivr, ngày đăng ký là ngày đơn hàng đầu tiên của người dùng
Truy vấn
SELECT
user_id,
MIN(order_date) AS reg_date
FROM orders
GROUP BY user_id
ORDER BY user_id
LIMIT 3;
Kết quả
user_id reg_date
------- ----------
0 2018-06-01
1 2018-06-01
2 2018-06-01
WITH reg_dates AS (
SELECT
user_id,
MIN(order_date) AS reg_date
FROM orders
GROUP BY user_id)
SELECT
DATE_TRUNC('month', reg_date) :: DATE AS delivr_month,
COUNT(DISTINCT user_id) AS regs
FROM reg_dates
GROUP BY delivr_month
ORDER BY delivr_month ASC
LIMIT 3;
Kết quả
delivr_month regs
------------ ----
2018-06-01 123
2018-07-01 140
2018-08-01 157
SELECT
DATE_TRUNC('month', order_date) :: DATE AS delivr_month,
COUNT(DISTINCT user_id) AS mau
FROM orders
GROUP BY delivr_month
ORDER BY delivr_month ASC
LIMIT 3;
delivr_month mau
------------ ---
2018-06-01 123
2018-07-01 226
2018-08-01 337
Phân tích dữ liệu kinh doanh bằng SQL