纵有疾风起
人生不言弃

ReportingServies——SQLServer报表开发综合实例

如果我们安装了sqlserver2008 R2,将会自动安装一个报表开发工具

ReportingServies——SQLServer报表开发综合实例插图

不要以为此报表开发工具只适合于sqlserver2008,其实在sqlserver2012中也是支持的,事实上我现在项目中就是使用的sqlserver2012

使用此工具进行报表开发,将会十分快速而且方便。这里我通过一个综合实例来演示它的使用技巧,可以毫不夸张的说,如果你能使用此工具把这个Demo做出来,那么工作中遇到的80%报表,你都能够使用此工具进行开发了。

先来看下我们要实现的效果:

ReportingServies——SQLServer报表开发综合实例插图1

 

报表中使用到的表及字段:

  1. –table 用水水量:hx_t_waterusedamount  
  2. –字段 开户信息:hx_fcustomerandmeterrelaid 主键:hx_t_WaterUsedAmountId  水表:hx_fmeterid 区段:hx_fzone 实用水量:hx_famount   
  3. –估水原因:hx_festimateamountreason  结算水量:hx_fpayamount  
  4. –抄表类型hx_frecordtype (估水:,000,001 未抄见:100,000,002 正常:100,000,000 预付费卡表估水:100,000,003)   
  5. –水量状态:hx_fstate(计划:100,000,000,已录入水量:100,000,001,等待录入金额:100,000,002,已生成应收:100,000,006,等待审核:100,000,004)   
  6. <span style=“font-family:宋体;”>–</span>本次抄表止度:hx_freading  
  7. –用水年份:hx_fyear  用水月份:hx_fmonth 次:hx_FTimes   
  8. –本次抄表时间:hx_frecorddate  预计水费:hx_fprepayment 预计代收费:hx_fpreproxypayment  
  9.   
  10. –table 用水类别hx_t_watercategory  
  11. –field 统计一类:hx_fstatcategoryoneid 主键:hx_t_watercategoryid 名称:hx_fname  
  12.   
  13. –table 用水性质hx_t_waterproperty  
  14. –field 主键:hx_t_waterpropertyid 用水类别:hx_fwatercategoryid  
  15.   
  16. –table 抄表班组归属hx_t_teamattribution  
  17. –字段主键:hx_t_teamattributionid 抄表班组信息:hx_fteamid 征费所信息:hx_flevyinstituteid  

好了,现在正式开始

从效果图上面可以看出,此报表,我们使用到了两张表来展示,而且这里是使用到了两个数据集,但是他们的查询条件是一致的。

我们先来看上面那张表的展示

 ReportingServies——SQLServer报表开发综合实例插图4

