Home  >  Q&A  >  body text

Optimize your use of interval functions

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粉321584263P粉321584263369 days ago551

reply all(1)I'll reply

  • P粉514001887

    P粉5140018872023-09-15 14:28:09

    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;

    reply
    0
  • Cancelreply