0

我想显示像枢轴网格这样的数据。我现在正在显示如下数据。请看下图或点击链接。 http://screencast.com/t/CWeGy0vi 在此处输入图像描述

但我想要以下数据: http ://screencast.com/t/ZTb2wk4cdmB 在此处输入图像描述

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

4

1 回答 1

1

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

您可以同时使用 anUNPIVOT和 a来执行此操作PIVOT

;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

于 2013-02-07T20:32:00.087 回答