Razan Aldossary
Razan Aldossary

Reputation: 259

Sum with conditions in Snowflake

I'm trying to write a query using Snowflake where I want to sum the total payment of a user who have multiple accounts (one email can create multiple accounts, yes).

If at least one account is currently active, then I want to sum up the total paid money(all payment history for that user), whether the account is active or not.

Columns I'm using:

CustomerEmail
Active
Total_Paid

Upvotes: 1

Views: 3361

Answers (2)

Lukasz Szozda
Lukasz Szozda

Reputation: 175606

Using HAVING and COUNT_IF:

SELECT customeremail, SUM(total_paid) AS total
FROM test
GROUP BY customeremail
HAVING COUNT_IF(active) > 0;

Upvotes: 1

Gokhan Atil
Gokhan Atil

Reputation: 10059

Something like this?

select customeremail, sum(total_paid)
from test
where customeremail IN (select customeremail from test where active =true )
group by customeremail;

Upvotes: 3

Related Questions