Selecting rows that might contain null values based on column comparison Given a table called table1 whose first few rows look like this:

Idkey DateA     DateB       FieldA    FieldB
1     '2011-06' '2011-03'   20        A
1     '2011-05' '2011-03'   20        A
1     '2011-04' '2011-03'   20        A
1     '2011-03' '2011-03'   20        A
1     '2011-02'  &lt;null&gt;     &lt;null&gt;    &lt;null&gt;
1     '2011-01'  &lt;null&gt;     &lt;null&gt;    &lt;null&gt;
2     '2011-08'  '2011-06'  30        B
2     '2011-07'  '2011-06'  30        B
2     '2011-06'  '2011-06'  30        B
2     '2011-05'  &lt;null&gt;     &lt;null&gt;    &lt;null&gt;


I would like to select all rows for unique Idkeys values starting from the point when DateA = DateB and DateB is in the year 2011. That is, I would like: 

Idkey DateA     DateB       FieldA    FieldB
1     '2011-03' '2011-03'   20        A
1     '2011-02'  &lt;null&gt;     &lt;null&gt;    &lt;null&gt;
1     '2011-01'  &lt;null&gt;     &lt;null&gt;    &lt;null&gt;
2     '2011-06'  '2011-06'  30        B
2     '2011-05'  &lt;null&gt;     &lt;null&gt;    &lt;null&gt;


I tried something like:

with subt as (
  select Idkey from table1
  where DateB between '2011-01' AND '2011-12'
  group by Idkey 
  order by 1
)

select Idkey, DateA, DateB, FieldA, FieldB,
from table1 b
join subt 
on subt.Idkey = b.Idkey
where b.DateA &lt;= nvl(DateB,b.DateA);


But it is not giving me what I want. Any ideas?
You can also try a solution based on window MAX():

WITH
  FirstMatches AS
  (
    SELECT
      *,
      MAX(CASE WHEN DateA = DateB AND DateB BETWEEN '2011-01' AND '2011-12' THEN DateA END)
        OVER (PARTITION BY Idkey) AS FirstDateA
    FROM
      table1
  )
SELECT
  Idkey,
  DateA,
  DateB,
  FieldA,
  FieldB
FROM
  FirstMatches
WHERE
  DateA &lt;= FirstDateA
;


This solution assumes that the order of rows implied by "starting from" is DateA DESC, same as ypercubeᵀᴹ's solution.

Note that while the query will be looking for rows with matching dates specifically among those where DateB is in the year 2011, the output may include rows with DateA dates earlier than 2011. If you only meant to get rows where DateA is in 2011, you can specify that as a WHERE filter in the CTE, also removing the range filter for DateB in the CASE expression as no longer necessary:

WITH
  FirstMatches AS
  (
    SELECT
      *,
      MAX(CASE WHEN DateA = DateB THEN DateA END)
        OVER (PARTITION BY Idkey) AS FirstDateA
    FROM
      table1
    WHERE
      DateA BETWEEN '2011-01' AND '2011-12'
  )
SELECT
  Idkey,
  DateA,
  DateB,
  FieldA,
  FieldB
FROM
  FirstMatches
WHERE
  DateA &lt;= FirstDateA
;