我一般都用下面这类语句来完成同上面相似的工作,但下面这条语句所完成的工作要复杂点。
就是有相同的结果的语句,下面这种写法也明显要长的多,而且在执行效率上,也在差一些。
可是,这种写法很容易掌握,和控制。是一种很好的选择。
而这种写法,也可能解决上面所提到的问题,只是其中的关键,一定要理解为什么用这种写法,
而不是使用传统的,如问题中所提到哪种方式?
select itemdcode, itemname, qtyunit, rprice, SUM(QC1)-SUM(QC2) as QC, SUM(JH1) as JH, round(SUM(JJ1)/SUM(JH1),4) AS JJ,SUM(XS1) as XS,SUM(KC1)-SUM(KC2) AS KC
from (
select a.itemdcode, a.itemname, a.qtyunit, b.rprice, SUM(a.qty) as QC1, 0 as QC2, 0 as JH1, 0 as JJ1, 0 as XS1, 0 as KC1, 0 as KC2
from t_salelist a INNER JOIN t_itembusiness AS b ON a.itemdcode=b.itemdcode
where a.whflag>='2' and a.optype ='购入' and a.configdate<=#2001-01-01#
Group by a.itemdcode, a.itemname, a.qtyunit, b.rprice
union
select a.itemdcode, a.itemname, a.qtyunit, b.rprice, 0 as QC1, SUM(a.qty) as QC2, 0 as JH1, 0 as JJ1, 0 as XS1, 0 as KC1, 0 as KC2
from t_possale a INNER JOIN t_itembusiness AS b ON a.itemdcode=b.itemdcode
where a.optype ='零售' and a.tdate<=#2001-01-01#
Group by a.itemdcode, a.itemname, a.qtyunit, b.rprice
union
select a.itemdcode, a.itemname, a.qtyunit, b.rprice, 0 as QC1, 0 as QC2, SUM(a.qty) as JH1, sum(a.qty*a.tprice) as JJ1, 0 as XS1, 0 as KC1, 0 as KC2
from t_salelist a INNER JOIN t_itembusiness AS b ON a.itemdcode=b.itemdcode
where whflag>='2' and optype ='购入' and configdate>=#2001-01-01# and configdate<=#2001-02-01#
Group by a.itemdcode, a.itemname, a.qtyunit, b.rprice
union
select a.itemdcode, a.itemname, a.qtyunit, b.rprice, 0 as QC1, 0 as QC2, 0 as JH1, 0 as JJ1, SUM(a.qty) as XS1, 0 as KC1, 0 as KC2
from t_possale a INNER JOIN t_itembusiness AS b ON a.itemdcode=b.itemdcode
where a.optype ='零售' and a.tdate>=#2001-01-01# and a.tdate<=#2001-02-01#
Group by a.itemdcode, a.itemname, a.qtyunit, b.rprice
union
select a.itemdcode, a.itemname, a.qtyunit, b.rprice, 0 as QC1, 0 as QC2, 0 as JH1, 0 as JJ1, 0 as XS1, SUM(a.qty) as KC1, 0 as KC2
from t_salelist a INNER JOIN t_itembusiness AS b ON a.itemdcode=b.itemdcode
where a.whflag>='2' and a.optype ='购入' and a.configdate<=#2001-02-01#
Group by a.itemdcode, a.itemname, a.qtyunit, b.rprice
union
select a.itemdcode, a.itemname, a.qtyunit, b.rprice, 0 as QC1, 0 as QC2, 0 as JH1, 0 as JJ1, 0 as XS1, 0 as KC1, SUM(a.qty) as KC2
from t_possale a INNER JOIN t_itembusiness AS b ON a.itemdcode=b.itemdcode
where a.optype ='零售' and a.tdate<=#2001-02-01#
Group by a.itemdcode, a.itemname, a.qtyunit, b.rprice
)
Group by itemdcode, itemname, qtyunit, rprice
我也不好讲为何要用这种方式,不知从何说起,但它的确能很好地提高服务器的执行效率。
反正就是有效地限制与原始数据的读取,就能实现这一目的。
具体如何作呢?就得理解每种语句的原理,并灵活地使用它们,
让它们相互配合,并且使你对原始的数据尽量只对需要的读取一次,
并再对它们有效地再处理,而得到你所要的结果。
呵呵,我自己也很糊涂呢,可别把你们也给说糊涂了。
枫兄,我这样说,你可满意?