SQL codeselect pluno = a.pluno, pluname = a.pluname, spec = a.spec, pkunit = a.pkunit, cursprc = a.cursprc, slprc = a.slprc, moon1qty= m1.qty, moon2qty= m2.qty, moon3qty= m3.qty, moon4qty= m4.qty, moon5qty= m5.qty, moon6qty= m6.qty, moon7qty= m7.qty, moon8qty= m8.qty, moon9qty= m9.qty, moon10qty= m10.qty, moon11qty= m11.qty, moon12qty= m12.qty, salessum = m1.qty + m2.qty + m3.qty + m4.qty + m4.qty + m5.qty + m6.qty + m7.qty + m8.qty + m9.qty + m10.qty + m11.qty + m12.qty, saleamont= cursprc * salessum, maori = (m1.slnet+m2.slnet+m3.slnet+m4.slnet+m5.slnet+m6.slnet+m7.slnet+m8.slnet+m9.slnet+m10.slnet+m11.slnet+m12.slnet) - (curcprc * salessum), mgnrat = maori / (m1.slnet+m2.slnet+m3.slnet+m4.slnet+m5.slnet+m6.slnet+m7.slnet+m8.slnet+m9.slnet+m10.slnet+m11.slnet+m12.slnet) * 100, endqty = b.qty, endmont = b.qty * cursprc, endmoney = b.qty * slprc, zzl = saleamont / endmoneyfrom ( select pluno = max(a.pluno), pluname=a.pluname, spec = a.spec, pkunit = a.pkunit, curcsprc = b.curcsprc, slprc = b.slprc from basplumain a, baspluprc b where a.pluid = b.pluid group by a.pluid )a join ( select qty = sum(b.qty), slamt = sum(b.slamt), slnet = sum(b.slnet), disamt = sum(b.disamt) from doc21bf a, doc21dt b where a.docno = b.docno and DOCDAT >= '2011-01-01' AND DOCDAT <= '2011-01-31' group by b.pluid )m1 on a.pluid = m1.pluid left join ( select qty = sum(b.qty), slamt = sum(b.slamt), slnet = sum(b.slnet), disamt = sum(b.disamt) from doc21bf a, doc21dt b where a.docno = b.docno and DOCDAT >= '2011-02-01' AND DOCDAT <= '2011-02-28' group by b.pluid )m2 on a.pluid = m2.pluid left join ( select qty = sum(b.qty), slamt = sum(b.slamt), slnet = sum(b.slnet), disamt = sum(b.disamt) from doc21bf a, doc21dt b where a.docno = b.docno and DOCDAT >= '2011-03-01' AND DOCDAT <= '2011-03-31' group by b.pluid )m3 on a.pluid = m3.pluid left join ( select qty = sum(b.qty), slamt = sum(b.slamt), slnet = sum(b.slnet), disamt = sum(b.disamt) from doc21bf a, doc21dt b where a.docno = b.docno and DOCDAT >= '2011-04-01' AND DOCDAT <= '2011-04-30' group by b.pluid )m4 on a.pluid = m4.pluid left join ( select qty = sum(b.qty), slamt = sum(b.slamt), slnet = sum(b.slnet), disamt = sum(b.disamt) from doc21bf a, doc21dt b where a.docno = b.docno and DOCDAT >= '2011-05-01' AND DOCDAT <= '2011-05-31' group by b.pluid )m5 on a.pluid = m5.pluid left join ( select qty = sum(b.qty), slamt = sum(b.slamt), slnet = sum(b.slnet), disamt = sum(b.disamt) from doc21bf a, doc21dt b where a.docno = b.docno and DOCDAT >= '2011-06-01' AND DOCDAT <= '2011-06-30' group by b.pluid )m6 on a.pluid = m6.pluid left join ( select qty = sum(b.qty), slamt = sum(b.slamt), slnet = sum(b.slnet), disamt = sum(b.disamt) from doc21bf a, doc21dt b where a.docno = b.docno and DOCDAT >= '2011-07-01' AND DOCDAT <= '2011-07-31' group by b.pluid )m7 on a.pluid = m7.pluid left join ( select qty = sum(b.qty), slamt = sum(b.slamt), slnet = sum(b.slnet), disamt = sum(b.disamt) from doc21bf a, doc21dt b where a.docno = b.docno and DOCDAT >= '2011-08-01' AND DOCDAT <= '2011-08-31' group by b.pluid )m8 on a.pluid = m8.pluid left join ( select qty = sum(b.qty), slamt = sum(b.slamt), slnet = sum(b.slnet), disamt = sum(b.disamt) from doc21bf a, doc21dt b where a.docno = b.docno and DOCDAT >= '2011-09-01' AND DOCDAT <= '2011-09-31' group by b.pluid )m9 on a.pluid = m9.pluid left join ( select qty = sum(b.qty), slamt = sum(b.slamt), slnet = sum(b.slnet), disamt = sum(b.disamt) from doc21bf a, doc21dt b where a.docno = b.docno and DOCDAT >= '2011-10-01' AND DOCDAT <= '2011-10-31' group by b.pluid )m10 on a.pluid = m10.pluid left join ( select qty = sum(b.qty), slamt = sum(b.slamt), slnet = sum(b.slnet), disamt = sum(b.disamt) from doc21bf a, doc21dt b where a.docno = b.docno and DOCDAT >= '2011-11-01' AND DOCDAT <= '2011-11-30' group by b.pluid )m11 on a.pluid = m11.pluid left join ( select qty = sum(b.qty), slamt = sum(b.slamt), slnet = sum(b.slnet), disamt = sum(b.disamt) from doc21bf a, doc21dt b where a.docno = b.docno and DOCDAT >= '2011-12-01' AND DOCDAT <= '2011-12-31' group by b.pluid )m12 on a.pluid = m12.pluid left join ( select qty = sum(begqty + inqty - outqty) from finstockshp where shpid = 2 group by pluid )b on b.pluid = a.pluid