Pro_[计数据分析] 存储过程

USE [420900_2012]
GO
/****** Object:  StoredProcedure [dbo].[计数据分析]    Script Date: 08/28/2012 09:31:55 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


ALTER PROCEDURE [dbo].[计数据分析]
@CheckTimeStar varchar(12),--时间段
@CheckTimeEnd varchar(12)--时间段
AS
declare @SqlStr varchar(5000)
declare @SqlZFY varchar(1000)--总费用
declare @SqlZFYHj varchar(1000)--总费用合计
declare @SqlZFYFHj varchar(1000)--非基药费用合计
declare @SqlXm varchar(1000)--项目费用合计
BEGIN


set @SqlZFY='select a.orgcode,  sum(a.fee)合计 from o_CbZyBxDjzb a,o_CbZyBxDj b,o_CbZyZdb c
where  WriteDate >='''+@CheckTimeStar+''' and WriteDate <='''+@CheckTimeEnd+''' and c.billcode=b.billcode and c.billno=b.billno
 and a.orgcode=b.orgcode and a.coopmedcode=b.coopmedcode and a.idno=b.idno and a.diagno=b.diagno'
 
set @SqlZFYHj=@SqlZFY+ ' group by  a.orgcode'
 
set @SqlZFYFHj=@SqlZFY+' FeeTypeCode in (''000204'',''000102'',''000202'')group by  a.orgcode'

set @SqlXm=@SqlZFY+' FeeTypeCode  not in (''000204'',''000102'',''000202'') and WipeOut =''2'') group by  a.orgcode'

set @SqlStr= 'select a.* ,b.非基药比例,c.项目比例 诊疗项目费 from
(select a.orgcode ,a.HospitalName, sum(totalfee)/count(1)例均住院费用,sum(DayCount )/count(1)平均住院天数,(sum(totalfee)/count(1))/(sum(DayCount )/count(1))平均床日费用
from dbo.o_CbZyBxDj a ,  o_CbZyZdb b
where a.orgcode in (select orgcode from p_ylfwdw where OrgType =2)
and  WriteDate >='''+@CheckTimeStar+''' and WriteDate <='''+@CheckTimeStar+'''
and a.billcode=b.billcode and a.billno=b.billno
group by a.orgcode,a.HospitalName)a ,
(select a.orgcode,b.合计/a.合计 非基药比例 from
('+@SqlZFYFHj+')a,('+@SqlZFYFHj+')b where a.orgcode=b.orgcode
)b,(select a.orgcode,b.合计/a.合计 项目比例 from
('+@SqlZFYFHj+')a,('+@SqlXm+')b
 where a.orgcode=b.orgcode)c
 where a.orgcode=b.orgcode and a.orgcode=c.orgcode'
print @SqlStr
exec(@SqlStr)
END

 

posted @ 2012-10-23 17:22  虫虫飞520  阅读(209)  评论(0编辑  收藏  举报