日期:2014-05-18  浏览次数:20510 次

一段SQL,不执行...另求MSSQL入门教程
SQL code

select
        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 / endmoney

from
        (
                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