在SQL中实现qcut以创建新列

时间:2018-05-18 07:19:10

标签: sql oracle postgresql pandas oracle12c

我正在进行新近 - 频率 - 货币分析,而我有一个使用Python的模型我试图在SQL中实现它,因为生产代码主要是PHP(Oracle 12c fwiw或者也可能是在postgres完成)。我在网上看到了一个使用ntile添加新列的示例,但是我想使用等效的分位数(例如,将5个分位数的新近度边缘分割为0,0,74,321)。我已经开始走下面的路线,但有更整洁的方式吗?

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中被困在一个小组中。 [4278] [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

1 个答案:

答案 0 :(得分:0)

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;