with sa_salestarget as (
    SELECT point, sum(target_l) target_l, sum(target_h) target_h, year,sys_enterpriseid from sa_salestarget
    WHERE  type = '月' and targettype='企业目标' and siteid=$siteid$ and $where$
    GROUP  BY point ,year,sys_enterpriseid
    )
SELECT DISTINCT m.year,
                m.sys_enterpriseid,
                t2.agentnum,
                t1.enterprisename,
                t4.areaname,
                m1.target_l + m2.target_l + m3.target_l + m4.target_l + m5.target_l + m6.target_l +
                m7.target_l + m8.target_l + m9.target_l + m10.target_l + m11.target_l +
                m12.target_l                               y1l,
                m1.target_h + m2.target_h + m3.target_h + m4.target_h + m5.target_h + m6.target_h +
                m7.target_h + m8.target_h + m9.target_h + m10.target_h + m11.target_h +
                m12.target_h                               y1h,
                m1.target_l + m2.target_l + m3.target_l    s1l,
                m1.target_h + m2.target_h + m3.target_h    s1h,
                m4.target_l + m5.target_l + m6.target_l    s2l,
                m4.target_h + m5.target_h + m6.target_h    s2h,
                m7.target_l + m8.target_l + m9.target_l    s3l,
                m7.target_h + m8.target_h + m9.target_h    s3h,
                m10.target_l + m11.target_l + m12.target_l s4l,
                m10.target_h + m11.target_h + m12.target_h s4h,
                m1.target_l                                m1l,
                m1.target_h                                m1h,
                m2.target_l                                m2l,
                m2.target_h                                m2h,
                m3.target_l                                m3l,
                m3.target_h                                m3h,
                m4.target_l                                m4l,
                m4.target_h                                m4h,
                m5.target_l                                m5l,
                m5.target_h                                m5h,
                m6.target_l                                m6l,
                m6.target_h                                m6h,
                m7.target_l                                m7l,
                m7.target_h                                m7h,
                m8.target_l                                m8l,
                m8.target_h                                m8h,
                m9.target_l                                m9l,
                m9.target_h                                m9h,
                m10.target_l                               m10l,
                m10.target_h                               m10h,
                m11.target_l                               m11l,
                m11.target_h                               m11h,
                m12.target_l                               m12l,
                m12.target_h                               m12h
from sa_salestarget m
         LEFT JOIN sa_salestarget m1 ON m1.year = m.year and m1.point = 1 and m.sys_enterpriseid = m1.sys_enterpriseid
         LEFT JOIN sa_salestarget m2 ON m2.year = m.year and m2.point = 2 and m.sys_enterpriseid = m2.sys_enterpriseid
         LEFT JOIN sa_salestarget m3 ON m3.year = m.year and m3.point = 3 and m.sys_enterpriseid = m3.sys_enterpriseid
         LEFT JOIN sa_salestarget m4 ON m4.year = m.year and m4.point = 4 and m.sys_enterpriseid = m4.sys_enterpriseid
         LEFT JOIN sa_salestarget m5 ON m5.year = m.year and m5.point = 5 and m.sys_enterpriseid = m5.sys_enterpriseid
         LEFT JOIN sa_salestarget m6 ON m6.year = m.year and m6.point = 6 and m.sys_enterpriseid = m6.sys_enterpriseid
         LEFT JOIN sa_salestarget m7 ON m7.year = m.year and m7.point = 7 and m.sys_enterpriseid = m7.sys_enterpriseid
         LEFT JOIN sa_salestarget m8 ON m8.year = m.year and m8.point = 8 and m.sys_enterpriseid = m8.sys_enterpriseid
         LEFT JOIN sa_salestarget m9 ON m9.year = m.year and m9.point = 9 and m.sys_enterpriseid = m9.sys_enterpriseid
         LEFT JOIN sa_salestarget m10 ON m10.year = m.year and m10.point = 10 and m.sys_enterpriseid = m10.sys_enterpriseid
         LEFT JOIN sa_salestarget m11 ON m11.year = m.year and m11.point = 11 and m.sys_enterpriseid = m11.sys_enterpriseid
         LEFT JOIN sa_salestarget m12 ON m12.year = m.year and m12.point = 12 and m.sys_enterpriseid = m12.sys_enterpriseid
         LEFT JOIN sys_enterprise t1 on t1.sys_enterpriseid=m.sys_enterpriseid
         LEFT JOIN sa_agents t2 on t1.sys_enterpriseid=t2.sys_enterpriseid and t1.siteid=t2.siteid
         LEFT JOIN sys_enterprise_tradefield t3 on t1.sys_enterpriseid=t3.sys_enterpriseid and t1.siteid=t3.siteid
         LEFT JOIN sa_salearea t4 on t3.sa_saleareaid=t4.sa_saleareaid and t3.siteid=t4.siteid
ORDER BY m.year desc