[英]How to group data by the month and year
在我進入這個問題之前,這里有一個 2 秒的背景:我一直在研究這個 RFM 分析,感謝我們的同行,終於能夠為我的數據集中的每個 customer_id 輸出一個 RFM 分數,以及他們的每個個人 R、F 和 M 分數。 在這里,如果您很好奇或想自己使用它:
SELECT *,
SUBSTRING(rfm_combined,1,1) AS recency_score,
SUBSTRING(rfm_combined,2,1) AS frequency_score,
SUBSTRING(rfm_combined,3,1) AS monetary_score
FROM (
SELECT
customer_id,
rfm_recency*100 + rfm_frequency*10 + rfm_monetary AS rfm_combined
FROM
(SELECT
customer_id,
ntile(5) over (order by last_order_date) AS rfm_recency,
ntile(5) over (order by count_order) AS rfm_frequency,
ntile(5) over (order by total_spent) AS rfm_monetary
FROM
(SELECT
customer_id,
MAX(oms_order_date) AS last_order_date,
COUNT(*) AS count_order,
SUM(quantity_ordered * unit_price_amount) AS total_spent
FROM
l_dmw_order_report
WHERE
order_type NOT IN ('Sales Return', 'Sales Price Adjustment')
AND item_description_1 NOT IN ('freight', 'FREIGHT', 'Freight')
AND line_status NOT IN ('CANCELLED', 'HOLD')
AND oms_order_date BETWEEN '2018-01-01' AND '2018-12-31'
GROUP BY customer_id))
ORDER BY customer_id desc)
這是一張圖片:在此處輸入圖片說明
現在,我的問題是我需要保持這種格式的輸出,但也要按月份和年份對數據進行分組。 我最初按 customer_id 對這些數據進行了分組,因為我希望 RFM 和個人分數僅按唯一的 customer_id 顯示,但現在我需要按月 + 年和 customer_id(即第一列是 2018 年 1 月,然后列出所有該月/年組合的唯一 customer_id 行。然后是 2018 年 2 月,依此類推)。 有人有什么建議嗎?
非常感謝,如果您有任何問題,請告訴我!!
最好的,Z
如果您想按year-month
和customer_id
分組,請GROUP BY
順序更改您的GROUP BY
:
SELECT *,
SUBSTRING(rfm_combined,1,1) AS recency_score,
SUBSTRING(rfm_combined,2,1) AS frequency_score,
SUBSTRING(rfm_combined,3,1) AS monetary_score
FROM (
SELECT
YearMonth,
customer_id,
rfm_recency*100 + rfm_frequency*10 + rfm_monetary AS rfm_combined
FROM
(SELECT
YearMonth,
customer_id,
ntile(5) over (order by last_order_date) AS rfm_recency,
ntile(5) over (order by count_order) AS rfm_frequency,
ntile(5) over (order by total_spent) AS rfm_monetary
FROM
(SELECT
to_char(oms_order_date, 'YYYY-MM') AS YearMonth,
customer_id,
MAX(oms_order_date) AS last_order_date,
COUNT(*) AS count_order,
SUM(quantity_ordered * unit_price_amount) AS total_spent
FROM
l_dmw_order_report
WHERE
order_type NOT IN ('Sales Return', 'Sales Price Adjustment')
AND item_description_1 NOT IN ('freight', 'FREIGHT', 'Freight')
AND line_status NOT IN ('CANCELLED', 'HOLD')
AND oms_order_date BETWEEN '2018-01-01' AND '2018-12-31'
GROUP BY to_char(oms_order_date, 'YYYY-MM'), customer_id))
ORDER BY YearMonth, customer_id desc)
根據安東尼奧的要求:
SELECT *,
SUBSTRING(rfm_combined,1,1) AS recency_score,
SUBSTRING(rfm_combined,2,1) AS frequency_score,
SUBSTRING(rfm_combined,3,1) AS monetary_score
FROM (
SELECT
to_char(oms_order_date, 'YYYY-MM'),
customer_id,
rfm_recency*100 + rfm_frequency*10 + rfm_monetary AS rfm_combined
FROM
(SELECT
customer_id,
ntile(5) over (order by last_order_date) AS rfm_recency,
ntile(5) over (order by count_order) AS rfm_frequency,
ntile(5) over (order by total_spent) AS rfm_monetary
FROM
(SELECT
customer_id,
MAX(oms_order_date) AS last_order_date,
COUNT(*) AS count_order,
SUM(quantity_ordered * unit_price_amount) AS total_spent
FROM
l_dmw_order_report
WHERE
order_type NOT IN ('Sales Return', 'Sales Price Adjustment')
AND item_description_1 NOT IN ('freight', 'FREIGHT', 'Freight')
AND line_status NOT IN ('CANCELLED', 'HOLD')
AND oms_order_date BETWEEN '2018-01-01' AND '2018-12-31'
GROUP BY to_char(oms_order_date, 'YYYY-MM'), customer_id))
ORDER BY customer_id desc)
LIMIT 100
錯誤說明:“42703:derive_table2 中不存在列“oms_order_date”
我知道事實上這是該表中的一列。 確認使用:SELECT oms_order_date FROM l_dmw_order_report
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.