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
join tprice c on substring(b.barcode,1,5) = c.sb 这三个表的索引情况如何?
函数太多
语句太长索引基本无效
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