<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.com.victorysoft.business.calc.dao.WellToolDataSyncDao">

  <select id="selectList" resultType="WellTool" fetchSize="20000">
    select t1.jh,
    t1.ny,
    t1.zscw,
    t1.kgzt,
    (select dmmc from fla15 where dm = t1.bzdm) tzyy,
    t2.zyrq,
    t2.zymd,
    t2.szgd,
    t2.csqkms,
    t2.yjylx,
    t2.yjyyl
    from (select jh,
    to_char(#{endDate}, 'yyyymm') ny,
    max(zscw) zscw,
    case
    when sum(scsj) > 24 and sum(rzsl) >= 0.5 then
    '开井'
    else
    '停井'
    end kgzt,
    max(bzdm) bzdm
    from ys_dba02 t
    where rq between #{startDate} and #{endDate}
    group by jh) t1
    left join (select a.jh,
    max(a.wgrq) zyrq,
    max((select dmmc from fla16 where dm = a.cslb)) zymd,
    max(b.szgd) szgd,
    max(b.fcwms) csqkms,
    max((select dmmc from fla81 where dm = c.yjydm)) yjylx,
    max(c.yjyyl) yjyyl
    from (select t.*,
    row_number() over(partition by jh order by wgrq desc) rn
    from ys_ddh01 t
    where wgrq &lt;= #{endDate}) a
    left join ys_ddb06 b on a.jh = b.jh
    and a.wgrq = b.wgrq
    left join ys_ddb02 c on a.jh = c.jh
    and a.wgrq = c.wgrq
    where rn = 1 group by a.jh) t2 on t1.jh = t2.jh
  </select>

</mapper>