1/***************************************************************************************************
2 *
3 * 作 者 :
4 * 日 期 :2008-03-18
5 * 描 述 :核单台帐表
6 *
7 ***************************************************************************************************/
8
9alter proc Finance_P_AreceivableClaiGroupBy
10@begindate datetime, --开始日期
11@enddate datetime, --截止日期
12@pavilionid varchar(8000), --楼盘代码
13@UnitCode varchar(50),--公司代码
14@ExesTypeName varchar(50),--收费标准
15@ExesGenreName varchar(50)--费用类型
16as
17begin
18 --创建临时表
19 create table #Temp_AreceivableClaiGroupBy
20 (
21 colId int,
22 CellNum varchar(200), --铺位编号(合同编号)
23 ExesTypeName varchar(100),--收费类别
24 AccountMoney decimal(10,2)--应收金额
25 )
26
27 if(@pavilionid<>'')
28 begin
29 /******************************情况一******************************************/
30 declare @where varchar(8000)
31 declare @sql varchar(8000)
32 set @where='and V_Lease_HouseCell.PavilionID in('+@pavilionid+') and V_Lease_HouseCell.BargainID=null'
33
34 set @sql='select distinct
35 Finance_Areceivable.AreceivableID,--主键
36 V_Lease_HouseCell.CellNum, --单元号(合同编号)
37 Finance_ChargeItemSet.name As ExesTypeName,--费用类型
38 Finance_Areceivable.AccountMoney--应收款
39 From Finance_Areceivable
40 join system_framework on system_framework.UnitID=Finance_Areceivable.CompanyID
41 join V_Lease_HouseCell on V_Lease_HouseCell.CellID=Finance_Areceivable.CellID or V_Lease_HouseCell.BargainID=Finance_Areceivable.BargainID
42 join House_Client on House_Client.OwnerID=Finance_Areceivable.OwnerID
43 join Lease_Bargain on Finance_Areceivable.BargainID=Lease_Bargain.BargainID
44 left outer join Finance_ChargeItemSet on Finance_Areceivable.ItemID=Finance_ChargeItemSet.ItemID
45 left outer join Sys_ExesGenre on Sys_ExesGenre.ExesGenreID=Finance_ChargeItemSet.ExesGenreID
46 left outer join Sys_ExesType on Sys_ExesType.ExecTypeID=Finance_Areceivable.ItemType
47
48 where (system_framework.UnitCode like '''+@UnitCode+'''+''%'') and Finance_Areceivable.AccountDate between '''
49 +convert(varchar(10),@begindate,120)+''' and '''+convert(varchar(10),@enddate,120)+''' '+@where+''
50 if(@ExesTypeName<>'')
51 set @sql=@sql+' and Finance_ChargeItemSet.name='''+@ExesTypeName+''''
52 if(@ExesGenreName<>'')
53 set @sql=@sql+' and Sys_ExesGenre.ExesGenreName='''+@ExesGenreName+''''
54 --print @sql
55
56 /*********************************情况二***************************************/
57 declare @leasesql varchar(8000)
58 declare @leasewhere varchar(8000)
59 set @leasewhere='and V_Lease_HouseCell.PavilionID in('+@pavilionid+') and (V_Lease_HouseCell.BargainID<>null or V_Lease_HouseCell.BargainID<>0)'
60
61 set @leasesql='select distinct
62 Finance_Areceivable.AreceivableID,--主键
63 (select dbo.fn_GetBargainCellNum(Finance_Areceivable.BargainID))+''(''+Lease_Bargain.BargainNum+'')'' as CellNum, --单元号(合同编号)
64 Finance_ChargeItemSet.name As ExesTypeName,--费用类型
65 Finance_Areceivable.AccountMoney--应收款
66
67 From Finance_Areceivable
68 join system_framework on system_framework.UnitID=Finance_Areceivable.CompanyID
69 join V_Lease_HouseCell on V_Lease_HouseCell.CellID=Finance_Areceivable.CellID or V_Lease_HouseCell.BargainID=Finance_Areceivable.BargainID
70 join House_Client on House_Client.OwnerID=Finance_Areceivable.OwnerID
71 join Lease_Bargain on Finance_Areceivable.BargainID=Lease_Bargain.BargainID
72 left outer join Finance_ChargeItemSet on Finance_Areceivable.ItemID=Finance_ChargeItemSet.ItemID
73 left outer join Sys_ExesGenre on Sys_ExesGenre.ExesGenreID=Finance_ChargeItemSet.ExesGenreID
74 left outer join Sys_ExesType on Sys_ExesType.ExecTypeID=Finance_Areceivable.ItemType
75
76 where (system_framework.UnitCode like '''+@UnitCode+'''+''%'') and Finance_Areceivable.AccountDate between '''
77 +convert(varchar(10),@begindate,120)+''' and '''+convert(varchar(10),@enddate,120)+''' '+@leasewhere+''
78 if(@ExesTypeName<>'')
79 set @leasesql=@leasesql+' and Finance_ChargeItemSet.name='''+@ExesTypeName+''''
80 if(@ExesGenreName<>'')
81 set @leasesql=@leasesql+' and Sys_ExesGenre.ExesGenreName='''+@ExesGenreName+''''
82
83 --向临时表中插入数据
84 set @sql='insert into #Temp_AreceivableClaiGroupBy '+@sql+' union '+@leasesql
85 exec(@sql)
86 --print @sql
87
88 --动态拼接Sql查询语句,实现行列转化
89 declare @sqlGroup varchar(8000)
90 set @sqlGroup = 'select CellNum as ''铺位编号(合同编号)'' '
91 select @sqlGroup = @sqlGroup + ' , sum(case ExesTypeName when ''' + a.ExesTypeName + ''' then AccountMoney else null end) [' + a.ExesTypeName + ']' from (select distinct ExesTypeName from #Temp_AreceivableClaiGroupBy) as a
92 set @sqlGroup = @sqlGroup + ',sum(AccountMoney) as [总计] from #Temp_AreceivableClaiGroupBy group by CellNum '
93 exec(@sqlGroup)
94 --print @sqlGroup
95
96 --删除临时表
97 DROP TABLE #Temp_AreceivableClaiGroupBy
98 end
99end
100
2 *
3 * 作 者 :
4 * 日 期 :2008-03-18
5 * 描 述 :核单台帐表
6 *
7 ***************************************************************************************************/
8
9alter proc Finance_P_AreceivableClaiGroupBy
10@begindate datetime, --开始日期
11@enddate datetime, --截止日期
12@pavilionid varchar(8000), --楼盘代码
13@UnitCode varchar(50),--公司代码
14@ExesTypeName varchar(50),--收费标准
15@ExesGenreName varchar(50)--费用类型
16as
17begin
18 --创建临时表
19 create table #Temp_AreceivableClaiGroupBy
20 (
21 colId int,
22 CellNum varchar(200), --铺位编号(合同编号)
23 ExesTypeName varchar(100),--收费类别
24 AccountMoney decimal(10,2)--应收金额
25 )
26
27 if(@pavilionid<>'')
28 begin
29 /******************************情况一******************************************/
30 declare @where varchar(8000)
31 declare @sql varchar(8000)
32 set @where='and V_Lease_HouseCell.PavilionID in('+@pavilionid+') and V_Lease_HouseCell.BargainID=null'
33
34 set @sql='select distinct
35 Finance_Areceivable.AreceivableID,--主键
36 V_Lease_HouseCell.CellNum, --单元号(合同编号)
37 Finance_ChargeItemSet.name As ExesTypeName,--费用类型
38 Finance_Areceivable.AccountMoney--应收款
39 From Finance_Areceivable
40 join system_framework on system_framework.UnitID=Finance_Areceivable.CompanyID
41 join V_Lease_HouseCell on V_Lease_HouseCell.CellID=Finance_Areceivable.CellID or V_Lease_HouseCell.BargainID=Finance_Areceivable.BargainID
42 join House_Client on House_Client.OwnerID=Finance_Areceivable.OwnerID
43 join Lease_Bargain on Finance_Areceivable.BargainID=Lease_Bargain.BargainID
44 left outer join Finance_ChargeItemSet on Finance_Areceivable.ItemID=Finance_ChargeItemSet.ItemID
45 left outer join Sys_ExesGenre on Sys_ExesGenre.ExesGenreID=Finance_ChargeItemSet.ExesGenreID
46 left outer join Sys_ExesType on Sys_ExesType.ExecTypeID=Finance_Areceivable.ItemType
47
48 where (system_framework.UnitCode like '''+@UnitCode+'''+''%'') and Finance_Areceivable.AccountDate between '''
49 +convert(varchar(10),@begindate,120)+''' and '''+convert(varchar(10),@enddate,120)+''' '+@where+''
50 if(@ExesTypeName<>'')
51 set @sql=@sql+' and Finance_ChargeItemSet.name='''+@ExesTypeName+''''
52 if(@ExesGenreName<>'')
53 set @sql=@sql+' and Sys_ExesGenre.ExesGenreName='''+@ExesGenreName+''''
54 --print @sql
55
56 /*********************************情况二***************************************/
57 declare @leasesql varchar(8000)
58 declare @leasewhere varchar(8000)
59 set @leasewhere='and V_Lease_HouseCell.PavilionID in('+@pavilionid+') and (V_Lease_HouseCell.BargainID<>null or V_Lease_HouseCell.BargainID<>0)'
60
61 set @leasesql='select distinct
62 Finance_Areceivable.AreceivableID,--主键
63 (select dbo.fn_GetBargainCellNum(Finance_Areceivable.BargainID))+''(''+Lease_Bargain.BargainNum+'')'' as CellNum, --单元号(合同编号)
64 Finance_ChargeItemSet.name As ExesTypeName,--费用类型
65 Finance_Areceivable.AccountMoney--应收款
66
67 From Finance_Areceivable
68 join system_framework on system_framework.UnitID=Finance_Areceivable.CompanyID
69 join V_Lease_HouseCell on V_Lease_HouseCell.CellID=Finance_Areceivable.CellID or V_Lease_HouseCell.BargainID=Finance_Areceivable.BargainID
70 join House_Client on House_Client.OwnerID=Finance_Areceivable.OwnerID
71 join Lease_Bargain on Finance_Areceivable.BargainID=Lease_Bargain.BargainID
72 left outer join Finance_ChargeItemSet on Finance_Areceivable.ItemID=Finance_ChargeItemSet.ItemID
73 left outer join Sys_ExesGenre on Sys_ExesGenre.ExesGenreID=Finance_ChargeItemSet.ExesGenreID
74 left outer join Sys_ExesType on Sys_ExesType.ExecTypeID=Finance_Areceivable.ItemType
75
76 where (system_framework.UnitCode like '''+@UnitCode+'''+''%'') and Finance_Areceivable.AccountDate between '''
77 +convert(varchar(10),@begindate,120)+''' and '''+convert(varchar(10),@enddate,120)+''' '+@leasewhere+''
78 if(@ExesTypeName<>'')
79 set @leasesql=@leasesql+' and Finance_ChargeItemSet.name='''+@ExesTypeName+''''
80 if(@ExesGenreName<>'')
81 set @leasesql=@leasesql+' and Sys_ExesGenre.ExesGenreName='''+@ExesGenreName+''''
82
83 --向临时表中插入数据
84 set @sql='insert into #Temp_AreceivableClaiGroupBy '+@sql+' union '+@leasesql
85 exec(@sql)
86 --print @sql
87
88 --动态拼接Sql查询语句,实现行列转化
89 declare @sqlGroup varchar(8000)
90 set @sqlGroup = 'select CellNum as ''铺位编号(合同编号)'' '
91 select @sqlGroup = @sqlGroup + ' , sum(case ExesTypeName when ''' + a.ExesTypeName + ''' then AccountMoney else null end) [' + a.ExesTypeName + ']' from (select distinct ExesTypeName from #Temp_AreceivableClaiGroupBy) as a
92 set @sqlGroup = @sqlGroup + ',sum(AccountMoney) as [总计] from #Temp_AreceivableClaiGroupBy group by CellNum '
93 exec(@sqlGroup)
94 --print @sqlGroup
95
96 --删除临时表
97 DROP TABLE #Temp_AreceivableClaiGroupBy
98 end
99end
100