SQL高手帮忙优化一下

Jay2018509 2009-06-22 04:34:33
update LCGrpCont set Peoples2 = (
select count(distinct InsuredNo)
from LCPol
where GrpContNo = '00025046000001'
and PolTypeFlag = '0'
and AppFlag = '1') , OnWorkPeoples = (
select count(*)
from LCInsured a, LCCont b
where a.ContNo = b.ContNo
and a.GrpContNo = '00025046000001'
and a.InsuredStat = '1'
and b.AppFlag = '1') , OffWorkPeoples = (
select count(*)
from LCInsured a, LCCont b
where a.ContNo = b.ContNo
and a.GrpContNo = '00025046000001'
and a.InsuredStat = '2'
and b.AppFlag = '1') , OtherPeoples = (
select count(distinct InsuredNo)
from LCPol
where GrpContNo = '00025046000001'
and PolTypeFlag = '0'
and AppFlag = '1') -(
select count(*)
from LCInsured a, LCCont b
where a.ContNo = b.ContNo
and a.GrpContNo = '00025046000001'
and a.InsuredStat = '1'
and b.AppFlag = '1') -(
select count(*)
from LCInsured a, LCCont b
where a.ContNo = b.ContNo
and a.GrpContNo = '00025046000001'
and a.InsuredStat = '2'
and b.AppFlag = '1')
where GrpContNo = '00025046000001'
...全文
23 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
ai_li7758521 2009-06-22
  • 打赏
  • 举报
回复
eclare @P1 int,@P2 int,@p3 int

select @P1=count(distinct InsuredNo)
from LCPol
where GrpContNo = '00025046000001'
and PolTypeFlag = '0'
and AppFlag = '1'

select @P2=sum(case a.InsuredStat when 1 then 1 else 0 end),
@P3=sum(case a.InsuredStat when 2 then 1 else 0 end)
from LCInsured a, LCCont b
where a.ContNo = b.ContNo
and a.GrpContNo = '00025046000001'
and a.InsuredStat = '1'
and b.AppFlag = '1'


update LCGrpCont
set Peoples2 = @P1
,OnWorkPeoples =@p2
,OffWorkPeoples =@p3
,OtherPeoples = @P1-@p2-@p3
中国风 2009-06-22
  • 打赏
  • 举报
回复
update LCGC
set Peoples2=LCP.InsuredNo,OnWorkPeoples=LCI.OnWorkPeoples,OffWorkPeoples=LCI.OffWorkPeoples,
OtherPeoples=LCP.InsuredNo-LCI.OtherPeoples
from LCGrpCont LCGC
,(select count(distinct InsuredNo )InsuredNo from LCPol where PolTypeFlag = '0' and AppFlag = '1' and GrpContNo = '00025046000001' ) LCP,
( select sum(case when a.InsuredStat = '1' then 1 else 0 end) as OnWorkPeoples ,sum(case when a.InsuredStat = '2' then 1 else 0 end) as OffWorkPeoples,count(*) as OtherPeoples from LCInsured a, LCCont b where a.ContNo = b.ContNo and a.GrpContNo = '00025046000001' and b.AppFlag = '1') LCI
where LCGC.GrpContNo = '00025046000001'
-晴天 2009-06-22
  • 打赏
  • 举报
回复
update LCGrpCont set 

Peoples2 = (
select count(distinct InsuredNo)
from LCPol
where GrpContNo = '00025046000001'
and PolTypeFlag = '0'
and AppFlag = '1') ,

OnWorkPeoples = (
select count(*)
from LCInsured a, LCCont b
where a.ContNo = b.ContNo
and a.GrpContNo = '00025046000001'
and a.InsuredStat = '1'
and b.AppFlag = '1') ,

OffWorkPeoples = (
select count(*)
from LCInsured a, LCCont b
where a.ContNo = b.ContNo
and a.GrpContNo = '00025046000001'
and a.InsuredStat = '2'
and b.AppFlag = '1')

where GrpContNo = '00025046000001'
go

update LCGrpCont set OtherPeoples=
Peoples2-OnWorkPeoples-OffWorkPeoples
where GrpContNo = '00025046000001'
go
JonasFeng 2009-06-22
  • 打赏
  • 举报
回复
建议楼主
把这两个表 LCInsured,LCCont写一个视图。

还有能用JOIN 的就不用子查询
ks_reny 2009-06-22
  • 打赏
  • 举报
回复
子查詢太多了,用連接吧.
--小F-- 2009-06-22
  • 打赏
  • 举报
回复
把conut(*)换成count(1)

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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