Returne SUM last 100 approved payments

Hi,
I’m a new metabase user and i’m having some difficulties on how to filter the last (most recent) 100 approved payments in my code.

SELECT SUM(value)
FROM ad_moip
WHERE approved_at is not NULL
ORDER BY approved_at asc

Hi @JeanGuimaraes
I’m not sure I understand, but depending on which type of database you’re querying, you should just add LIMIT 100 to the end of the query.

Thanks Flamber, but I already tried using this “Limit 100” function, but MetaBase does not return.

I would like to add (SUM) the last 100 payments received (approved) from the database.

OBs: sorry my bad english.

@JeanGuimaraes I would recommend that you ask in a forum specific to your database type, since your question is not specific to Metabase. Example stackoverflow.com has forums for most database types.

Your original query’s SUM function will only ever return a single number, being the sum of all approved orders. Your ORDER BY is in the wrong direction too - you’re returning the oldest first.
Not sure what your database is, but in MS SQL, your query would need to be something like:

select SUM(a.value) from 
( SELECT TOP 100 value 
FROM ad_moip
WHERE approved_at is not NULL
ORDER by approved_at desc
) a

Thanks for Help, Andrew.
But the MetaBase accused error with this Query.
What is wrong? :neutral_face:

My version of MetaBase: v0.36.4

Your using MySQL. The syntax is different.
Try this:
https://www.w3schools.com/sqL/sql_top.asp

Guys, I got it!

SELECT SUM(value)
FROM(SELECT value, approved_at
FROM ad_moip
WHERE approved_at IS NOT NULL
GROUP BY approved_at, value
ORDER BY approved_at DESC
LIMIT 100) AS table_tmp

1 Like