ReportingServies——SQLServer報表開發綜合執行個體,sqlserver觸發器執行個體
文章目錄
ReportingServies報表開發執行個體教程
ReportingServies——SQLServer報表開發綜合執行個體
如果我們安裝了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 datetimedeclare @end_date datetimeselect @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 datetimeDECLARE @EndDate datetimeSET @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 datetimeSET @strSDate=CONVERT(varchar, @StarDate, 23)+' '+@sHMSSET @strEDate=CONVERT(varchar, @EndDate, 23)+' '+@eHMSSET @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_fteamidAND 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_WaterPropertyIdLEFT 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 datetimeSET @strSDate=CONVERT(varchar, @StarDate, 23)+' '+@sHMSSET @strEDate=CONVERT(varchar, @EndDate, 23)+' '+@eHMSSET @StartDateTime=convert(datetime,@strSDate,20)SET @EndDateTime=convert(datetime,@strEDate,20)----------------------------------下面的表------------------------------------------------------if object_id('tempdb..#t_estimateamountreason') is not null drop table #t_estimateamountreasonselect * 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 select1 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----------------------------------------------------------------------------