select xslx,kh,sum(quantity),sum(je) from tsales a join tsalesdetail b on a.lsh = b.lsh
                        join tprice c on substring(b.barcode,1,5) = c.sb where xslx = '普通销售' and datediff(day,rq,getdate()) = 0 group by xslx,kh
                        union all select '合计销售','',sum(quantity),sum(je) from tsales a join tsalesdetail b on a.lsh = b.lsh
                        where xslx = '普通销售' and datediff(day,rq,getdate()) = 0 union all select xslx,kh,sum(quantity),sum(je) from tsales a join tsalesdetail b on a.lsh = b.lsh
                        join tprice c on substring(b.barcode,1,5) = c.sb where xslx = '退货' and datediff(day,rq,getdate()) = 0 group by xslx,kh
                        union all select '合计退货','',sum(quantity),sum(je) from tsales a join tsalesdetail b on a.lsh = b.lsh
                        where xslx = '退货' and datediff(day,rq,getdate()) = 0 union all select xslx,kh,sum(quantity),sum(je)
                        from tsales a join tsalesdetail b on a.lsh = b.lsh join tprice c on substring(b.barcode,1,5) = c.sb
                        where xslx = '换货' and datediff(day,rq,getdate()) = 0 and quantity >0 group by xslx,kh union all select '合计换货','',sum(quantity),sum(je)
                        from tsales a join tsalesdetail b on a.lsh = b.lsh where xslx = '换货' and datediff(day,rq,getdate()) = 0 and quantity >0
                        union all select xslx,kh,sum(quantity),sum(je) from tsales a join tsalesdetail b on a.lsh = b.lsh
                        join tprice c on substring(b.barcode,1,5) = c.sb where xslx = '赠送' and datediff(day,rq,getdate()) = 0 
                        group by xslx,kh union all select '合计赠送','',sum(quantity) ,sum(je)
                        from tsales a join tsalesdetail b on a.lsh = b.lsh where xslx = '赠送' and datediff(day,rq,getdate()) = 0 
                        union all select xslx,kh,sum(quantity),sum(je) from tsales a join tsalesdetail b on a.lsh = b.lsh
                        join tprice c on substring(b.barcode,1,5) = c.sb where xslx = '赠送退货'  and datediff(day,rq,getdate()) = 0 
                        group by xslx,kh union all select '合计赠送退货','',sum(quantity) ,sum(je)
                        from tsales a join tsalesdetail b on a.lsh = b.lsh where xslx = '赠送退货' and datediff(day,rq,getdate()) = 0 
                        union all select xslx,kh,sum(quantity),sum(je) from tsales a join tsalesdetail b on a.lsh = b.lsh
                        join tprice c on substring(b.barcode,1,5) = c.sb where xslx = '欠款销售'  and datediff(day,rq,getdate()) = 0 
                        group by xslx,kh union all select '合计欠款销售','',sum(quantity) ,sum(je)
                        from tsales a join tsalesdetail b on a.lsh = b.lsh where xslx = '欠款销售' and datediff(day,rq,getdate()) = 0 
                        union all select xslx,kh,sum(quantity),sum(je) from tsales a join tsalesdetail b on a.lsh = b.lsh join tprice c on substring(b.barcode,1,5) = c.sb
                        where xslx = '欠款换货' and datediff(day,rq,getdate()) = 0 and quantity >0 group by xslx,kh union all select '合计欠款换货','',sum(quantity),sum(je)
                        from tsales a join tsalesdetail b on a.lsh = b.lsh where xslx = '欠款换货' and datediff(day,rq,getdate()) = 0 and quantity >0
                        union all select xslx,kh,sum(quantity),sum(je) from tsales a join tsalesdetail b on a.lsh = b.lsh
                        join tprice c on substring(b.barcode,1,5) = c.sb where xslx = '欠款退货' and datediff(day,rq,getdate()) = 0 
                        group by xslx,kh union all select '合计欠款退货','',sum(quantity) ,sum(je)
                        from tsales a join tsalesdetail b on a.lsh = b.lsh where xslx = '欠款退货' and datediff(day,rq,getdate()) = 0 

解决方案 »

  1.   

    这个很难优化了,索引基本上用不到,还这么多的union all
      

  2.   

    tsales a join tsalesdetail b on a.lsh = b.lsh
    join tprice c on substring(b.barcode,1,5) = c.sb 这三个表的索引情况如何?
      

  3.   

    union all太多 
    函数太多 
    语句太长索引基本无效 
      

  4.   

    在GROUP BY 条件上加聚集索引吧,返回的应该是大量数据集吧
      

  5.   

    如下即可
    select xslx,kh,sum(quantity),sum(je), xslx as k1,0 as k2
    from tsales a join tsalesdetail b on a.lsh = b.lsh
    join tprice c on substring(b.barcode,1,5) = c.sb 
    where datediff(day,rq,getdate()) = 0 
    group by xslx,kh
    union all
    select '合计销售','',sum(quantity),sum(je),xslx as k1,1 as k2
    from tsales a join tsalesdetail b on a.lsh = b.lsh
    where datediff(day,rq,getdate()) = 0
    group by xslx
    order by k1,k2