 SELECT j.aufnr,
        CASE
            WHEN (j.mengn * a.atwrt1)::numeric(18,3) IS NULL THEN j.mengn::numeric(18,3)
            ELSE (j.mengn * a.atwrt1)::numeric(18,3)
        END AS mengn,
        CASE
            WHEN (j.fmeng1 * a.atwrt1)::numeric(18,3) IS NULL THEN j.fmeng1::numeric(18,3)
            ELSE (j.fmeng1 * a.atwrt1)::numeric(18,3)
        END AS fmeng1,
    j.rate1,
        CASE
            WHEN (j.fmeng2 * a.atwrt1)::numeric(18,3) IS NULL THEN j.fmeng2::numeric(18,3)
            ELSE (j.fmeng2 * a.atwrt1)::numeric(18,3)
        END AS fmeng2,
    j.rate2,
        CASE
            WHEN (j.fmeng3 * a.atwrt1)::numeric(18,3) IS NULL THEN j.fmeng3::numeric(18,3)
            ELSE (j.fmeng3 * a.atwrt1)::numeric(18,3)
        END AS fmeng3,
    j.rate3,
    a.atwrt1 AS modulus
   FROM ( SELECT c.mengn,
            c.aufnr,
            c.matnr,
            b.fmeng1,
            b.rate1,
            d.fmeng2,
            d.rate2,
            e.fmeng3,
            e.rate3
           FROM ( SELECT a_1.menge_1 + a_1.menge_2 AS mengn,
                    a_1.aufnr,
                    a_1.matnr
                   FROM ( SELECT mseg.aufnr,
                            mseg.matnr,
                            sum(
                                CASE
                                    WHEN mseg.bwart = '102'::text THEN mseg.menge::numeric / (-1)::numeric
                                    ELSE 0::numeric
                                END) AS menge_1,
                            sum(
                                CASE
                                    WHEN mseg.bwart = '101'::text THEN mseg.menge::numeric
                                    ELSE 0::numeric
                                END) AS menge_2
                           FROM mseg
                          WHERE (mseg.bwart = ANY (ARRAY['101'::text, '102'::text])) AND mseg.aufnr <> ''::text
                          GROUP BY mseg.aufnr, mseg.matnr) a_1) c
             LEFT JOIN ( SELECT a_1.menge_1 + a_1.menge_2 AS fmeng1,
                    a_1.aufnr AS aufnr1,
                    afko.gamng,
                    afko.plnbez,
                        CASE
                            WHEN ((a_1.menge_1 + a_1.menge_2) / afko.gamng)::numeric(10,3) > 1::numeric THEN '1.000%'::text
                            WHEN ((a_1.menge_1 + a_1.menge_2) / afko.gamng)::numeric(10,3) <= 1::numeric THEN ((a_1.menge_1 + a_1.menge_2) / afko.gamng)::numeric(10,3) || '%'::text
                            ELSE NULL::text
                        END AS rate1
                   FROM ( SELECT mseg.aufnr,
                            sum(
                                CASE
                                    WHEN mseg.bwart = '102'::text THEN mseg.menge::numeric / (-1)::numeric
                                    ELSE 0::numeric
                                END) AS menge_1,
                            sum(
                                CASE
                                    WHEN mseg.bwart = '101'::text THEN mseg.menge::numeric
                                    ELSE 0::numeric
                                END) AS menge_2
                           FROM mseg
                             LEFT JOIN vbep ON vbep.vbeln = mseg.kdauf AND vbep.posnr = mseg.kdpos
                          WHERE (mseg.bwart = ANY (ARRAY['101'::text, '102'::text])) AND (to_number(to_char(mseg.budat_mkpf, 'yyyymmdd'::text), '99999999'::text) < to_number(vbep.edatu, '99999999'::text) OR (to_number(to_char(mseg.budat_mkpf, 'yyyymmdd'::text), '99999999'::text) = to_number(vbep.edatu, '99999999'::text) AND mseg.cputm_mkpf < '12:00:00'::time without time zone))
                          GROUP BY mseg.aufnr) a_1
                     LEFT JOIN afko ON a_1.aufnr = afko.aufnr) b ON c.aufnr = b.aufnr1
             LEFT JOIN ( SELECT a_1.menge_1 + a_1.menge_2 AS fmeng2,
                    a_1.aufnr AS aufnr2,
                    afko.gamng,
                    afko.plnbez,
                        CASE
                            WHEN ((a_1.menge_1 + a_1.menge_2) / afko.gamng)::numeric(10,3) > 1::numeric THEN '1.000%'::text
                            WHEN ((a_1.menge_1 + a_1.menge_2) / afko.gamng)::numeric(10,3) <= 1::numeric THEN ((a_1.menge_1 + a_1.menge_2) / afko.gamng)::numeric(10,3) || '%'::text
                            ELSE NULL::text
                        END AS rate2
                   FROM ( SELECT mseg.aufnr,
                            sum(
                                CASE
                                    WHEN mseg.bwart = '102'::text THEN mseg.menge::numeric / (-1)::numeric
                                    ELSE 0::numeric
                                END) AS menge_1,
                            sum(
                                CASE
                                    WHEN mseg.bwart = '101'::text THEN mseg.menge::numeric
                                    ELSE 0::numeric
                                END) AS menge_2
                           FROM mseg
                             LEFT JOIN vbep ON vbep.vbeln = mseg.kdauf AND vbep.posnr = mseg.kdpos
                          WHERE (mseg.bwart = ANY (ARRAY['101'::text, '102'::text])) AND (to_number(to_char(mseg.budat_mkpf, 'yyyymmdd'::text), '99999999'::text) < to_number(vbep.edatu, '99999999'::text) OR (to_number(to_char(mseg.budat_mkpf, 'yyyymmdd'::text), '99999999'::text) = to_number(vbep.edatu, '99999999'::text) AND mseg.cputm_mkpf < '16:00:00'::time without time zone))
                          GROUP BY mseg.aufnr) a_1
                     LEFT JOIN afko ON a_1.aufnr = afko.aufnr) d ON c.aufnr = d.aufnr2
             LEFT JOIN ( SELECT a_1.menge_1 + a_1.menge_2 AS fmeng3,
                    a_1.aufnr AS aufnr3,
                    afko.gamng,
                    afko.plnbez,
                        CASE
                            WHEN ((a_1.menge_1 + a_1.menge_2) / afko.gamng)::numeric(10,3) > 1::numeric THEN '1.000%'::text
                            WHEN ((a_1.menge_1 + a_1.menge_2) / afko.gamng)::numeric(10,3) <= 1::numeric THEN ((a_1.menge_1 + a_1.menge_2) / afko.gamng)::numeric(10,3) || '%'::text
                            ELSE NULL::text
                        END AS rate3
                   FROM ( SELECT mseg.aufnr,
                            sum(
                                CASE
                                    WHEN mseg.bwart = '102'::text THEN mseg.menge::numeric / (-1)::numeric
                                    ELSE 0::numeric
                                END) AS menge_1,
                            sum(
                                CASE
                                    WHEN mseg.bwart = '101'::text THEN mseg.menge::numeric
                                    ELSE 0::numeric
                                END) AS menge_2
                           FROM mseg
                             LEFT JOIN vbep ON vbep.vbeln = mseg.kdauf AND vbep.posnr = mseg.kdpos
                          WHERE (mseg.bwart = ANY (ARRAY['101'::text, '102'::text])) AND to_number(to_char(mseg.budat_mkpf, 'yyyymmdd'::text), '99999999'::text) <= to_number(vbep.edatu, '99999999'::text)
                          GROUP BY mseg.aufnr) a_1
                     LEFT JOIN afko ON a_1.aufnr = afko.aufnr) e ON c.aufnr = e.aufnr3) j
     LEFT JOIN ( SELECT (to_number(split_part(ausp.atwrt, '*'::text, 1), '9999'::text) / 1000::numeric)::numeric(18,3) AS atwrt1,
            ausp.atwrt,
            mara.matnr
           FROM mara
             LEFT JOIN ausp ON ausp.objek = mara.matnr
          WHERE ausp.atinn = '0000001032'::text AND (mara.matkl = ANY (ARRAY['6005'::text, '6011'::text, '6033'::text, '6034'::text])) AND mara.meins = 'EA'::text) a ON j.matnr = a.matnr