This article introduces how to convert the data in the table to the format of the category perspective, where the vacancy is represented as a NULL or empty string. This can be implemented by dynamic SQL Pivot query.
The following steps outline the process of creating a dynamic pivot query:
MAX()
Perform dynamic pivot queries to generate the result after the conversion. <code class="language-sql">DECLARE @cols AS NVARCHAR(MAX), @query AS NVARCHAR(MAX); SET @cols = STUFF((SELECT distinct ',' + QUOTENAME(c.category) FROM temp c FOR XML PATH(''), TYPE ).value('.', 'NVARCHAR(MAX)') ,1,1,'') set @query = 'SELECT date, ' + @cols + ' from ( select date , amount , category from temp ) x pivot ( max(amount) for category in (' + @cols + ') ) p ' execute(@query)</code>
The above is the detailed content of How to Use Dynamic SQL PIVOT for Data Transformation with NULL or Empty String Handling?. For more information, please follow other related articles on the PHP Chinese website!