行转列

 

 

SQL 行转列,

 

CREATE PROCEDURE [SP_HRSAL] AS

declare @SQL varchar(8000)
   set @SQL=' SELECT PLANT,EPNO,YEA,MON'
   select @SQL = @SQL + ', SUM( CASE SRNO WHEN '''+SRNO+''' THEN AMT END) [S'+SRNO+']'
   from ( select distinct SRNO from HRSALD) as a
   select @SQL = @SQL + ' FROM  HRSALD GROUP BY PLANT,EPNO,YEA,MON'
  exec(@SQL)
GO

你可能感兴趣的:(sql,Go)