First Kind
SELECT title,
SUM (Case typeid if ' 1 ' then gjdate ELSE 0 END) as ' take some ',
SUM (case typeid if ' 2 ' then gjdate ELSE 0 END) as ' leave ',
SUM (Case typeid if ' 3 ' then gjdate ELSE 0 END) as ' overtime '
From LFIL_TB
GROUP by Title
The second Kind
DECLARE @sql_str NVARCHAR (max) DECLARE @sql_col NVARCHAR (max) DECLARE @tableName SYSNAME--table name declare @groupColumn SYSNAME--Group Field (title name what thing) DECLARE @row2column SYSNAME--row-to-column fields declare @row2columnValue SYSNAME--field set @tableName for row variable column values='LFIL_TB'SET @groupColumn='title'SET @row2column='typeID'SET @row2columnValue='gjdate'--gets the column set that may exist from the row data @sql_str= N'SELECT @sql_col_out = ISNULL (@sql_col_out +"',"'," '") + QUOTENAME (['[Email protected]+']) from ['[Email protected]+'] GROUP by ['[Email protected]+']'--PRINT @sql_strEXEC sp_executesql @sql_str, N'@sql_col_out NVARCHAR (MAX) OUTPUT', @sql_col_out =@sql_col OUTPUT--PRINT @sql_colSET @sql_str= N'SELECT *From (SELECT ['[Email protected]+'],['[Email protected]+'],['[Email protected]+'] FROM ['[Email protected]+']) P PIVOT (SUM (['[Email protected]+']) for ['[Email protected]+'] In ('+ @sql_col +') as Pvt ORDER by Pvt. ['[Email protected]+']'--PRINT (@sql_str) EXEC (@sql_str)
SQL Server row to column