I'm having real trouble getting the sub query join to the main query in the where clause somewhere along the way.
Query that works when explicitly defining the field:
SELECT m.field1, m.field2, m.field3, m.myfield, etc etc
(SELECT aa.daysfromprev12 FROM(
(SELECT subsubm.myfield, MAX(subsubm.date_to)-(SELECT MAX(add_months( to_date(subsubsubm.date_from), -12 )) FROM maintable subsubsubm WHERE subsubsubm.myfield= subsubm.myfield) AS daysfromprev12,
row_number() OVER (ORDER BY (MAX(subsubm.date_to)-(SELECT MAX(add_months( to_date(subsubsubm.date_from), -12 )) FROM maintable subsubsubm WHERE subsubsubm.myfield= subsubm.myfield)) DESC) rn
FROM maintable subsubm
WHERE subsubm.myfield = '123456'
GROUP BY subsubm.myfield, subsubm.absence_id) aa)
where aa.myfield = '123456' and aa.rn = 2)
AS dayss
FROM maintable m
where m.myfield = '123456'
How can I replace subsubm.myfield = '123456' & aa.myfield = '123456' to reference the main query = m.myfield
where subsubm.myfield = subm.myfield, but defines it on the end of query(line 8) ? I am not sure if its possible in Oracle