N
N
newbie_python2021-07-05 17:25:23
PostgreSQL
newbie_python, 2021-07-05 17:25:23

How to display the difference between two fields?

Hello!
There is such a request. Tell me, please, how can I implement the output of a new field, in which the value of the difference between the fields "Total children" will be "18.1"?
The field "Total children" itself is not needed as a result at all, it would be necessary to specifically display the value of the difference between the two fields instead of it.
Request link: https://ps.tmpc.ru/ac78abbfae86

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
galaxy, 2021-07-05
@newbie_python

Well, write the difference right away:

select
                region_code, ...
                sum(case when status_id = 2 then 1 else 0 end) - sum(case when consul_need_id = 3 then 1 else 0 end) as diff, ...

Or wrap everything in another select, and then do what you want:
select t.*,
      "Всего детей",
      "Всего детей" - "18.1",
      "18.1" * "До года жизни" / ("7.1" + "8.1")
      ...
  from (
      select
                region_code, sum(case when age_group_id < 13 then 1 else 0 end) as "До года жизни",
      ....
  ) t

The only thing I didn’t understand was the purpose of the structures (sum(case when ... end),0)- why do you need it?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question