sqlserver報表統計——引數化動態PIVOT行轉列
先列出所有表結構:
SQL程式碼:--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
--title查表員估收報表
declare @begin_date datetime
declare @end_date datetime
select @begin_date = getdate()
declare @hx_ClosingAccountInfoId uniqueidentifier
set @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 int
declare @FYear int
declare @FTimes int
select @FYear=t.hx_FYear,@FMonth=t.hx_FMonth,@FTimes=t.hx_FTimes from hx_ClosingAccountInfo t where [email protected]_ClosingAccountInfoId
--select @FYear,@FMonth,@FTimes
begin
if object_id('tempdb..#t_estimateamountreason') is not null drop table #t_estimateamountreason
if object_id('tempdb..#t_EstimateamountReasonTotal') is not null drop table #t_EstimateamountReasonTotal
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 * 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 [email protected] and [email protected] and [email protected] 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 ALL
select 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(['[email protected]+'])
FROM ['[email protected]+'] GROUP BY ['[email protected]+']'
--PRINT @sql_str
EXEC sp_executesql @sql_str,N'@sql_col_out NVARCHAR(MAX) OUTPUT',@[email protected]_col OUTPUT
--PRINT @sql_col
SET @sql_str = N'
SELECT * FROM (
SELECT '[email protected]+',['[email protected]+'],['[email protected]+']'+'FROM ['[email protected]+']) p PIVOT
(sum(['[email protected]+']) FOR ['[email protected]+'] IN ( '+ @sql_col +') ) AS pvt
where pvt.hx_fzone is not null ORDER BY '[email protected]
--PRINT (@sql_str)
EXEC (@sql_str)
drop table #t_estimateamountreason
drop table #t_EstimateamountReasonTotal
end
---------------------------------------------------------------------------
select @end_date = getdate()
select datediff(ms,@begin_date,@end_date) as '用時/毫秒'
相關推薦
sqlserver報表統計——引數化動態PIVOT行轉列
先列出所有表結構: --title 報表 --table 實收水費:hx_t_received --欄位 收費部門id:hx_fdepartmentid 1、收費部門名稱:hx_fdepartmentname 應收水費資訊ID:hx_freceivableid --
Oracle根據【日期】組,其他條件根據PIVOT行轉列。使每個日期條件關聯的其他數據只有一行。
col acl time pivot per clas 一行 crop group select OPER_TIME, MICROPAY, REFUND from ( select trunc(oper_time) oper_time,
Oracle pivot行轉列
行轉列語法為pivot(聚合函式 for 列名 in(型別)): pivot ( sum ( planqty ) for plantype in ( 'in', 'out' ) ) 1.建立測試表格 CREATE TABLE XXXMGR.FAB_BSFACTORYDAI
SQL Server 動態行轉列(引數化表名、分組列、行轉列欄位、欄位值)
一.本文所涉及的內容(Contents) 二.背景(Contexts) 其實行轉列並不是一個什麼新鮮的話題了,甚至已經被大家說到爛了,網上的很多例子多多少少都有些問題,所以我希望能讓大家快速的看到執行的效果,所以在動態列的基礎上再把表、分組欄位、行轉列欄位、值這四個行轉列固定需要的值變成真正意義的
ASP.NET SQLServer 行轉列,不固定列,動態列 的實現
前言 剛開始工作就成了被溫水煮的青蛙的話,那該有多難吃啊。所以要保持冷靜的頭腦,好好思考自己該做的不該做的事,好好的提升自己的能力,給自己加點料。嗯,這樣就算真被煮熟了,也會可口很多吧。 問題背景 這個問題是前面工作中遇到的,經過一番努力最終解決了。在
SqlServer PIVOT函式快速實現行轉列,UNPIVOT實現列轉行
我們在寫Sql語句的時候沒經常會遇到將查詢結果行轉列,列轉行的需求,拼接sql字串,然後使用sp_executesql執行sql字串是比較常規的一種做法。但是這樣做實現起來非常複雜,而在SqlServer2005中我們有了PIVOT/UNPIVOT函式可以快速實現行轉列和
sqlserver行轉列與列轉行(PIVOT與UNPIVOT)
PIVOT用於將列值旋轉為列名(即行轉列),在可以用聚合函式配合CASE語句實現 PIVOT的一般語法是:PIVOT(聚合函式(列) FOR 列 in (…) )AS P 完整語法: table_source PIVOT( 聚合函式(value_column) FOR p
sql內置函數pivot強大的行轉列功能
子查詢 註意 ges 中一 rom cnblogs blog 聚合函數 星期六 http://blog.csdn.net/xb12369/article/details/8149608 http://www.cnblogs.com/lwhkdash/archive/20
oracle 存儲過程-動態行轉列,解決。
pla gin sel rom con left join from blog creat 包頭 create or replace package pro_test as TYPE out_cursor IS REF CURSOR; procedure A
sql的行轉列(PIVOT)與列轉行(UNPIVOT)
列轉行 gif 而且 碼農 實現 score username info rect 在做數據統計的時候,行轉列,列轉行是經常碰到的問題。case when方式太麻煩了,而且可擴展性不強,可以使用 PIVOT,UNPIVOT比較快速實現行轉列,列轉行,而且可擴展性強
PIVOT:行轉列函數
sco 課程 計算列 格式 ... str logs sub .com PIVOT函數,行轉列 PIVOT函數的格式如下: PIVOT(<聚合函數>([聚合列值]) FOR [行轉列前的列名] IN([行轉列後的列名1],[行轉列後的列名2],[行轉列後的列名3
sql pivot(行轉列) 和unpivot(列轉行)的用法
sql clas 數據 sele core unp null col style 1、PIVOT用法(行轉列) select * from Table_Score as a pivot (sum(score) for a.name in ([語文],[數學],[外語],[
sqlserver 行轉列(轉)
微軟 技術 all drop 記錄 記錄拆分 div student 數學 1、行轉列 微軟官方的圖: 方法二 -- 行轉列 DROP TABLE studentB; CREATE TABLE studentB (姓名 varchar(10),語文
mybatis 動態行轉列
SELECT user_name , MAX(CASE course WHEN '數學' THEN score END ) 數學, MAX(CASE course WHEN '語文' THEN score END ) 語文, MAX(CASE course WHEN '英語'
sqlserver 行轉列、字串行轉列、自動生產行轉列指令碼
行轉列,老生常談的問題。這裡總結一下網上的方法。 1、生成測試資料: CREATE TABLE human( name NVARCHAR(5), --姓名 norm NVARCHAR(5), --指標 score INT , --分數
SQL行轉列的動態構造方法
SQL行轉列的動態構造方法 /*假設有張學生成績表(tb)如下: 姓名 課程 分數 張三 語文 74 張三 數學 83 張三 物理 93 李四 語文 74 李四 數學 84 李四 物理 94 想變成(得到如下結果): 姓名 語文 數學 物理 ---- ---- ---- ---- 李四 74 8
oracle 11g下的行轉列pivot填坑--xmlType轉clob或String
臨近年關,我們給全公司用的API平臺也到了要裝逼的時刻,然而裝逼利器還沒搞完,那就是報表呈現,於是leader說你來搞一下吧。 echarts肯定是要的,報表呈現還是很完美的。然後就是資料庫層面的了。我們裝的是oracle 11g,這貨提供了pivot來進行轉列,然而它是有坑的,本次記錄的就是這個。 pi
Mysql 列轉行統計查詢 、行轉列統計查詢
- ---------------------------- -- Table structure for `TabName` -- ---------------------------- DROP TABLE IF EXISTS `TabN
pivot 行變列
with temp as(select '四川省' nation ,'成都市' city,'第一' ranking from dual union allselect '四川省' nation ,'綿陽市' city,'第二' ranking from dual union allselect '四川省' n
spark 累加歷史 + 統計全部 + 行轉列
轉載自https://www.cnblogs.com/piaolingzxh/p/5538783.html 感覺寫的特別好,特別有用 spark 累加歷史主要用到了視窗函式,而進行全部統計,則需要用到rollup函式 1 應用場景: 1、我們需要統計使用者的總