本文介绍了我不想动态传递working_dates,我想在pivot中的范围之间传递working_dates的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 选择 * 来自 ( SELECT [TM_UserID] ,[FullName] ,[Worked_dte] ,[Worked_Hours] FROM @ Reporting_User_Timesheet 其中 [working_dte] BETWEEN ' 2014-04-04' AND ' 2014-04-06' ) as sourceTable Pivot ( sum([Worked_Hours]) for [Worked_dte] ([2014-04-04],[2014-04-05],[ 2014-04-06])) as 数据透视表 解决方案 尝试这个: DECLARE @cols AS NVARCHAR (MAX); DECLARE @ query AS NVARCHAR (MAX); 选择 @cols = STUFF(( SELECT distinct ' ,' + QUOTENAME(Worked_dte) FROM User_Timesheet FOR XML PATH(' '), TYPE )。value(' 。',' NVARCHAR(MAX)'), 1 , 1 ,' '); SELECT @ query = ' SELECT * FROM ( SELECT [TM_UserID], [FullName], [Worked_dte ], [Worked_Hours] 来自User_Timesheet,其中[working_dte] BETWEEN''2014-04-04''和''2014-04-06'')AS t PIVOT ( MAX(Worked_Hours) FOR Worked_dte IN(' + @cols + ' )' + ' )AS p; ; 执行( @ query ); select * from(SELECT [TM_UserID] ,[FullName] ,[Worked_dte] ,[Worked_Hours] FROM @Reporting_User_Timesheet where [worked_dte] BETWEEN '2014-04-04' AND '2014-04-06') as sourceTablePivot( sum([Worked_Hours]) for [Worked_dte] in ([2014-04-04],[2014-04-05],[2014-04-06])) as PivotTable 解决方案 TRY THIS:DECLARE @cols AS NVARCHAR(MAX);DECLARE @query AS NVARCHAR(MAX);select @cols = STUFF((SELECT distinct ',' + QUOTENAME(Worked_dte) FROM User_Timesheet FOR XML PATH(''), TYPE ).value('.', 'NVARCHAR(MAX)') , 1, 1, '');SELECT @query = 'SELECT * FROM(SELECT[TM_UserID],[FullName],[Worked_dte],[Worked_Hours] FROM User_Timesheet where [worked_dte] BETWEEN ''2014-04-04'' AND ''2014-04-06'' ) AS tPIVOT ( MAX(Worked_Hours) FOR Worked_dte IN( ' + @cols + ' )' +' ) AS p ; '; execute(@query); 这篇关于我不想动态传递working_dates,我想在pivot中的范围之间传递working_dates的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-25 09:54