Optimize your use of interval functions
P粉321584263
P粉321584263 2023-09-14 16:24:44
0
1
615

I have to do the following:

Rank users with persona = 'z' in order from highest to lowest last week.

I wrote the following code:

SELECT U.*, SUM(T.amount) AS total_spends
FROM User U
JOIN Transact T ON U.id = T.created_by
WHERE U.persona = 'Z' AND T.date_created >= CURRENT_DATE - INTERVAL '1 week'
GROUP BY U.id
ORDER BY total_spends DESC;

However, I get the following error: Error: near "'1 week'": syntax error

Any help would be greatly appreciated.

P粉321584263
P粉321584263

reply all(1)
P粉514001887

SELECT U.*, SUM(T.amount) AS total_spends FROM User U JOIN Transact T ON U.id = T.created_by WHERE U.persona = 'Z' AND T.date_created >= DATE_SUB(CURRENT_DATE, INTERVAL 1 WEEK) GROUP BY U.id ORDER BY total_spends DESC;

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template