gpt4 book ai didi

sql - 在 SQL 中实现 qcut 以创建新列

转载 作者:行者123 更新时间:2023-11-29 13:44:39 47 4
gpt4 key购买 nike

我正在做 Recency-Frequency-Monetary 分析,虽然我有一个在 Python 中工作的模型,但由于生产代码主要是 PHP(Oracle 12c fwiw 或者也可以在 postgres 中完成),我试图在 SQL 中实现它.我在网上看到一个示例,它使用 ntile 添加了新列,但是我想使用等价的分位数(例如,将新近度 bin 边缘拆分为 0,0,74,321,用于 5 个分位数)。我已经开始走下面的路线,但是有没有更整洁的方法?

SELECT
percentile_disc(0.2) within group (order by recency) as recency_1_quin,
percentile_disc(0.4) within group (order by recency) as recency_2_quin,
percentile_disc(0.6) within group (order by recency) as recency_3_quin,
percentile_disc(0.8) within group (order by recency) as recency_4_quin,
percentile_disc(0.2) within group (order by monetary_value) as monetary_1_quin,
percentile_disc(0.4) within group (order by monetary_value) as monetary_2_quin,
percentile_disc(0.6) within group (order by monetary_value) as monetary_3_quin,
percentile_disc(0.8) within group (order by monetary_value) as monetary_4_quin,
percentile_disc(0.2) within group (order by frequency) as frequency_1_quin,
percentile_disc(0.4) within group (order by frequency) as frequency_2_quin,
percentile_disc(0.6) within group (order by frequency) as monetary_3_quin,
percentile_disc(0.8) within group (order by frequency) as monetary_4_quin
FROM RFM;

编辑:在帮助下,这就是我所在的位置,但在 CTE 中被困在一个小组中。 [42000][978] ORA-00978:没有 GROUP BY 的嵌套组函数

RFM AS (
SELECT SRC_USER_ID,
COUNT(distinct PICKUP_DATE) -1 as frequency,
(MAX(PICKUP_DATE) - MIN(PICKUP_DATE)) as recency,
(TO_DATE ('2018/05/12', 'yyyy/mm/dd') - MIN(PICKUP_DATE)) as T,
SUM(PRICE_TOTAL) AS monetary_value
FROM TRANSACTIONS
group by SRC_USER_ID
ORDER BY frequency DESC),
MAX_VALUES AS (
select sum(max(recency) + 0.0000000001) as max_recency,
sum(max(frequency) + 0.00000001) as max_frequency,
sum(max(monetary_value) + 0.000000001) as max_monetary
FROM RFM
)
SELECT
SRC_USER_ID,
recency,
frequency,
monetary_value,
WIDTH_BUCKET(recency, 0, max_recency, 5) "recency_quantile",
WIDTH_BUCKET(frequency, 0, max_frequency, 5) "frequency_quantile",
WIDTH_BUCKET(monetary_value, 0, max_monetary, 5) "monetary_quantile"
FROM RFM, MAX_VALUES

最佳答案

width_bucket() 没有给出我要找的东西,所以我得到了下面的结果。

RFM AS (
SELECT SRC_USER_ID,
COUNT(distinct PICKUP_DATE) -1 as frequency,
(MAX(PICKUP_DATE) - MIN(PICKUP_DATE)) as recency,
(TO_DATE ('2018/05/12', 'yyyy/mm/dd') - MIN(PICKUP_DATE)) as T,
SUM(PRICE_TOTAL) AS monetary_value
FROM TRANSACTIONS
group by SRC_USER_ID
ORDER BY frequency DESC),
QUINTILES AS (SELECT
percentile_disc(0.2) within group (order by recency) as recency_1_quin,
percentile_disc(0.4) within group (order by recency) as recency_2_quin,
percentile_disc(0.6) within group (order by recency) as recency_3_quin,
percentile_disc(0.8) within group (order by recency) as recency_4_quin,
percentile_disc(0.2) within group (order by monetary_value) as monetary_value_1_quin,
percentile_disc(0.4) within group (order by monetary_value) as monetary_value_2_quin,
percentile_disc(0.6) within group (order by monetary_value) as monetary_value_3_quin,
percentile_disc(0.8) within group (order by monetary_value) as monetary_value_4_quin,
percentile_disc(0.2) within group (order by frequency) as frequency_1_quin,
percentile_disc(0.4) within group (order by frequency) as frequency_2_quin,
percentile_disc(0.6) within group (order by frequency) as frequency_3_quin,
percentile_disc(0.8) within group (order by frequency) as frequency_4_quin
FROM RFM)
SELECT
SRC_USER_ID,
recency,
frequency,
monetary_value,
(CASE WHEN recency <= recency_1_quin THEN 1
WHEN recency > recency_1_quin and recency <= recency_2_quin THEN 2
WHEN recency > recency_2_quin and recency <= recency_3_quin THEN 3
WHEN recency > recency_3_quin and recency <= recency_4_quin THEN 4
ELSE 5 END) "recency_quantile",
(CASE WHEN frequency <= frequency_1_quin THEN 1
WHEN frequency > frequency_1_quin and frequency <= frequency_2_quin THEN 2
WHEN frequency > frequency_2_quin and frequency <= frequency_3_quin THEN 3
WHEN frequency > frequency_3_quin and frequency <= frequency_4_quin THEN 4
ELSE 5 END) "frequency_quantile",
(CASE WHEN monetary_value <= monetary_value_1_quin THEN 1
WHEN monetary_value > monetary_value_1_quin and monetary_value <= monetary_value_2_quin THEN 2
WHEN monetary_value > monetary_value_2_quin and monetary_value <= monetary_value_3_quin THEN 3
WHEN monetary_value > monetary_value_3_quin and monetary_value <= monetary_value_4_quin THEN 4
ELSE 5 END) "monetary_value_quantile"

FROM RFM, QUINTILES
ORDER BY recency DESC, frequency DESC, monetary_value DESC;

关于sql - 在 SQL 中实现 qcut 以创建新列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50405856/

47 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com