Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将Sql server表中的表列转换为垂直数据?_Sql_Sql Server_Pivot_Crosstab_Unpivot - Fatal编程技术网

如何将Sql server表中的表列转换为垂直数据?

如何将Sql server表中的表列转换为垂直数据?,sql,sql-server,pivot,crosstab,unpivot,Sql,Sql Server,Pivot,Crosstab,Unpivot,我想将一个Sql server表转换为另一个 原始表格 Period Date Portfolio Benchmark Pre0Month 12/31/2014 -0.0001 -0.0025 Pre1Month 11/31/2014 0.0122 0.0269 Pre2Month 10/31/2014 0.0176 0.0244 改造后 Returns

我想将一个Sql server表转换为另一个

原始表格

    Period      Date          Portfolio   Benchmark

    Pre0Month   12/31/2014   -0.0001      -0.0025
    Pre1Month   11/31/2014    0.0122       0.0269
    Pre2Month   10/31/2014    0.0176       0.0244
改造后

    Returns      Pre0Month   Pre1Month    Pre2Month

    Portfolio   -0.0001      0.0122       0.0176
    Benchmark   -0.0025      0.0269       0.0244

因为您使用的是SQL Server,所以可以使用pivot命令执行您想要的操作。在这里查看:


您也可以使用DateDiff函数轻松地将日期按月分开

考虑到表名为MyTable,您可以通过以下方式对其进行透视:

SELECT * FROM
(
  SELECT Period, [Returns], value
  FROM MyTable
  CROSS APPLY
  (
    SELECT 'Portofolio', CAST(Portofolio as varchar(10)) 
    UNION ALL
    SELECT 'Benchmark', CAST(Benchmark as varchar(10)) 
  ) c([Returns], value)
) d
PIVOT
(
    MAX(value)
    FOR Period IN (Pre0Month, Pre1Month, Pre2Month)
) piv;

这需要枢轴和取消枢轴的组合:

DECLARE @t TABLE(period VARCHAR(32),[date] DATETIME, portfolio DECIMAL(28,4), benchmark DECIMAL(28,4));
INSERT INTO @t(period,[date],portfolio,benchmark)VALUES('Pre0Month','2014-12-31',-0.0001,-0.0025);
INSERT INTO @t(period,[date],portfolio,benchmark)VALUES('Pre1Month','2014-11-30',0.0122,0.0269);
INSERT INTO @t(period,[date],portfolio,benchmark)VALUES('Pre2Month','2014-10-31',0.0176,0.0244);

SELECT
    *
FROM
    (
        SELECT
            *
        FROM
            (
                SELECT
                    period,
                    portfolio,
                    benchmark
                FROM
                    @t
            ) AS t
            UNPIVOT(
                value
                FOR Returns IN (portfolio,benchmark)
            ) AS up
    ) AS t
    PIVOT(
        MAX(value)
        FOR period IN ([Pre0Month],[Pre1Month],[Pre2Month])
    ) AS p;
结果如下:

Returns     Pre0Month   Pre1Month   Pre2Month
benchmark   -0.0025     0.0269      0.0244
portfolio   -0.0001     0.0122      0.0176

非常感谢。两个答案都很有魅力。一个使用交叉应用,另一个使用Unpivot。我不能接受两个答案吗?你不能接受两个答案:)。就我个人而言,我更喜欢使用
UNPIVOT
查询,因为这更清楚地反映了意图<代码>交叉应用还用于其他用途,而不仅仅是取消填充。如果您看到
UNPIVOT
您知道它将取消pivot,而您必须分析使用
CROSS-APPLY
执行的操作,才能知道取消pivot就是执行的操作<代码>交叉应用确实有它的好处,例如,如果您想更改数据类型,可以按照George的回答进行转换。但由于没有必要这样做,我在回答中使用了
UNPIVOT
。这一点非常正确:)。