如何在MVC中的Webgrid上显示数据透视表

时间:2019-02-23 12:24:10

标签: asp.net asp.net-mvc-4

我正在尝试显示每月的员工出勤情况。因此,我创建了一个存储过程,该存储过程使用数据透视表添加了动态列。我不知道如何访问枢轴列。因此,我无法创建具有动态字段数的类作为数据源。

存储过程

ALTER procedure [dbo].[sps_AttendanceShow] @mon int, @year int As
begin DECLARE @cols  AS NVARCHAR(MAX)=''; DECLARE @query AS
NVARCHAR(MAX)='';

SELECT @cols = @cols + QUOTENAME(AttendaceDate) + ',' FROM (  select 
e.Name,a.WorkHours,a.AttendaceDate from  Attendace a,employee e where
e.EmpID=a.EmpID AND MONTH(a.AttendaceDate)=@mon AND
YEAR(a.AttendaceDate)=@year) as temp3 select @cols = substring(@cols,
0, len(@cols)) 

set @query =  'SELECT * from  (
    select  e.Name,a.WorkHours,a.AttendaceDate from  Attendace a,employee e where e.EmpID=a.EmpID  ) src pivot  (
    max(WorkHours) for AttendaceDate in (' + @cols + ') ) piv'

execute(@query) end

结果如下:

enter image description here

1 个答案:

答案 0 :(得分:1)

您可以添加具有30个字段的模型类。