首页 > 代码库 > 列转行

列转行

SELECT b.项目,b.[单位],b.[数量/金额] FROM
(SELECT ProjectCategory 项目,
CAST(COUNT(1) AS VARCHAR(20)) AS 数量,
CAST(ISNULL(SUM(InvestAmount),0.0) AS VARCHAR(20)) AS 投资金额,
cast(ISNULL(SUM(MonthIncome),0.0) AS VARCHAR(20)) AS 项目月增收
FROM dbo.TB_IOMProjectInfo
GROUP BY ProjectCategory) a
UNPIVOT
([数量/金额] FOR [单位] IN([数量],[投资金额],[项目月增收])) b

 

 

注意:要转的字段一定要是varchar或nvarchar

列转行