sqlserver報表統計——參數化動態PIVOT行轉列,sqlserverpivot

來源:互聯網
上載者:User

sqlserver報表統計——參數化動態PIVOT行轉列,sqlserverpivot


先列出所有表結構:

--title 報表--table 實收水費:hx_t_received --欄位 收費部門id:hx_fdepartmentid  1、收費部門名稱:hx_fdepartmentname 應收水費資訊ID:hx_freceivableid--收費類別:hx_ftype (水費(1):再分[開戶(table) 供水類別:自來水(100000001)==水費,中水(100000002)==中水費],代理費(2)==汙水處理費)--實收金額:hx_freceivedamount  建立時間:createdon 支付方式:hx_fpayway(現金:100,000,000與支票:100,000,001)--交易狀態:hx_fstate(入賬100,000,000: 支票付款100,000,001 除:作廢:100,000,002、銀行付款:100,000,003)--票據資訊:hx_finvoiceid 收費員:CreatedByName 建立時間:createdon  主鍵:hx_t_receivedid--收費部門:hx_fdepartmentid  水表ID:hx_fmeterid 修改時:modifiedon  催費員id:hx_fdunid 收費員:createdby--table 應收水費:hx_t_receivable--欄位  水量資訊ID:hx_fusedamountid 主鍵:hx_t_ReceivableId 客戶資訊:hx_faccountid  水表:hx_fmeterid--選項組:hx_fstate(支票付款中:100,000,002)--table 用水水量:hx_t_waterusedamount--欄位  開戶資訊:hx_fcustomerandmeterrelaid 主鍵:hx_t_WaterUsedAmountId  水表:hx_fmeterid 區段:hx_fzone 實用水量:hx_famount --估水原因:hx_festimateamountreason  結算水量:hx_fpayamount--抄表類型hx_frecordtype (估水:100,000,001 未抄見:100,000,002 正常:100,000,000 預付卡表估水:100,000,003) --水量狀態:hx_fstate(計劃:100,000,000,已錄入水量:100,000,001,已產生應收:100,000,006 本次抄表止度:hx_freading--用水年份:hx_fyear  用水月份:hx_fmonth--table  客戶:account--欄位  主鍵:accountid 客戶名稱:name 客戶編碼:accountnumber--table 客戶賬戶交易明細:hx_t_transactiondetails--欄位 交易類型:hx_ftranstype(賬戶儲值:100,000,000) 交易金額:hx_ftransamount  實收水費資訊:hx_freceivedid 收費員:createdby 區段:hx_fzoneid--交易狀態:hx_fstate(作廢:100,000,002) 票據資訊:hx_finvoiceid 支付方式:hx_fpayway--table  客戶儲值賬戶:hx_t_rechargeaccount--主鍵;hx_t_rechargeaccountid 水表ID:hx_fmeterid 客戶ID:hx_fbuildinaccountid--table 開戶資訊:hx_t_customerandmeterrela--區段:hx_fzoneid 主鍵:hx_t_customerandmeterrelaid 水表:hx_fmeterid 客戶:hx_faccountid--********二********-- table 臨時用水實收:hx_t_tempwaterreceived  主鍵:hx_t_tempwaterreceivedid--欄位 臨時用水:hx_ftempwaterid   支付方式:hx_fpayway(現金:100,000,000與支票:100,000,001)--交易狀態:hx_fstate(除:作廢:100,000,002、銀行付款:100,000,002)--票據資訊:hx_finvoiceid  收費部門ID:hx_fdepartmentid--table 臨時用水:hx_t_tempwater--欄位  主鍵:hx_t_tempwaterid  負責人:ownerid  收費項目:hx_ffeeitems(水費(再分)、汙水費) 供水類別:hx_fwatertype  區段:hx_fzoneid--水量:hx_fusedamount--table  使用者:systemuser--欄位  主鍵:SystemUserId 業務部門:businessunitid 全名:FullName--table 部門:businessunit--欄位 主鍵:businessunitid  名稱:name--table 區段資訊:hx_t_zone--欄位  主鍵:hx_t_zoneid 團隊資訊:hx_fteamid  抄表員:hx_frecorder 區段號:hx_fzone--部門:hx_fbureau  催費員:hx_fdunname(尋找)  部門名稱:hx_fbureauName --table 征費所 hx_t_levyinstitute--欄位 主鍵:hx_t_levyinstituteid 名稱:hx_fname--table 抄表班組歸屬hx_t_teamattribution--欄位 主鍵:hx_t_teamattributionid 抄表班組資訊:hx_fteamid 征費所資訊:hx_flevyinstituteid--table 職員:hx_t_staff--欄位 姓名:hx_name 主鍵:hx_t_staffid
SQL代碼:

--title查表員估收報表declare @begin_date datetimedeclare @end_date datetimeselect @begin_date = getdate()declare @hx_ClosingAccountInfoId uniqueidentifierset @hx_ClosingAccountInfoId='08AFEF9F-E174-46F2-855A-32F26BE38F3B'--2014年09月01期declare @BusinessunitId uniqueidentifier --征費所set @BusinessunitId='6A2537DD-B87A-E411-93FE-002590622897' ----第十五收費所DECLARE @TeamId uniqueidentifier --抄表班組SET @TeamId=null--'18ACBD81-5B45-E411-9402-6CAE8B22702D' --第十五收費所(零散查表組)-----------------SQL本文----------------------------------------------------------執行時間:declare @FMonth intdeclare @FYear intdeclare @FTimes intselect @FYear=t.hx_FYear,@FMonth=t.hx_FMonth,@FTimes=t.hx_FTimes from hx_ClosingAccountInfo t where hx_ClosingAccountInfoId=@hx_ClosingAccountInfoId--select @FYear,@FMonth,@FTimesbeginif object_id('tempdb..#t_estimateamountreason') is not null drop table #t_estimateamountreasonif object_id('tempdb..#t_EstimateamountReasonTotal') is not null drop table #t_EstimateamountReasonTotalselect * 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) tselect * into #t_EstimateamountReasonTotal from(select w.hx_frecordername fullname,--抄表員w.hx_fzone hx_fzone, --區段號1 mcounts,e.Label Label,w.hx_fpayamount hx_fpayamount, --水量r.hx_freceivablefee hx_freceivablefee, --水費r.hx_fcollchargesreceivable4 wsf --汙水費from hx_t_waterusedamount w --水量INNER JOIN Team t --團隊ON w.OwningTeam = t.TeamId  AND isnull(w.OwningTeam,'00000000-0000-0000-0000-000000000000') = isnull(isnull(@TeamId,w.OwningTeam),'00000000-0000-0000-0000-000000000000')AND w.hx_FYear=@FYear and w.hx_FMonth=@FMonth and w.hx_FTimes=@FTimes and w.hx_frecordtype='100000001'INNER JOIN hx_t_teamattribution n    ON t.TeamId=n.hx_fteamid AND isnull(n.hx_flevyinstituteid,'00000000-0000-0000-0000-000000000000') = isnull(isnull(@BusinessunitId,n.hx_flevyinstituteid),'00000000-0000-0000-0000-000000000000')inner JOIN #t_estimateamountreason e --估水原因    ON e.Value=w.hx_festimateamountreason left join hx_t_receivable r --應收on w.hx_t_waterusedamountid=r.hx_fusedamountid  UNION ALLselect null fullname,null hx_fzone,1 mcounts,e.Label Label,0 hx_fpayamount,0 hx_freceivablefee,0 wsf from #t_estimateamountreason e) h--參數化動態PIVOT行轉列DECLARE @sql_str NVARCHAR(MAX)DECLARE @sql_col NVARCHAR(MAX)DECLARE @tableName SYSNAME --行轉列表DECLARE @orderColumn SYSNAME --分組欄位DECLARE @row2column SYSNAME --行變列的欄位DECLARE @row2columnValue SYSNAME --行變列值的欄位DECLARE @OtherField NVARCHAR(100)DECLARE @sql_col_out NVARCHAR(MAX)SET @tableName = '#t_EstimateamountReasonTotal'SET @orderColumn = 'pvt.hx_fzone'SET @row2column = 'Label'SET @row2columnValue = 'mcounts'SET @OtherField='fullname,hx_fzone,hx_fpayamount,hx_freceivablefee,wsf'--從行資料中擷取可能存在的列SET @sql_str = N'SELECT @sql_col_out = ISNULL(@sql_col_out + '','','''') + QUOTENAME(['+@row2column+'])     FROM ['+@tableName+'] GROUP BY ['+@row2column+']'--PRINT @sql_strEXEC sp_executesql @sql_str,N'@sql_col_out NVARCHAR(MAX) OUTPUT',@sql_col_out=@sql_col OUTPUT--PRINT @sql_colSET @sql_str = N'SELECT * FROM (    SELECT '+@OtherField+',['+@row2column+'],['+@row2columnValue+']'+'FROM ['+@tableName+']) p PIVOT     (sum(['+@row2columnValue+']) FOR ['+@row2column+'] IN ( '+ @sql_col +') ) AS pvt where pvt.hx_fzone is not null ORDER BY '+@orderColumn--PRINT (@sql_str)EXEC (@sql_str)drop table #t_estimateamountreasondrop table #t_EstimateamountReasonTotalend---------------------------------------------------------------------------select @end_date = getdate()select datediff(ms,@begin_date,@end_date) as '用時/毫秒'

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.