在网格或任何其他控件中显示像Pivot这样的数据

时间:2013-02-07 19:01:55

标签: c# asp.net sql sql-server-2008 gridview

我想显示像pivot grid这样的数据。我现在正在显示如下数据。请参阅以下图片或点击链接。 http://screencast.com/t/CWeGy0vi enter image description here

但我想要以下数据如下: http://screencast.com/t/ZTb2wk4cdmB enter image description here

任何建议如何实现这一目标。初始点。我应该使用转发器吗?

1 个答案:

答案 0 :(得分:1)

如果没有看到你的桌面结构等,很难给出确切的答案。但我可以建议你如何在SQL中执行此操作。你有一些先前的问题用sql server标记,所以我猜。

您可以同时使用UNPIVOTPIVOT

;with unpiv as
(
  select activity, 
      work, 
      Location+'_'+col as col, 
      value
    from
    (
      select activity,
        work,
        cast(AssignedTasks as varchar(50)) AssignedTasks,
        cast(CompletedTasks as varchar(50)) AchievedTasks,
        Location
      from yourtable
    ) src
    unpivot
    (
      value
      for col in (AssignedTasks, AchievedTasks)
    ) unpiv
),
piv as
(
  select Activity,
    work,
    London_AssignedTasks, 
    London_AchievedTasks,
    Geneva_AssignedTasks, 
    Geneva_AchievedTasks,
    row_number() over(partition by activity order by activity, work) rn
  from unpiv
  pivot
  (
    max(value)
    for col in (London_AssignedTasks, London_AchievedTasks,
                Geneva_AssignedTasks, Geneva_AchievedTasks)
  ) piv
)
select case when rn = 1 then activity else '' end activity,
  work,
  London_AssignedTasks, 
  London_AchievedTasks,
  Geneva_AssignedTasks, 
  Geneva_AchievedTasks
from piv

请参阅SQL Fiddle with Demo

结果是:

|   ACTIVITY |   WORK | LONDON_ASSIGNEDTASKS | LONDON_ACHIEVEDTASKS | GENEVA_ASSIGNEDTASKS | GENEVA_ACHIEVEDTASKS |
-------------------------------------------------------------------------------------------------------------------
| Activity 1 | Task 1 |                   10 |                    8 |                    1 |                    1 |
|            | Task 2 |                   15 |                   15 |                  100 |                   25 |
| Activity 2 | Task 1 |                    5 |                    5 |                    0 |                    0 |
|            | Task 2 |                    0 |                    0 |                    2 |                    2 |
| Activity 3 | Task 1 |                   10 |                   10 |                   50 |                   40 |

编辑#1,如果您有一个未知或动态数量的Locations,那么您可以使用动态SQL返回结果:

DECLARE @cols AS NVARCHAR(MAX),
    @query  AS NVARCHAR(MAX)

select @cols = STUFF((SELECT ',' + QUOTENAME(Location+'_'+t.tasks) 
                    from yourtable
                    cross apply
                    (
                      select 'AssignedTasks' tasks
                      union all
                      select 'AchievedTasks'
                    ) t
                    group by location, tasks
                    order by location
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

set @query = ';with unpiv as
              (
                select activity, 
                    work, 
                    Location+''_''+col as col, 
                    value
                  from
                  (
                    select activity,
                      work,
                      cast(AssignedTasks as varchar(50)) AssignedTasks,
                      cast(CompletedTasks as varchar(50)) AchievedTasks,
                      Location
                    from yourtable
                  ) src
                  unpivot
                  (
                    value
                    for col in (AssignedTasks, AchievedTasks)
                  ) unpiv
              ),
              piv as
              (
                select Activity,
                  work,
                  row_number() over(partition by activity order by activity, work) rn,
                  '+@cols+'
                from unpiv
                pivot
                (
                  max(value)
                  for col in ('+@cols+')
                ) piv
              )
              select case when rn = 1 then activity else '''' end activity,
                work,
                '+@cols+'
              from piv'


execute(@query)

请参阅SQL Fiddle with Demo