IF EXISTS(SELECT 1 FROM sysobjects WHERE name = 'USP_MZ_SFMXBB_QUERY' AND xtype = 'P')
DROP PROC USP_MZ_SFMXBB_QUERY
go
create PROC [dbo].[USP_MZ_SFMXBB_QUERY]
@WSSDM varchar(20), --单位代码
@KSRQ varchar(20), --开始日期
@JSRQ varchar(20), --结束日期
@KSDM VARCHAR(30), --科室代码
@HZLB VARCHAR(10) --患者类别
AS
/**********
*************/
set nocount on
DECLARE @SQL1 NVARCHAR(4000)
SET @SQL1 = '
Declare @Sql varchar(4000),
@SFXM VARCHAR(30),
@sFieldSql nvarchar(4000),
@sZjeSql nvarchar(4000),
@sSumSql nvarchar(4000),
@SFXMMC varchar(30)
Select @Sql='',@sFieldSql='',@sZjeSql='',@sSumSql=''
Select c.brid as id,a.xm,b.sfxmflmc,Sum(ISNULL(b.je,0)) as zje Into #tmp_sfxx
From mz_sfzb a(nolock),mz_sfmx b(nolock),mz_ghxx c(nolock)
where a.wssdm=b.wssdm
and a.id=b.zb_id
and a.wssdm=c.WSSDM
and a.ghlsh=c.ID
and a.sfbz not in (0,9)
and a.wssdm ='''+@WSSDM+'''
and a.sfsj between '''+@KSRQ+''' and '''+@JSRQ+'''
'
IF @KSDM<> ''
SET @SQL1 = @SQL1 + ' and a.ksdm = '''+@KSDM+''''
IF @HZLB <> ''
SET @SQL1 = @SQL1 + ' and a.fbmc = '''+@HZLB+''''
SET @SQL1 = @SQL1 + ' group by a.xm,b.sfxmflmc,c.brid '
SET @SQL1 = @SQL1 + '
update #tmp_sfxx set sfxmflmc=isnull(sfxmflmc,''无分类'')
Create Table #tmpData_Rst
(
病人ID varchar(20),
患者姓名 varchar(200),
总金额 decimal(14,2) default(0)
)
Insert into #tmpData_Rst(病人ID,患者姓名,总金额)
Select distinct id,xm,SUM(zje) AS zje
from #tmp_sfxx group by xm,id
DECLARE tmpCursor CURSOR
FOR
Select distinct sfxmflmc from #tmp_sfxx
OPEN tmpCursor
FETCH NEXT FROM tmpCursor INTO @SFXM
WHILE @@FETCH_STATUS=0
BEGIN
Set @Sql = ''Alter table #tmpData_Rst Add [''+@SFXM+''] numeric(14,2)''
EXEC(@Sql)
Set @Sql = ''Update a Set [''+@SFXM+'']=Convert(numeric(14,2),ISNULL(b.zje,0)) ''
Set @Sql = @Sql+ ''From #tmpData_Rst a(nolock),#tmp_sfxx b(nolock) ''
Set @Sql = @Sql+ ''Where a.患者姓名=b.xm and a.病人ID=b.ID and b.sfxmflmc=''+''''''''+@SFXM+''''''''
SET @Sql = @Sql + '' update #tmpData_Rst set [''+@SFXM+''] = isnull([''+@SFXM+''],0)''
exec(@Sql)
FETCH NEXT FROM tmpCursor INTO @SFXM
END
--删除游标
DEALLOCATE tmpCursor
ALTER TABLE #tmpData_Rst DROP COLUMN 病人ID
SELECT * into TemTable_USP_MZ_SFMXBB_QUERY FROM #tmpData_Rst
--释放临时表
Drop Table #tmpData_Rst
Drop table #tmp_sfxx
'
exec (@SQL1)
--2019-01-09 增加合计
declare @sql3 varchar(4000)
set @sql3=''
select @sql3=@sql3+',sum(['+name+']) as ['+ name + ']' FROM syscolumns where id=object_id('TemTable_USP_MZ_SFMXBB_QUERY') and name not in('患者姓名','总金额') ORDER BY name
exec('SELECT * FROM TemTable_USP_MZ_SFMXBB_QUERY(nolock) union all select ''合计''as 患者姓名 ,sum(总金额) as 总金额'+@sql3+' from TemTable_USP_MZ_SFMXBB_QUERY(nolock) ')
drop table TemTable_USP_MZ_SFMXBB_QUERY
DECLARE @SQL2 NVARCHAR(4000)
SET @SQL2 = 'SELECT id into #tem_id FROM mz_sfzb WHERE wssdm = '''+@WSSDM+''' AND sfbz not in (0,9) AND sfsj BETWEEN '''+@KSRQ+''' and '''+@JSRQ+''''
IF @KSDM<> ''
SET @SQL2 = @SQL2 + ' and ksdm = '''+@KSDM+''''
IF @HZLB <> ''
SET @SQL2 = @SQL2 + ' and fbmc = '''+@HZLB+''''
SET @SQL2 = @SQL2 + '
SELECT itemcode AS 项目代码,itemname as 项目名称,itemgg as 规格,itemunit as 单位,sum(itemcount) as 数量,sum(je) as 金额 into #tep_mzmx
FROM mz_sfmx where zb_id in(select id from #tem_id) and wssdm = '''+@WSSDM+''' group by itemcode,itemname,itemgg,itemunit
delete from #tep_mzmx where 金额 = 0
select * from #tep_mzmx '
exec(@SQL2)
go