码迷,mamicode.com
首页 > 数据库 > 详细

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

时间:2015-01-27 20:26:25      阅读:388      评论:0      收藏:0      [点我收藏+]

标签:

上一篇:ReportingServies报表开发实例教程

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

技术分享


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

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

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

技术分享

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

--table 用水水量:hx_t_waterusedamount
--字段 开户信息:hx_fcustomerandmeterrelaid 主键:hx_t_WaterUsedAmountId  水表:hx_fmeterid 区段:hx_fzone 实用水量:hx_famount 
--估水原因:hx_festimateamountreason  结算水量:hx_fpayamount
--抄表类型hx_frecordtype (估水:,000,001 未抄见:100,000,002 正常:100,000,000 预付费卡表估水:100,000,003) 
--水量状态:hx_fstate(计划:100,000,000,已录入水量:100,000,001,等待录入金额:100,000,002,已生成应收:100,000,006,等待审核:100,000,004) 
<span style="font-family:宋体;">--</span>本次抄表止度:hx_freading
--用水年份:hx_fyear  用水月份:hx_fmonth 次:hx_FTimes 
--本次抄表时间:hx_frecorddate  预计水费:hx_fprepayment 预计代收费:hx_fpreproxypayment

--table 用水类别hx_t_watercategory
--field 统计一类:hx_fstatcategoryoneid 主键:hx_t_watercategoryid 名称:hx_fname

--table 用水性质hx_t_waterproperty
--field 主键:hx_t_waterpropertyid 用水类别:hx_fwatercategoryid

--table 抄表班组归属hx_t_teamattribution
--字段主键:hx_t_teamattributionid 抄表班组信息:hx_fteamid 征费所信息:hx_flevyinstituteid

好了,现在正式开始

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

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

 技术分享

我们来看SQL

declare @begin_date datetime
declare @end_date datetime
select @begin_date = getdate()

declare @BusinessunitId uniqueidentifier --征费所
--set @BusinessunitId='6A2537DD-B87A-E411-93FE-002590622897' ----第十五收费所
DECLARE @TeamId uniqueidentifier --抄表班组
--SET @TeamId='18ACBD81-5B45-E411-9402-6CAE8B22702D' --第十五收费所(零散查表组)
DECLARE @StarDate datetime
DECLARE @EndDate datetime
SET @StarDate =  convert(datetime,'2015-01-15 00:00:00',20)
SET @EndDate = convert(datetime,'2015-01-25 23:59:59',20)
DECLARE @sHMS nvarchar(10)
DECLARE @eHMS nvarchar(10)
SET @sHMS='00:00:00';
SET @eHMS='23:59:59';
DECLARE @ZoneNO varchar(30)

-----------------SQL正文----------------------------------------------------------执行时间:
DECLARE @strSDate nvarchar(50),@strEDate nvarchar(50)
DECLARE @StartDateTime datetime,@EndDateTime datetime
SET @strSDate=CONVERT(varchar, @StarDate, 23)+' '+@sHMS
SET @strEDate=CONVERT(varchar, @EndDate, 23)+' '+@eHMS
SET @StartDateTime=convert(datetime,@strSDate,20)
SET @EndDateTime=convert(datetime,@strEDate,20)
-------------------------------------上面的表-------------------------------------------------
select 
1 WaterMeterCount, --表数
w.hx_fpayamount, --水量
w.hx_fprepayment,--预计水费
w.hx_fpreproxypayment,--预计代收费
c.hx_fname --用水类别
 from 
hx_t_waterusedamount w --水量
INNER JOIN hx_t_teamattribution n --抄表班组归属
    ON w.OwningTeam=n.hx_fteamid
