这条语句还能优化吗?

jackluo1981 2005-09-12 10:00:53
declare @S1 datetime
declare @S2 datetime
select @S1='2005-08-01'
select @S2='2005-08-31'
select a.modelNo as ModelNo,count(1) as num,
avg(datediff(mi,RecDate,GmtDate)) as InStoreTime, --在库时间
avg(datediff(mi,GmtDate,a.checkdate)) as WaitProduce, --生产滞留时间
avg(datediff(mi,a.checkdate,b.checkdate)) as WaitTime, --在制造滞留时间
avg(datediff(mi,b.checkdate,c.checkdate)) as ProductTime, --制造时间
avg(datediff(mi,c.checkdate,d.checkdate)) as OQCTime, --OQC时间
avg(datediff(mi,d.checkdate,e.checkdate)) as PackTime, --包装时间
avg(datediff(mi,b.checkdate,e.checkdate)) as SumProductTime, --生产时间
avg(datediff(mi,a.checkdate,e.checkdate)) as TotleTime --总时间
from ( select Producth.modelNo,Productd1.Productid,Productd1.checkdate from Productd1,producth where Producth.Productid=Productd1.Productid and
Productd1.ProductProject='1000' and PackDate>=@S1 and PackDate<=@S2) a,
(select Productd1.Productid,Productd1.checkdate from Productd1,producth where Producth.Productid=Productd1.Productid and
Productd1.ProductProject='1010' and PackDate>=@S1 and PackDate<=@S2) b,
(select Productd1.Productid,Productd1.checkdate from Productd1,producth where Producth.Productid=Productd1.Productid and
(Productd1.ProductProject='1700' or Productd1.ProductProject='1450' or Productd1.ProductProject='1521')
and PackDate>=@S1 and PackDate<=@S2) c,
(select Productd1.Productid,Productd1.checkdate from Productd1,producth where Producth.Productid=Productd1.Productid and
Productd1.ProductProject='1500' and PackDate>=@S1 and PackDate<=@S2) d,
(select Productd1.Productid,Productd1.checkdate from Productd1,producth where Producth.Productid=Productd1.Productid and
Productd1.ProductProject='1501' and PackDate>=@S1 and PackDate<=@S2) e,
(select Producth.Productid ,RecDate from Producth,Productd1 where Producth.Productid=Productd1.Productid and PackDate>=@S1 and PackDate<=@S2 ) f,
(select Producth.Productid ,GmtDate from Producth,Productd1 where Producth.Productid=Productd1.Productid and PackDate>=@S1 and PackDate<=@S2 ) g
where a.Productid = b.Productid and c.Productid = b.Productid and d.Productid = b.Productid
and e.Productid = b.Productid and f.Productid = b.Productid and g.Productid = b.Productid group by a.modelNo order by a.ModelNo
运行时间太长,请问各位如何优化!
...全文
124 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
lula 2005-09-12
  • 打赏
  • 举报
回复
关注
vbasten 2005-09-12
  • 打赏
  • 举报
回复
关注
gimy007 2005-09-12
  • 打赏
  • 举报
回复
一些镶套的SQL可以用临时表,这可以提高效率
longwulaoda 2005-09-12
  • 打赏
  • 举报
回复
臨時表
iwl 2005-09-12
  • 打赏
  • 举报
回复
declare @S1 datetime
declare @S2 datetime
select @S1='2005-08-01'
select @S2='2005-08-31'
select Producth.modelNo as ModelNo,count(1) as num,
avg(datediff(mi,RecDate,GmtDate)) as InStoreTime, --在库时间
avg(datediff(mi,GmtDate,a.checkdate)) as WaitProduce, --生产滞留时间
avg(datediff(mi,a.checkdate,b.checkdate)) as WaitTime, --在制造滞留时间
avg(datediff(mi,b.checkdate,c.checkdate)) as ProductTime, --制造时间
avg(datediff(mi,c.checkdate,d.checkdate)) as OQCTime, --OQC时间
avg(datediff(mi,d.checkdate,e.checkdate)) as PackTime, --包装时间
avg(datediff(mi,b.checkdate,e.checkdate)) as SumProductTime, --生产时间
avg(datediff(mi,a.checkdate,e.checkdate)) as TotleTime --总时间
from Productd1,producth
where Productd1.ProductProject IN ('1000','1010','1700','1450','1521','1500','1501') and Producth.Productid=Productd1.Productid and
PackDate>=@S1 and PackDate<=@S2
group by Producth.modelNo order by Producth.modelNo
zkw 2005-09-12
  • 打赏
  • 举报
回复
帮你顶

34,591

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