我们来看SQL

  1. declare @begin_date datetime  
  2. declare @end_date datetime  
  3. select @begin_date = getdate()  
  4.   
  5. declare @BusinessunitId uniqueidentifier –征费所  
  6. –set @BusinessunitId=’6A2537DD-B87A-E411-93FE-002590622897′ —-第十五收费所  
  7. DECLARE @TeamId uniqueidentifier –抄表班组  
  8. –SET @TeamId=’18ACBD81-5B45-E411-9402-6CAE8B22702D’ –第十五收费所(零散查表组)  
  9. DECLARE @StarDate datetime  
  10. DECLARE @EndDate datetime  
  11. SET @StarDate =  convert(datetime,‘2015-01-15 00:00:00’,20)  
  12. SET @EndDate = convert(datetime,‘2015-01-25 23:59:59’,20)  
  13. DECLARE @sHMS nvarchar(10)  
  14. DECLARE @eHMS nvarchar(10)  
  15. SET @sHMS=’00:00:00′;  
  16. SET @eHMS=’23:59:59′;  
  17. DECLARE @ZoneNO varchar(30)  
  18.   
  19. —————–SQL正文———————————————————-执行时间:  
  20. DECLARE @strSDate nvarchar(50),@strEDate nvarchar(50)  
  21. DECLARE @StartDateTime datetime,@EndDateTime datetime  
  22. SET @strSDate=CONVERT(varchar, @StarDate, 23)+‘ ‘+@sHMS  
  23. SET @strEDate=CONVERT(varchar, @EndDate, 23)+‘ ‘+@eHMS  
  24. SET @StartDateTime=convert(datetime,@strSDate,20)  
  25. SET @EndDateTime=convert(datetime,@strEDate,20)  
  26. ————————————-上面的表————————————————-  
  27. select   
  28. 1 WaterMeterCount, –表数  
  29. w.hx_fpayamount, –水量  
  30. w.hx_fprepayment,–预计水费  
  31. w.hx_fpreproxypayment,–预计代收费  
  32. c.hx_fname –用水类别  
  33.  from   
  34. hx_t_waterusedamount w –水量  
  35. INNER JOIN hx_t_teamattribution n –抄表班组归属  
  36.     ON w.OwningTeam=n.hx_fteamid  
  37. AND isnull(w.hx_fzone,”) = isnull(isnull(@ZoneNO,w.hx_fzone),”)   
  38. AND isnull(w.OwningTeam,‘00000000-0000-0000-0000-000000000000’) = isnull(isnull(@TeamId,w.OwningTeam),‘00000000-0000-0000-0000-000000000000’)   
  39. AND w.hx_fpayamount>0 –结算水量  
  40. AND w.hx_frecorddate BETWEEN @StartDateTime AND @EndDateTime   
  41. AND isnull(n.hx_flevyinstituteid,‘00000000-0000-0000-0000-000000000000’) = isnull(isnull(@BusinessunitId,n.hx_flevyinstituteid),  
  42. ‘00000000-0000-0000-0000-000000000000’)   
  43. LEFT JOIN hx_t_waterproperty p –用水性质  
  44. on w.hx_FWaterPropertyID=p.hx_t_WaterPropertyId  
  45. LEFT JOIN hx_t_watercategory c –用水类别  
  46. on p.hx_FWaterCategoryID=c.hx_t_WaterCategoryId  
  47.   
  48. —————–END—————————————————————————-  
  49. select @end_date = getdate()  
  50. select datediff(ms,@begin_date,@end_date) as ‘用时/毫秒’  

执行结果如下:

ReportingServies——SQLServer报表开发综合实例插图5

可以看出这是十分常见的纵表变横表了,如果使用纯sql来实现转换效果的话,就有得写一连串复杂的sql语句了,但是通过使用此报表开发工具,我们可以轻而易举的将其纵向显示结果变为横向显示结果。

操作步骤如下:

1、打开此报表开发工具,文件——新建——项目

ReportingServies——SQLServer报表开发综合实例插图6

2、右键单击右侧解决方案中的报表目录,然后一直下一步,

ReportingServies——SQLServer报表开发综合实例插图7

ReportingServies——SQLServer报表开发综合实例插图8

ReportingServies——SQLServer报表开发综合实例插图9ReportingServies——SQLServer报表开发综合实例插图10

但是考虑到矩阵的界面配置甚为反锁,这里我们先选择表格吧,然后一直点击下一步,直到完成

ReportingServies——SQLServer报表开发综合实例插图11

1、把界面上面的表格删掉,然后从左侧工具栏拖入一个矩阵,

ReportingServies——SQLServer报表开发综合实例插图12

选中“数据”单元格,右键——插入行——组内部——下方,添加4行,然后右键——插入列——组外部(因为要统计合计)

ReportingServies——SQLServer报表开发综合实例插图13

列这个地方,选择hx_fname这个字段,从sql中可以看到hx_fname这个字段是我们需要转换为横表展示的字段,然后数据这里,选中要聚合的字段。

右侧报表数据,

ReportingServies——SQLServer报表开发综合实例插图14

这里自动生成了许多参数,这些参数是因为我们刚才添加的SQL语句里面用到了,注意看名称,和我们刚才声明的SQL语句是一样的。

选中属性,右键单击,可以查看参数的配置信息

这些东西大家多去点一下,自然就熟悉了,涉及的东西太多了,我一下子也说不完。

这里有两个参数是有级联关系的,我就先说下

ReportingServies——SQLServer报表开发综合实例插图15

我们来看下这两个参数的配置,这两个参数都是以下拉框的形式显示的,而且值都是取自数据库中。

先看参数:BusinessunitId

ReportingServies——SQLServer报表开发综合实例插图16ReportingServies——SQLServer报表开发综合实例插图17

这个数据集哪里来的?我们可以自己添加

ReportingServies——SQLServer报表开发综合实例插图18ReportingServies——SQLServer报表开发综合实例插图19