AND isnull(w.hx_fzone,'') = isnull(isnull(@ZoneNO,w.hx_fzone),'') 
AND isnull(w.OwningTeam,'00000000-0000-0000-0000-000000000000') = isnull(isnull(@TeamId,w.OwningTeam),'00000000-0000-0000-0000-000000000000') 
AND w.hx_fpayamount>0 --结算水量
AND w.hx_frecorddate BETWEEN @StartDateTime AND @EndDateTime 
AND isnull(n.hx_flevyinstituteid,'00000000-0000-0000-0000-000000000000') = isnull(isnull(@BusinessunitId,n.hx_flevyinstituteid),
'00000000-0000-0000-0000-000000000000') 
LEFT JOIN hx_t_waterproperty p --用水性质
on w.hx_FWaterPropertyID=p.hx_t_WaterPropertyId
LEFT JOIN hx_t_watercategory c --用水类别
on p.hx_FWaterCategoryID=c.hx_t_WaterCategoryId

-----------------END----------------------------------------------------------------------------
select @end_date = getdate()
select datediff(ms,@begin_date,@end_date) as '用时/毫秒'

执行结果如下:

技术分享

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

操作步骤如下:

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

技术分享

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

技术分享

技术分享

技术分享技术分享

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

技术分享

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

技术分享


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

技术分享

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

右侧报表数据,

技术分享


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

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

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

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

技术分享

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

先看参数:BusinessunitId

技术分享技术分享


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

技术分享技术分享


再来看下参数:TeamId

技术分享技术分享技术分享技术分享


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

技术分享技术分享技术分享技术分享技术分享

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

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

数量/水量:#,0

单价:0.00

金额:#,0.00

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

技术分享

SQL如下:

----------------SQL正文----------------------------------------------------------执行时间:
DECLARE @strSDate nvarchar(50),@strEDate nvarchar(50)
DECLARE @StartDateTime datetime,@EndDateTime datetime
SET @strSDate=CONVERT(varchar, @StarDate, 23)+' '+@sHMS
SET @strEDate=CONVERT(varchar, @EndDate, 23)+' '+@eHMS
SET @StartDateTime=convert(datetime,@strSDate,20)
SET @EndDateTime=convert(datetime,@strEDate,20)
----------------------------------下面的表------------------------------------------------------
if object_id('tempdb..#t_estimateamountreason') is not null drop table #t_estimateamountreason

select * into #t_estimateamountreason from   
(select distinct t1.Label,t2.Value from MetadataSchema.LocalizedLabel t1   
inner join MetadataSchema.AttributePicklistValue t2  
 on t1.ObjectId=t2.AttributePicklistValueId inner join MetadataSchema.OptionSet t3 on t2.OptionSetId=t3.OptionSetId   
 where t3.Name='hx_estimateamountreason_values' and t1.ObjectColumnName='DisplayName' and t1.LanguageId=2052) t
--select * from #t_estimateamountreason 
 
select
1 MeterCounts, --水表块数
isnull(e.Label,'其它') ReasonName  --估水原因名称
from hx_t_waterusedamount w --水量 
INNER JOIN hx_t_teamattribution n  --抄表班组归属
    ON w.OwningTeam=n.hx_fteamid 
AND isnull(w.hx_fzone,'') = isnull(isnull(@ZoneNO,w.hx_fzone),'') 
AND isnull(w.OwningTeam,'00000000-0000-0000-0000-000000000000') = isnull(isnull(@TeamId,w.OwningTeam),'00000000-0000-0000-0000-000000000000') 
AND w.hx_fpayamount=0 --结算水量
AND w.hx_frecorddate BETWEEN @StartDateTime AND @EndDateTime 
AND isnull(n.hx_flevyinstituteid,'00000000-0000-0000-0000-000000000000') = isnull(isnull(@BusinessunitId,n.hx_flevyinstituteid),
'00000000-0000-0000-0000-000000000000')  
LEFT JOIN #t_estimateamountreason e --估水原因 
    ON e.Value=w.hx_festimateamountreason   
    
UNION ALL  
select 1 MeterCounts,e.Label ReasonName from #t_estimateamountreason e 
  
 if object_id('tempdb..#t_estimateamountreason') is not null drop table #t_estimateamountreason
----------------END----------------------------------------------------------------------------

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

标签:

原文地址:http://blog.csdn.net/zouyujie1127/article/details/43198263

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!