Tag Info

New answers tagged

0

Change select @cols = STUFF((SELECT distinct ',' + QUOTENAME(DATENAME(MONTH, CONVERT(DATETIME, createDate))) FROM OSCL WHERE ([status] = - 3) FOR XML PATH(''), TYPE ).value('.', 'NVARCHAR(MAX)') , 1, 1, ''); to select @cols = ...


1

If I guessed the missing pieces correctly, this query using a crosstab() function should do the job (and much faster than the original - except for small cardinalities where not much can be gained to begin with): It requires that you install the additional module tablefunc first (once per database. Detailed instructions in this related answer on SO: ...


1

If you are interested in returning all the values as columns, you need to be try something very adventurous. First look at your query SELECT CONCAT(id, '-', date), MAX(IF(`num` = 0, avg, NULL)) num0 FROM table GROUP BY id, date; If would be a big mess to get MySQL to execute it as a query by giving MySQL the query with each column ...



Top 50 recent answers are included