欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SQL:列转行 博客分类: sql  

程序员文章站 2024-03-21 08:15:16
...
CREATE TABLE #t(  
----[PKID] int primary key identity(101,1),  
    [iyear] [Nvarchar](4) NOT NULL,  
    [budget1] [int] NOT NULL,  
    [budget2] [int] NOT NULL,  
    [budget3] [int] Not Null  
    )  
GO  
INSERT #t  
SELECT '2016',100000,12676575,3623232
GO  
SELECT * FROM #t 

SELECT iyear as 年份,replace(bm,'budget','') as 月份,budget as 预算 
FROM  #t  
UNPIVOT(budget FOR bm IN (budget1,budget2,budget3) ) p

 结果:

SQL:列转行
            
    
    博客分类: sql