SQL问题啦!(10分)

  • 主题发起人 主题发起人 maomao888
  • 开始时间 开始时间
M

maomao888

Unregistered / Unconfirmed
GUEST, unregistred user!
我有一个SQL语句:
select 1,2,(select from ..where ),(select from ..where ),(select from ..where ),
我后面的三个select是一模一样的,但就是where中的时间段取的不一样,
我的目的就是让三个时间段的select(sum())的全都显示出来
现在的问题是我的每一个:(select from ..where )都有特别的长,
可否有一个好的办法,能简单一点,因为他们仅仅是时间段取的不同。
是三个并列的字段,不用union all
见过那种:
select 1,2,(select....),(select...),(select..)
from
where
我的意思是三个括号里的是三个不同的时间段。
 
你的表述不很清楚,干脆把select 1,2,(select from ..where ),
(select from ..where ),(select from ..where ),写完整再说吧!
 
select f.cpdlmc as c1,f.ccpnjh as j1,f.ccpnjh/12 as j2,f.ccpnjh/(12*4.5) as j3,
(select sum(a.hlbj*b.sssl*c.zgjhj)
from db2admin.t_rkd as a inner join db2admin.t_rkdmx as b on a.rkdid=b.rkdid
inner join db2admin.t_gxjgcs as c on b.wlbh=c.wlbh
where a.rkrq>='2001-9-1' and a.rkrq<='2001-11-1' and a.rkly='生产入库'
and ( b.wlbh like '4502%' or b.wlbh like 'X4410%')
group by a.rkly ) as cpcz1,
(select sum(a.hlbj*b.sssl*c.zgjhj) from db2admin.t_rkd as a inner join db2admin.t_rkdmx as b on a.rkdid=b.rkdid
inner join db2admin.t_gxjgcs as c on b.wlbh=c.wlbh
where a.rkrq>='2001-6-1' and a.rkrq<='2001-11-1' and a.rkly='生产入库'
and ( b.wlbh like 'X4502%' or b.wlbh like 'X4410%')
group by a.rkly) as cpcz2,
(select sum(a.hlbj*b.sssl*c.zgjhj)
from db2admin.t_rkd as a inner join db2admin.t_rkdmx as b on a.rkdid=b.rkdid
inner join db2admin.t_gxjgcs as c on b.wlbh=c.wlbh
where a.rkrq>='2001-3-1' and a.rkrq<='2001-11-1' and a.rkly='生产入库' and ( b.wlbh like 'X4502%' or b.wlbh like 'X4410%')
group by a.rkly ) as cpcz3
from db2admin.t_acpsczj as f
where f.cpdlmc='铝塑线'
group by f.cpdlmc,f.ccpnjh
 
先建立一个query视图 vwsum
select sum(a.hlbj*b.sssl*c.zgjhj)
from db2admin.t_rkd as a inner join db2admin.t_rkdmx as b on a.rkdid=b.rkdid
inner join db2admin.t_gxjgcs as c on b.wlbh=c.wlbh
where a.rkly='生产入库'
and ( b.wlbh like '4502%' or b.wlbh like 'X4410%'
然后再
select f.cpdlmc as c1,f.ccpnjh as j1,f.ccpnjh/12 as j2,f.ccpnjh/(12*4.5) as j3,
(select * from vwsum where a.rkrq>='2001-3-1' and a.rkrq<='2001-11-1' group by a.rkly) as cpcz1
(select * from vwsum where a.rkrq>='2001-6-1' and a.rkrq<='2001-11-1' group by a.rkly) as cpcz2
(select * from vwsum where a.rkrq>='2001-9-1' and a.rkrq<='2001-11-1' group by a.rkly) as cpcz3
 
我有一种SQL写法,可以解决你这个问题,
但写起来很复杂,如果SQL写成之后,会明显比你这个还长,
但,那是经过优化后的,能让服务器很有效地使执行。
不会象你这条语句,能使服务器溢出。
只是,你不能很好地理解SQL语句的原理,所以,你现在还学不会那种写法。
 
楼上那位,是否可以把你说的举个例子呢?
 
select * from table where .. or .. or ..or .................................
[red]对不起,没有看清楚题[/red]
 
比如,使用交叉语句,见下面的例子
TRANSFORM Sum(ss.qty) AS qtyofsum
SELECT ss.itemdcode,ss.itemname
FROM (SELECT t_optype.*, t_salelist.*
FROM t_optype LEFT JOIN t_salelist ON t_optype.optype = t_salelist.optype;
) as ss
GROUP BY ss.itemdcode, ss.itemname
PIVOT t_optype.optype;
这种写法,可以使t_optype.optype这个操作方式字段产生交叉形式。
但这个方法并不太好用,我不提畅用这个的。
 
我一般都用下面这类语句来完成同上面相似的工作,但下面这条语句所完成的工作要复杂点。
就是有相同的结果的语句,下面这种写法也明显要长的多,而且在执行效率上,也在差一些。
可是,这种写法很容易掌握,和控制。是一种很好的选择。
而这种写法,也可能解决上面所提到的问题,只是其中的关键,一定要理解为什么用这种写法,
而不是使用传统的,如问题中所提到哪种方式?
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

我也不好讲为何要用这种方式,不知从何说起,但它的确能很好地提高服务器的执行效率。
反正就是有效地限制与原始数据的读取,就能实现这一目的。
具体如何作呢?就得理解每种语句的原理,并灵活地使用它们,
让它们相互配合,并且使你对原始的数据尽量只对需要的读取一次,
并再对它们有效地再处理,而得到你所要的结果。
呵呵,我自己也很糊涂呢,可别把你们也给说糊涂了。
枫兄,我这样说,你可满意?
 
对了,我上面的语句,只是个例子,对SQL语句巧妙地运用
还不远不止这些。
如果你能习惯使用交叉语句,哪或许更好。
不过,我相信,它是很难以使用的,我已很少用到它了。
 
多人接受答案了。
 
后退
顶部