-
Notifications
You must be signed in to change notification settings - Fork 0
/
Question 3.sql
56 lines (48 loc) · 2.21 KB
/
Question 3.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
You are given the below dataset, Which is the daily purchasing transactions for customers.
You are required to answer two questions:
a- What is the maximum number of consecutive days a customer made purchases?
b- On average, How many days/transactions does it take a customer to reach a spent threshold of 250 L.E?
*/
--Creating the table
CREATE TABLE transactions
(
Cust_Id NUMBER(20),
Calendar_Dt VARCHAR2(50),
Amt_LE NUMBER (10,3)
);
------------------------------------------
--a- What is the maximum number of consecutive days a customer made purchases?
WITH difference AS (
SELECT Cust_Id, calendar_dt,
calendar_dt - ROW_NUMBER() OVER (PARTITION BY Cust_Id ORDER BY calendar_dt) AS "Group"
FROM transactions
), count_days as(
SELECT cust_id,
COUNT("Group") OVER (PARTITION BY "Group", cust_id) AS count_group
FROM difference
)
SELECT cust_id, MAX (count_group) as "Max Consecutive Day"
FROM count_days
GROUP BY cust_id
ORDER BY cust_id;
----------------------------------------------------------------------------------------------------------------------
--b- On average, How many days/transactions does it take a customer to reach a spent threshold of 250 L.E?
WITH customers_sales AS (
SELECT cust_id , calendar_dt ,
COUNT(calendar_dt) OVER (PARTITION BY cust_id ORDER BY TO_DATE(calendar_dt, 'YYYY-MM-DD')) AS count_days , Amt_LE ,
SUM(Amt_LE) OVER (PARTITION BY cust_id ORDER BY TO_DATE(calendar_dt, 'YYYY-MM-DD')) AS total_amount
FROM transactions
) ,
high_amounts AS (
SELECT cust_id , count_days ,total_amount
FROM customers_sales
WHERE total_amount >=250
)
SELECT ROUND(AVG (count_days), 2) AS avg_days
FROM high_amounts
WHERE (cust_id, total_amount) IN (SELECT cust_id , MIN(total_amount)
FROM high_amounts
GROUP BY cust_id
) ;
-----------------------------------------------------------------------------------------------------------------------