select dh.d2,dh.d3,dh.d5,dh.typeid, (select d2 from xy_dwinfo where d21=dh.xyid) dwd2,(select d21 from xy_dwinfo where d21=dh.xyid) d21 from dw_history dh,
(select xyid,MAX(to_date(d3,'yyyy/mm/dd')) md from dw_history group by xyid) mh where dh.xyid=mh.xyid and (to_date(dh.d3,'yyyy/mm/dd'))=mh.md and (to_date(dh.d3,'yyyy/mm/dd')-SYSDATE)>30 order by d21