Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.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 如何在取消PIVOT之前重命名列?SQL Server_Sql Server_Database - Fatal编程技术网

Sql server 如何在取消PIVOT之前重命名列?SQL Server

Sql server 如何在取消PIVOT之前重命名列?SQL Server,sql-server,database,Sql Server,Database,帮助我想在使用unpivot方法之前重命名列 我的桌子是这样的: select t.id, t.value, x.* from t cross apply (values (1,ene),(2,feb),(3,mar),(4,abr),(5,may)) x (Mnth,Amount) 列表项 id| value| ENE| FEB| MAR| ABR| MAY 1 | dsads|2000|2334|2344|2344|2344 解开皮弗后,我得到了这样的东西 id| value| mo

帮助我想在使用
unpivot
方法之前重命名列

我的桌子是这样的:

select t.id, t.value, x.*
from t
cross apply (values (1,ene),(2,feb),(3,mar),(4,abr),(5,may)) x (Mnth,Amount)
列表项

id| value| ENE| FEB| MAR| ABR| MAY  
1 | dsads|2000|2334|2344|2344|2344
解开皮弗后,我得到了这样的东西

id| value| month| amount
1 |  dads| ENE  | 2000
2 |  sadf| FEB  | 2334
但是我想要这样的东西

id| value| month| amount
1 |  dads| 01   | 2000
2 |  sadf| 02   | 2334
这是我的问题

select 
    [año], [Empresa], [Region], [Suc], [CC], [Cuenta], 
    [Subcuenta], [Descripcion], Periodo, Importe
from 
    (select * from [dbo].['P Cargado$']) S
unpivot 
    (Importe for Periodo in
        ([ENE], [FEB], [MAR], [ABR], [MAY], [JUN], [JUL], [AGO], [SEP],[OCT], [NOV], [DIC])
    ) AS unpvt;
如果您使用unpivot,您可以这样做:

select t.id, t.value, x.*
from t
cross apply (values (1,ene),(2,feb),(3,mar),(4,abr),(5,may)) x (Mnth,Amount)
rextester演示:

返回:

+----+-------+------+--------+
| id | value | Mnth | Amount |
+----+-------+------+--------+
|  1 | dsads |    1 |   2000 |
|  1 | dsads |    2 |   2334 |
|  1 | dsads |    3 |   2344 |
|  1 | dsads |    4 |   2344 |
|  1 | dsads |    5 |   2344 |
+----+-------+------+--------+

“sadf”来自哪里?它不在
列表项中。