日期:2014-05-18  浏览次数:20446 次

哪位帮忙看看
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go



ALTER PROCEDURE [dbo].[daysales] 
@depname varchar(80) ='温州恒盛' ,
@fdate datetime='2011-09-01 00:00:00.000'
as
select fsupplyid as 客户名称,convert(varchar(80),fnumber) as 品号色号,convert(varchar(80),sum(fqty)) as 数量,
convert(varchar(80),avg(fconsignprice)) as 单价,convert(varchar(80),sum(fconsignamount)) as 金额
 from vwICBill_8  
where 
fdate=@fdate and fcheckflag='※' 
and 
finterid in (
select finterid from ICStockBill where fdeptid in 
(select fitemid from t_department where fname=@depname))
group by fsupplyid ,fnumber
union all 
select '合计','',convert(varchar(80),sum(fqty)) ,
'',convert(varchar(80),sum(fconsignamount)) as 金额
 from vwICBill_8  
where 
fdate=@fdate and fcheckflag='※' 
and 
finterid in (
select finterid from ICStockBill where fdeptid in 
(select fitemid from t_department where fname=@depname))
union all select '客户名称','银行存款','现金','备注','累计回款'
union all
select convert(varchar(80),tb.fbankacctname),convert(varchar(80),tb.yinhang),convert(varchar(80),tb.xianjin),convert(varchar(80),tb.fexplanation),

(case when tb.yinhang=(select top 1 tb.yinhang from 
(
select fbankacctname,
(case when faccountid='1001' then famountfor else 0 end) as yinhang ,
(case when faccountid='1000' then famountfor else 0 end) as xianjin,
FExplanation
from t_RP_NewReceiveBill 
where fdate=@fdate 
and fstatus<>'0'
and fdepartment in 
(select fitemid from t_department where fname=@depname)) tb where tb.fbankacctname=tbsum.fbankacctname) then convert(varchar(80),tbsum.cou) else '' end

)

 from
(
select fbankacctname,
(case when faccountid='1001' then famountfor else 0 end) as yinhang ,
(case when faccountid='1000' then famountfor else 0 end) as xianjin,
FExplanation
from t_RP_NewReceiveBill 
where fdate=@fdate 
and fstatus<>'0'
and fdepartment in 
(select fitemid from t_department where fname=@depname)) tb
left join 
(select fbankacctname,sum(famountfor) as cou from t_RP_NewReceiveBill
where CONVERT(varchar(12) , @fdate, 112 ) >= convert(varchar(80),year(@fdate )-1)+convert(varchar(80),12)+convert(varchar(80),26)
and CONVERT(varchar(12) , @fdate, 112 ) <= convert(varchar(80),year(@fdate ))+convert(varchar(80),12)+convert(varchar(80),25)
and fstatus<>'0'
group by fbankacctname) tbsum
on tb.fbankacctname=tbsum.fbankacctname

union all

select '合计',convert(varchar(80),sum(tmp1.yinhang)),convert(varchar(80),sum(tmp1.xianjin)),'',convert(varchar(80),sum(tmp1.heji))
from
(
select tb.fbankacctname,tb.yinhang,tb.xianjin,tb.fexplanation
,
(case when tb.yinhang=(select top 1 tb.yinhang from 
(
select fbankacctname,
(case when faccountid='1001' then famountfor else 0 end) as yinhang ,
(case when faccountid='1000' then famountfor else 0 end) as xianjin,
FExplanation
from t_RP_NewReceiveBill 
where fdate=@fdate 
and fstatus<>'0'
and fdepartment in 
(select fitemid from t_department where fname=@depname)) tb
 where tb.fbankacctname=tbsum.fbankacctname) then 
tbsum.cou else '' end ) as heji

from
(
select fbankacctname,
(case when faccountid='1001' then famountfor else 0 end) as yinhang ,
(case when faccountid='1000' then famountfor else 0 end) as xianjin,
FExplanation
from t_RP_NewReceiveBill 
where fdate=@fdate 
and fstatus<>'0'
and fdepartment in 
(select fitemid from t_department whe