Query in PostgreSQL with filter in 2 different columns

1

I have a problem when doing a select with filters to several columns. I have a table with different age groups (days, months, years) and I need to make a consultation where only people who are younger than 2 bring me but the months are greater than 5, that is, people with 6 months of age to 1 year.

select * 
from dean_eapg.v_cuenta_total_evaluados
where  edad = '1'
or (meses > '5' and meses < '12')

I've tried with this query and it does not work for me.

    
asked by Angel Gutierrez 28.11.2017 в 14:43
source

2 answers

0
  

I need to ask a question where only the people who bring them   years are less than 2 but the months are greater than 5   that is, people with 6 months of age to 1 year.

According to what you describe, you want people from 6 months of age (years = 0 and months > = 6) up to one year (years = 1 and months = 0). I guess that a person who has exactly a year can be described as either (years = 1 and months = 0) or as (years = 0 and months = 12). p>

In that case it would be:

SELECT * 
FROM dean_eapg.v_cuenta_total_evaluados
WHERE  (edad::integer = 1 AND meses::integer = 0) -- exactamente 1 año
OR (edad::integer = 0 AND meses::integer > 5) -- 0 años y 6 a 12 meses
    
answered by 30.11.2017 в 13:41
0

This problem can be simplified by making a calculation between edad (which I understand to be years) and meses , and then recovering people between 6 and 12 months old.

Something like:

select * 
  from dean_eapg.v_cuenta_total_evaluados
 where (edad::integer * 12 + meses::integer) between 6 and 12 

EYE, if you have another column with days, this will still bring you people who are one year old and a few days old.

    
answered by 11.12.2018 в 00:34