• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

222
Views
coalesce with filter function in postgresql

I am running this query:

SELECT
     SUM(PRICE) FILTER (my filter's parameters)

and when there's no record it returns <null>

then I tried:

SELECT
       COALESCE(SUM(PRICE),0) FILTER (my filter's parameters)

But then I get an error FILTER not valid for coalesce function

my filter's parameters are time window, like this:

FILTER(WHERE date(order_date) >= (cast(current_date as date) - interval '1' day)) as day1

How can I replace <null> by 0 when there's no record?

over 3 years ago · Santiago Trujillo
1 answers
Answer question

0

You should try wrapping the entire FILTER expression in COALESCE, e.g.

SELECT
    COALESCE(SUM(PRICE) FILTER (my filter's parameters), 0) AS sum_price
over 3 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error