如何按月份和年份分组数据

时间:2019-11-26 17:37:37

标签: sql group-by amazon-redshift tableau aginity

在开始讨论此问题之前,这里有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)

这是一张图片: enter image description here

现在,我的问题是我需要使输出保持这种格式,但还要按月和年对数据进行分组。我最初将数据按customer_id分组,因为我希望RFM和单个分数仅按唯一的customer_id出现,但是现在我需要按月+年和customer_id进行显示(即第一列为2018年1月,然后列出所有该月/年组合的唯一的customer_id行。然后是2018年2月,依此类推)。有人有什么建议吗?

非常感谢,如果您有任何疑问,请告诉我!!

最好, Z

2 个答案:

答案 0 :(得分:1)

如果要按year-monthcustomer_id分组,请按此顺序更改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)

答案 1 :(得分:0)

根据安东尼奥的要求:

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:” oms_order_date“列在派生表2中不存在”

我知道这是此表中的一列。确认使用: 从l_dmw_order_report中选择oms_order_date