with B as (select cust_uid,
datediff(curdate(),max(trx_dt)) as recency,
count(distinct trx_dt) as frequency,
avg(trx_amt) as monetary
from mt_trx_rcd_f
group by cust_uid
)
select b.cust_uid,
ntile(3)over(order by b.recency desc) asrecency_score,
ntile(3)over(order by b.frequency) as frequency_score,
ntile(3)over(order by b.monetary) as monetary_score
from B b
order by cust_uid;
with B as (select cust_uid,
datediff(curdate(),max(trx_dt)) as recency,
count( cust_uid) as frequency,
sum(trx_amt) as monetary
from mt_trx_rcd_f
group by cust_uid
)
select b.cust_uid,
case
when b.recency <= 9 then 3
when b.recency between 10 and 30 then 2
else 1 end as recency_score,
case
when b.frequency between 1 and 5 then 1
when b.frequency between 6 and 15 then 2
else 3 end as frequency_score,
case
when b.monetary <2000 then 1
when b.monetary between 2000 and 5000 then 2
else 3 end as monetary_score
from B b
order by cust_uid;
with B as (select cust_uid,
datediff(curdate(),max(trx_dt)) as recency,
count(distinct trx_dt) as frequency,
avg(trx_amt) as monetary
from mt_trx_rcd_f
group by cust_uid
)
select b.cust_uid,
case
when b.recency <= 9 then 3
when b.recency between 10 and 30 then 2
else 1 end as recency_score,
case
when b.frequency between 1 and 10 then 1
when b.frequency between 11 and 20 then 2
else 3 end as frequency_score,
case
when b.monetary <150 then 1
when b.monetary between 150 and 250 then 2
else 3 end as monetary_score
from B b
order by cust_uid;
with B as (select cust_uid,
datediff(curdate(),max(trx_dt)) as recency,
count(distinct trx_dt) as frequency,
sum(trx_amt) as monetary
from mt_trx_rcd_f
group by cust_uid
)
select b.cust_uid,
case
when b.recency <= 9 then 3
when b.recency between 10 and 30 then 2
else 1 end as recency_score,
case
when b.frequency between 1 and 10 then 1
when b.frequency between 11 and 20 then 2
else 3 end as frequency_score,
case
when b.monetary <150 then 1
when b.monetary between 150 and 250 then 2
else 3 end as monetary_score
from B b
order by cust_uid;
with B as (select cust_uid,
datediff(curdate(),max(trx_dt)) as recency,
count(distinct trx_dt) as frequency,
sum(trx_amt) as monetary
from mt_trx_rcd_f
group by cust_uid
)
select b.cust_uid,
case
when b.recency <= 9 then 3
when b.recency between 10 and 30 then 2
else 1 end as recency_score,
case
when b.frequency between 1 and 10 then 1
when b.frequency between 11 and 20 then 2
else 3 end as frequency_score,
case
when b.monetary <= 149 then 1
when b.monetary between 150 and 250 then 2
else 3 end as monetary_score
from B b
order by cust_uid;
with B as (select cust_uid,
datediff(curdate(),max(trx_dt)) as recency,
count(distinct trx_dt) as frequency,
sum(trx_amt) as monetary
from mt_trx_rcd_f
group by cust_uid
)
select b.cust_uid,
case
when b.recency <= 9 then 3
when b.recency between 10 and 30 then 2
else 1 end as recency_score,
case
when b.frequency between 1 and 10 then 1
when b.frequency between 11 and 20 then 2
else 3 end as frequency_score,
case
when b.monetary <= 150 then 1
when b.monetary between 151 and 250 then 2
else 3 end as monetary_score
from B b
order by cust_uid;
select cust_uid,
datediff(curdate(),max(trx_dt)) as recency,
count(distinct trx_dt) as frequency,
avg(trx_amt) as Monetary
from mt_trx_rcd_f
group by cust_uid
select cust_uid,
datediff(curdate(),max(trx_dt)) as recency,
count(cust_uid) as frequency,
avg(trx_amt) as Monetary
from mt_trx_rcd_f
group by cust_uid
select cust_uid,
datediff(curdate(),max(trx_dt)) as recency,
count(cust_uid) as frequency,
sum(trx_amt) as Monetary
from mt_trx_rcd_f
group by cust_uid
WITH TransactionFrequency AS (
SELECT cust_uid, COUNT(*) AS transaction_count
FROM mt_trx_rcd_f
GROUP BY cust_uid
),
ActiveDaysFrequency AS (
SELECT cust_uid, COUNT(DISTINCT DATE(trx_dt)) AS active_days_count
FROM mt_trx_rcd_f
GROUP BY cust_uid
),
RankedTransactionFrequency AS (
SELECT cust_uid,
transaction_count,
CAST(RANK() OVER (ORDER BY transaction_count DESC) AS SIGNED) AS transaction_rank
FROM TransactionFrequency
),
RankedActiveDaysFrequency AS (
SELECT cust_uid,
active_days_count,
CAST(RANK() OVER (ORDER BY active_days_count DESC) AS SIGNED) AS active_days_rank
FROM ActiveDaysFrequency
)
SELECT
t.cust_uid,
t.transaction_count,
t.transaction_rank,
a.active_days_count,
a.active_days_rank,
ABS(t.transaction_rank - a.active_days_rank) AS rank_difference
FROM RankedTransactionFrequency t
JOIN RankedActiveDaysFrequency a ON t.cust_uid = a.cust_uid
ORDER BY rank_difference DESC;
WITH TotalSpending AS (
SELECT cust_uid, SUM(trx_amt) AS total_amount
FROM mt_trx_rcd_f
GROUP BY cust_uid
)
SELECT *
FROM TotalSpending
ORDER BY total_amount DESC;