Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/24.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_Group By_Unpivot - Fatal编程技术网

SQL Server:将多个度量值列聚合为一列

SQL Server:将多个度量值列聚合为一列,sql,sql-server,group-by,unpivot,Sql,Sql Server,Group By,Unpivot,我是一个SQL新手,在提问之前,我会尽最大努力搜索这个主题 我有以下格式的数据: 我需要以下格式: 我意识到这可能很容易,但任何帮助都将不胜感激 谢谢 您可以在SQL SERVER上尝试以下操作: CREATE TABLE TestPvt (Year varchar(32), January int, february int, March int); GO INSERT INTO TestPvt VALUES ('Actual 2019',700,220,456);

我是一个SQL新手,在提问之前,我会尽最大努力搜索这个主题

我有以下格式的数据:

我需要以下格式:

我意识到这可能很容易,但任何帮助都将不胜感激


谢谢

您可以在SQL SERVER上尝试以下操作:

CREATE TABLE TestPvt (Year varchar(32), January int, february int,  
    March int);  
GO  
INSERT INTO TestPvt VALUES ('Actual 2019',700,220,456);  
INSERT INTO TestPvt VALUES ('Budget 2019',200,752,500);  

SELECT Year, Month, Amount  
FROM   
   (SELECT Year, January, february, March  
   FROM TestPvt) p  
UNPIVOT  
   (Amount FOR Month IN   
      (January, february, March)  
)AS unpvt;
结果是:

Year    Month   Amount
Actual 2019 January 700
Actual 2019 february    220
Actual 2019 March   456
Budget 2019 January 200
Budget 2019 february    752
Budget 2019 March   500

您可以在SQL SERVER上尝试此操作:

CREATE TABLE TestPvt (Year varchar(32), January int, february int,  
    March int);  
GO  
INSERT INTO TestPvt VALUES ('Actual 2019',700,220,456);  
INSERT INTO TestPvt VALUES ('Budget 2019',200,752,500);  

SELECT Year, Month, Amount  
FROM   
   (SELECT Year, January, february, March  
   FROM TestPvt) p  
UNPIVOT  
   (Amount FOR Month IN   
      (January, february, March)  
)AS unpvt;
结果是:

Year    Month   Amount
Actual 2019 January 700
Actual 2019 february    220
Actual 2019 March   456
Budget 2019 January 200
Budget 2019 february    752
Budget 2019 March   500

如果您使用的是
SQL Server
,则可以使用
APPLY

SELECT t.year, tt.Months, tt.Amount
FROM TABLE t CROSS APPLY
     ( VALUES (1, 'January', [Month1]), 
              (2, 'February', [Month2]), 
               . . .  
     ) tt(seq, Months, Amount)
ORDER BY tt.seq, t.year;

如果您使用的是
SQL Server
,则可以使用
APPLY

SELECT t.year, tt.Months, tt.Amount
FROM TABLE t CROSS APPLY
     ( VALUES (1, 'January', [Month1]), 
              (2, 'February', [Month2]), 
               . . .  
     ) tt(seq, Months, Amount)
ORDER BY tt.seq, t.year;
你可以试试: 以您提供的列为例,我制作了这个SQL,希望这对您有所帮助! 把桌子换成原样就行了

SELECT Year, 
case when Month = 'Month1' then 'January'
    when Month = 'Month2' then 'February'
    when Month = 'Month3' then 'March' 
    else '' end as Month,
    Amount  
FROM   
   (SELECT Year, Month1, Month2, Month3  
   FROM  [dbo].[ExampleMonths]) tb1  
    UNPIVOT  
   (Amount for Month IN   
      (tb1.Month1, tb1.Month2, tb1.Month3)      
    ) AS uNp
您可以尝试: 以您提供的列为例,我制作了这个SQL,希望这对您有所帮助! 把桌子换成原样就行了

SELECT Year, 
case when Month = 'Month1' then 'January'
    when Month = 'Month2' then 'February'
    when Month = 'Month3' then 'March' 
    else '' end as Month,
    Amount  
FROM   
   (SELECT Year, Month1, Month2, Month3  
   FROM  [dbo].[ExampleMonths]) tb1  
    UNPIVOT  
   (Amount for Month IN   
      (tb1.Month1, tb1.Month2, tb1.Month3)      
    ) AS uNp

您需要使用SQL Server查看
unpivot
和/或
cross-apply
您需要使用SQL Server查看
unpivot
和/或
cross-apply