0

我有一个存储过程,它以以下格式返回数据:

Date  |  Unit A Count  |  Unit B Count  |  Unit C Count
2010  |         335    |          52    |         540
2011  |         384    |          70    |         556
2012  |         145    |          54    |         345

这个存储过程在内部调用了另一个存储过程,所以我不能再次嵌套它。
我所追求的是一种旋转上述输出的方法,它看起来像这样:

              |  2010  |  2011  |  2012
Unit A Count  |   335  |   384  |   145
Unit B Count  |    52  |    70  |    54
Unit C Count  |   540  |   556  |   345

编辑:使用 3 个参数调用 StorProc,我确实需要保留原始输出。

有任何想法吗?

我正在使用 SQL Server 2005、Visual Studio 2008、C#,输出将转到 .aspx 页面。

4

1 回答 1

3

如果要转换存储过程的结果,则需要使用UNPIVOT 和 PIVOT来获得最终产品:

查看带有演示的 SQL Fiddle

select *
from 
(
    select date, value, unit
    from 
    (
        select *
        from t1
    ) x
    unpivot ([value] for unit in ([unita], [unitb], [unitc])) u
) a
pivot
(
    sum(value)
    for date in ([2010], [2011], [2012])
) p

您想在存储过程中转换这些值,然后您必须发布完整的查询以便我们可以看到它。

于 2012-06-21T18:53:45.393 回答