再来看下参数:TeamId

ReportingServies——SQLServer报表开发综合实例插图20ReportingServies——SQLServer报表开发综合实例插图21ReportingServies——SQLServer报表开发综合实例插图22ReportingServies——SQLServer报表开发综合实例插图23

右键单击空白处——创建占位符

ReportingServies——SQLServer报表开发综合实例插图24ReportingServies——SQLServer报表开发综合实例插图25ReportingServies——SQLServer报表开发综合实例插图26ReportingServies——SQLServer报表开发综合实例插图27ReportingServies——SQLServer报表开发综合实例插图28

 

批量修改可以直接通过这里进行修改,

否则,只能一个文本框一个文本框属性进行修改了。#代表千分位显示,后面的数字代码保留小数位数,0代表整数

数量/水量:#,0

单价:0.00

金额:#,0.00

报表中下面那个程序集的操作类似,这里就不多说了,东西太多了。

ReportingServies——SQLServer报表开发综合实例插图29

SQL如下:

    1. —————-SQL正文———————————————————-执行时间:  
    2. DECLARE @strSDate nvarchar(50),@strEDate nvarchar(50)  
    3. DECLARE @StartDateTime datetime,@EndDateTime datetime  
    4. SET @strSDate=CONVERT(varchar, @StarDate, 23)+‘ ‘+@sHMS  
    5. SET @strEDate=CONVERT(varchar, @EndDate, 23)+‘ ‘+@eHMS  
    6. SET @StartDateTime=convert(datetime,@strSDate,20)  
    7. SET @EndDateTime=convert(datetime,@strEDate,20)  
    8. ———————————-下面的表——————————————————  
    9. if object_id(‘tempdb..#t_estimateamountreason’) is not null drop table #t_estimateamountreason  
    10.   
    11. select * into #t_estimateamountreason from     
    12. (select distinct t1.Label,t2.Value from MetadataSchema.LocalizedLabel t1     
    13. inner join MetadataSchema.AttributePicklistValue t2    
    14.  on t1.ObjectId=t2.AttributePicklistValueId inner join MetadataSchema.OptionSet t3 on t2.OptionSetId=t3.OptionSetId     
    15.  where t3.Name=‘hx_estimateamountreason_values’ and t1.ObjectColumnName=‘DisplayName’ and t1.LanguageId=2052) t  
    16. –select * from #t_estimateamountreason   
    17.    
    18. select  
    19. 1 MeterCounts, –水表块数  
    20. isnull(e.Label,‘其它’) ReasonName  –估水原因名称  
    21. from hx_t_waterusedamount w –水量   
    22. INNER JOIN hx_t_teamattribution n  –抄表班组归属  
    23.     ON w.OwningTeam=n.hx_fteamid   
    24. AND isnull(w.hx_fzone,”) = isnull(isnull(@ZoneNO,w.hx_fzone),”)   
    25. AND isnull(w.OwningTeam,‘00000000-0000-0000-0000-000000000000’) = isnull(isnull(@TeamId,w.OwningTeam),‘00000000-0000-0000-0000-000000000000’)   
    26. AND w.hx_fpayamount=0 –结算水量  
    27. AND w.hx_frecorddate BETWEEN @StartDateTime AND @EndDateTime   
    28. AND isnull(n.hx_flevyinstituteid,‘00000000-0000-0000-0000-000000000000’) = isnull(isnull(@BusinessunitId,n.hx_flevyinstituteid),  
    29. ‘00000000-0000-0000-0000-000000000000’)    
    30. LEFT JOIN #t_estimateamountreason e –估水原因   
    31.     ON e.Value=w.hx_festimateamountreason     
    32.       
    33. UNION ALL    
    34. select 1 MeterCounts,e.Label ReasonName from #t_estimateamountreason e   
    35.     
    36.  if object_id(‘tempdb..#t_estimateamountreason’) is not null drop table #t_estimateamountreason  
    37. —————-END—————————————————————————- 

文章转载于:https://www.cnblogs.com/jiekzou/p/4395768.html

原著是一个有趣的人,若有侵权,请通知删除

未经允许不得转载:起风网 » ReportingServies——SQLServer报表开发综合实例
分享到: 生成海报

评论 抢沙发

评论前必须登录!

立即登录