SQL Server dynamic PIVOT query?

Dynamic SQL PIVOT: create table temp ( date datetime, category varchar(3), amount money ) insert into temp values (‘1/1/2012’, ‘ABC’, 1000.00) insert into temp values (‘2/1/2012’, ‘DEF’, 500.00) insert into temp values (‘2/1/2012’, ‘GHI’, 800.00) insert into temp values (‘2/10/2012’, ‘DEF’, 700.00) insert into temp values (‘3/1/2012’, ‘ABC’, 1100.00) DECLARE @cols AS NVARCHAR(MAX), @query AS … Read more

How can I return pivot table output in MySQL?

This basically is a pivot table. A nice tutorial on how to achieve this can be found here: http://www.artfulsoftware.com/infotree/qrytip.php?id=78 I advise reading this post and adapt this solution to your needs. Update After the link above is currently not available any longer I feel obliged to provide some additional information for all of you searching … Read more

SQL query rows as columns [duplicate]

Test Data DECLARE @Table TABLE(RecID INT,Name VARCHAR(20),Value VARCHAR(20)) INSERT INTO @Table VALUES (1,’Color’ ,’Red’), (2,’Size’ ,’Small’), (3,’Weight’,’20lbs’), (4,’Shape’ ,’Square’) Query SELECT * FROM (SELECT Name,Value FROM @Table) T PIVOT ( MAX(Value) FOR Name IN ([Color],[Size],[Weight],[Shape]) )P Result Set ╔═══════╦═══════╦════════╦════════╗ ║ Color ║ Size ║ Weight ║ Shape ║ ╠═══════╬═══════╬════════╬════════╣ ║ Red ║ Small ║ 20lbs … Read more