0

i have table like this

id | name | planned_amount | actual | avail

1   ABC     100                123    100
2   DEF     200                345    200
3   uytuyn  9000               311    300
4   oiui    890                200    200

above data output from query like this:

select a.name, 
       e.planned_amount, 
       dd.sum as actual, 
       e.planned_amount - dd.sum as avail 
from crossovered_budget_lines as e, 
     account_analytic_account as a, 
     (select analytic_account_id, sum(debit+credit)  
      from account_move_line 
      where date between '2021-01-01' and '2021-05-01' 
      group by analytic_account_id) as dd 
where e.analytic_account_id = a.id 
  and dd.analytic_account_id = .id
 limit 5

when range data from table account_move_line is null ( no output data ), i want to output like this, column actual give 0 value :

 id | name | planned_amount | actual | avail
    
    1   ABC     100                0    100
    2   DEF     200                0    200
    3   uytuyn  9000               0    300
    4   oiui    890                0    200

how to query to produce the output above ? iam stuck , thanks

5
  • Dupe: postgresql return 0 if returned value is null Commented Sep 2, 2021 at 7:40
  • how to implement from my query ? Commented Sep 2, 2021 at 7:43
  • As the answer says, use coalesce. Basically select ... dd.sum ... becomes select coalesce(dd.sum, 0) ... Commented Sep 2, 2021 at 7:47
  • give me example from my query Commented Sep 2, 2021 at 7:52
  • 2
    You are looking for an outer join - but to use it, you have to ditch the ancient implicit join conditions in the WHERE clause and switch to the "modern" (nearly 30 years old) explicit JOIN and LEFT JOIN operators Commented Sep 2, 2021 at 9:24

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.