当前位置: 代码迷 >> Sql Server >> 请高手帮忙﹐优化一SQL语句。若有需要﹐改写成存储过程也行﹐多谢了﹐
  详细解决方案

请高手帮忙﹐优化一SQL语句。若有需要﹐改写成存储过程也行﹐多谢了﹐

热度:209   发布时间:2016-04-27 21:51:26.0
请高手帮忙﹐优化一SQL语句。若有需要﹐改写成存储过程也行﹐谢谢了﹐在线等
下面的SQL语句﹐来自项目开发中﹐请高手指点优化一下。非常感谢﹗
其實就是一SQL技巧﹐怎么更快的取得非Group   By   語句中的字段值﹖
Select   top   1   SupCode,TBNo,TBDate,MakeNo,PartCode,WHCode,ReceiveDate,Currency,Up,
Sum(Num_)as   TNum_,   Sum(SpareNum_)as   TSpareNum_,Sum(ReceiveNum_)as   TReceiveNum_,
Sum(ReceiveSpare_)as   TReceiveSpare_,Sum(QcPassNum)   as   TQcPassNum_,  
Sum(QcPassSpare)as   TQcPassSpare_,Sum(ReturnNum)as   TReturnNum_
From   PurB
Where   Final_=1   and   Finish=0
Group   By   SupCode,TBNo,TBDate,MakeNo,PartCode,WHCode,ReceiveDate,Currency,Up

------解决方案--------------------
--try

select * from PurB A
left join
(
select SupCode, TBNo, PartCode,
Sum(Num_)as TNum_, Sum(SpareNum_)as TSpareNum_,Sum(ReceiveNum_)as TReceiveNum_,
Sum(ReceiveSpare_)as TReceiveSpare_,Sum(QcPassNum) as TQcPassNum_,
Sum(QcPassSpare)as TQcPassSpare_,Sum(ReturnNum)as TReturnNum_
from PurB
Where Final_=1 and Finish=0
Group By SupCode, TBNo, PartCode
)B on A.SupCode=B.SupCode and A.TBNo=B.TBNo and A.PartCode=B.PartCode
where A.Final_=1 and A.Finish=0

------解决方案--------------------
好像没有优化的必要
------解决方案--------------------
对SupCode,TBNo,TBDate,MakeNo,PartCode,WHCode,ReceiveDate,Currency,Up做一个聚集试试吧
  相关解决方案