I am new to sql and sorry if I posted on wrong forum. Please do let me know so I will refrain from posting here next time. Thank You
My answer was marked as incorrect for this one , i don't know what went wrong. plz help
This was the question: Retrieve the customer id that has their spending increase every month from january of 2021 to march of 2021 (that is from 202101-202103))
This is the table
I got 412317 as the answer
CREATE TABLE customer_spending (
CUSTOMER_ID INT,
MONTH INT,
SPENT DECIMAL(10, 2)
);
INSERT INTO customer_spending (CUSTOMER_ID, MONTH, SPENT)
VALUES
(304721, 202101, 4060),
(412317, 202101, 5530),
(483751, 202101, 1590),
(252938, 202101, 3660),
(392678, 202101, 2810),
(590579, 202101, 5450),
(865495, 202101, 5010),
(201710, 202101, 2850),
(310986, 202101, 7400),
(966525, 202101, 5120),
(273645, 202101, 7650),
(825853, 202101, 5460),
(995242, 202101, 5100),
(633255, 202101, 2060),
(122005, 202101, 4890),
(948512, 202101, 4620),
(358935, 202101, 8510),
(937737, 202101, 3980),
(641096, 202101, 1840),
(316995, 202101, 3350),
(901775, 202101, 2910),
(304721, 202102, 3290),
(412317, 202102, 5580),
(483751, 202102, 8990),
(252938, 202102, 2750),
(392678, 202102, 7480),
(590579, 202102, 6690),
(865495, 202102, 3610),
(201710, 202102, 1850),
(310986, 202102, 6960),
(966525, 202102, 6250),
(273645, 202102, 7590),
(825853, 202102, 3870),
(995242, 202102, 7650),
(633255, 202102, 6990),
(122005, 202102, 1420),
(948512, 202102, 3180),
(358935, 202102, 3290),
(495105, 202102, 3820),
(888277, 202102, 1280),
(315565, 202102, 1710),
(865905, 202102, 7250),
(517038, 202102, 7050),
(763039, 202102, 5830),
(304721, 202103, 3700),
(412317, 202103, 8170),
(483751, 202103, 2020),
(252938, 202103, 7660),
(392678, 202103, 1770),
(590579, 202103, 4970),
(865495, 202103, 6660),
(201710, 202103, 3020),
(310986, 202103, 5230),
(966525, 202103, 3750),
(273645, 202103, 6580),
(851939, 202103, 5330),
(500345, 202103, 6520),
(611233, 202103, 8150),
(122005, 202103, 5860),
(948512, 202103, 3300),
(358935, 202103, 8620),
(495105, 202103, 6870),
(888277, 202103, 3280),
(315565, 202103, 6670),
(865905, 202103, 8280),
(517038, 202103, 3970),
(763039, 202103, 5890);
SQL code :
SELECT CUSTOMER_ID, SUM(SPENT) AS total_spent
FROM customer_spending
WHERE MONTH IN (202101, 202102, 202103)
GROUP BY CUSTOMER_ID
HAVING MAX(CASE WHEN MONTH = 202101 THEN SPENT END) <
MAX(CASE WHEN MONTH = 202102 THEN SPENT END) AND
MAX(CASE WHEN MONTH = 202102 THEN SPENT END) <
MAX(CASE WHEN MONTH = 202103 THEN SPENT END);