Use conditional to check dates if not in a search column with null value

Asked

Viewed 100 times

0

I have the following tables in BD Oracle 11g

irrigation
----------------
militet_applied, 
close_date

rainfall
---------------
rain_index, 
reading_date

And I would like to make a select similar to that:

select r.rain_index, 
       i.militet_applied
from irrigation i, rainfall r

But in my where clause I would like to do the following: - If r.reading_date = i.close_date bring me the values (i.militet_applied), but also if r.reading_date be equal/not present in the column i.close_cycle, bring me null values (i.militet_applied).

I’m counting on you in this doubt.

  • 1

    It became a little difficult to understand the second part, from the "but also if...". This column i.close_cycle is another column of irrigation? Or was it a typo and refers to itself i.close_date?

  • e r.reading_date = i.close_date bring me the values (i.militet_applied), , but also if r.reading_date is equal/does not have in the i.close_cycle column, bring me null values (i.militet_applied). SELECT ... (CASE WHEN e r.reading_date = i.close_date THEN i.militet_applied ELSE NULL END but as you said @diegofm got confused

  • Sorry for the error, yes I was referring to i.close_date

1 answer

0


Josefosad, although the question is confused, following exactly what you reported, one option is this:

SELECT 
  R.RAIN_INDEX, 
  CASE 
    WHEN R.READING_DATE = I.CLOSE_CYCLE OR R.READING_DATE NOT IN (SELECT I2.CLOSE_CYCLE FROM IRRIGATION I2) THEN NULL
    ELSE I.MILITET_APPLIED END AS MILITET_APPLIED
FROM 
  RAINFALL R
  JOIN IRRIGATION I ON R.READING_DATE = I.CLOSE_DATE;

If you want the column MILITET_APPLIED to be null only when there is no match at the junction (R.READING_DATE = I.CLOSE_DATE), use LEFT JOIN:

SELECT 
  R.RAIN_INDEX, 
  I.MILITET_APPLIED
FROM 
  RAINFALL R
  LEFT JOIN IRRIGATION I ON R.READING_DATE = I.CLOSE_DATE;
  • Very grateful, that’s exactly what I wanted.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.