建议使用临时表和while循环来取代游标
解决方案 »
- 简单SQL语句,你知道它的运行结果吗?(欢迎讨论!)
- 请问如何写一个存储过程用于适应不同参数的来返回数据呢?
- 求一查询语句
- 邹建大哥,请帮忙,存储过程的植的接受过程中遇到难题了,请帮忙,谢谢
- 求助呀设备激活错误。物理文件名 'g;\SQL_DBs\myDB1_Dat.mdf' 可能有误。
- 问个问题,解答的给高分,做一条选择的SQL语句。
- 请问如何记录dbcc的执行结果,比如dbcc inputbuffer,等高手解答
- 求最大单号加工存储过程
- 急!update问题
- sql语句高手请进!(只要回答的有道理,分数必给)
- 問了好幾個人和一下午都解決不了的問題,大家幫幫
- 如何在C#编写的SQLSERVER 2005存储过程中返回SELECT查询结果?
create procedure datagsreport @wtype varchar,@year int
as
set nocount on select ac.* into #tb from (
SELECT c.[group] AS custgroup, c.customer,e.pfamily,d.pname, a.productno,year(a.fabdate) as okmyear,
case month(a.fabdate)
when 1 then 1
when 2 then 1
when 3 then 1
when 4 then 2
when 5 then 2
when 6 then 2
when 7 then 3
when 8 then 3
when 9 then 3
when 10 then 4
when 11 then 4
when 12 then 4
end
as okmqur, fac.factoryname ,
case when left(a.analysisresult,1)='4' then a.number
when left(a.analysisresult,1)='5' then b.number else 0 end as bnum,
case when left(a.analysisresult,1)='2' then a.number else 0 end as cnum,
case when left(a.analysisresult,1)='3' then a.number else 0 end as snum,
a.number as pcs ,a.implemdate ,a.dept,a.improvement ,a.u8dreport ,
case when left(a.analysisresult,1)='4' then rs.causedescen
when left(a.analysisresult,1)='5' then rs.causedescen else null end as uaescause
FROM dbo.complaintinfob a INNER JOIN
dbo.complaintinfoa b ON b.caID = a.caID AND a.type <> 'CP' AND b.status = 'E'
and a.typeno in (select typeno from reportswt where rid=25)
and year(a.fabdate) between @year-2 and @year INNER JOIN
dbo.customerinfo c ON a.customer = c.customer AND
c.customer NOT LIKE 'Retail%' INNER JOIN
dbo.productno d ON a.productno = d.productno
inner join productinfo e on b.pname=e.pname INNER JOIN failurecode f on a.analysisresult=f.failurecode
INNER JOIN ii_factory fac on e.ftid=fac.ftid inner join rootcause rs on a.rootcause=rs.id ) ac
select custgroup,customer,pfamily,pname,'' as productno ,okmyear,okmqur,factoryname,
case when isnull(sum(bnum),0) = 0 then null else
( select top 1 uaescause
from #tb bb
where isnull(ac.custgroup,' ')=isnull(bb.custgroup,' ')
and isnull(ac.customer,' ') =isnull(bb.customer,' ')
and isnull(ac.pfamily,' ') =isnull(bb.pfamily,' ')
and isnull(ac.pname,' ') =isnull(bb.pname,' ')
and ac.okmyear =bb.okmyear and ac.okmqur =bb.okmqur
and isnull(ac.factoryname,' ') =isnull(bb.factoryname,' ' )
group by uaescause ,improvement,implemdate,dept, u8dreport
order by count(uaescause) desc
) end as uaescause ,
case when isnull(sum(bnum),0) = 0 then null else
( select top 1 dept
from #tb bb
where isnull(ac.custgroup,' ')=isnull(bb.custgroup,' ')
and isnull(ac.customer,' ') =isnull(bb.customer,' ')
and isnull(ac.pfamily,' ') =isnull(bb.pfamily,' ')
and isnull(ac.pname,' ') =isnull(bb.pname,' ')
and ac.okmyear =bb.okmyear and ac.okmqur =bb.okmqur
and isnull(ac.factoryname,' ') =isnull(bb.factoryname,' ' )
group by uaescause ,improvement,implemdate,dept, u8dreport
order by count(uaescause) desc
) end as dept ,
case when isnull(sum(bnum),0) = 0 then null else
( select top 1 improvement
from #tb bb
where isnull(ac.custgroup,' ')=isnull(bb.custgroup,' ')
and isnull(ac.customer,' ') =isnull(bb.customer,' ')
and isnull(ac.pfamily,' ') =isnull(bb.pfamily,' ')
and isnull(ac.pname,' ') =isnull(bb.pname,' ')
and ac.okmyear =bb.okmyear and ac.okmqur =bb.okmqur
and isnull(ac.factoryname,' ') =isnull(bb.factoryname,' ' )
group by uaescause ,improvement,implemdate,dept, u8dreport
order by count(uaescause) desc
) end as improvement,
case when isnull(sum(bnum),0) = 0 then null else
( select top 1 implemdate
from #tb bb
where isnull(ac.custgroup,' ')=isnull(bb.custgroup,' ')
and isnull(ac.customer,' ') =isnull(bb.customer,' ')
and isnull(ac.pfamily,' ') =isnull(bb.pfamily,' ')
and isnull(ac.pname,' ') =isnull(bb.pname,' ')
and ac.okmyear =bb.okmyear and ac.okmqur =bb.okmqur
and isnull(ac.factoryname,' ') =isnull(bb.factoryname,' ' )
group by uaescause ,improvement,implemdate,dept, u8dreport
order by count(uaescause) desc
) end as implemdate ,
case when isnull(sum(bnum),0) = 0 then null else
( select top 1 u8dreport
from #tb bb
where isnull(ac.custgroup,' ')=isnull(bb.custgroup,' ')
and isnull(ac.customer,' ') =isnull(bb.customer,' ')
and isnull(ac.pfamily,' ') =isnull(bb.pfamily,' ')
and isnull(ac.pname,' ') =isnull(bb.pname,' ')
and ac.okmyear =bb.okmyear and ac.okmqur =bb.okmqur
and isnull(ac.factoryname,' ') =isnull(bb.factoryname,' ' )
group by uaescause ,improvement,implemdate,dept, u8dreport
order by count(uaescause) desc
) end as u8dreport,
isnull(sum(pcs),0) as pcs, isnull(sum(bnum),0) as bnum,
isnull(sum(cnum),0) as cnum,
isnull(sum(snum),0) as snum,
isnull(sum(qty),0) as qty
from ( select
custgroup,customer,pfamily,pname,okmyear,okmqur,productno,factoryname,sum(pcs) as pcs, sum(bnum) as bnum,
sum(cnum) as cnum,
sum(snum) as snum ,case okmqur when 1 then (select sum(quantity) from sales where
productno=ab.productno
and year(pdate)=ab.okmyear and month(pdate) between 1 and 3)
when 2 then (select sum(quantity) from sales where
productno=ab.productno
and year(pdate)=ab.okmyear and month(pdate) between 4 and 6)
when 3 then (select sum(quantity) from sales where
productno=ab.productno
and year(pdate)=ab.okmyear and month(pdate) between 7 and 9)
when 4 then (select sum(quantity) from sales where
productno=ab.productno
and year(pdate)=ab.okmyear and month(pdate) between 10 and 12)
end as qty from #tb ab group by custgroup,customer,pfamily,pname,productno,okmyear,okmqur,factoryname ) ac
group by custgroup,customer,pfamily,pname,okmyear,okmqur,factoryname
order by custgroup,customer,pfamily,pname,okmyear,okmqur,factoryname
drop table #tb