SQLTeam is a MS SQL Server forum so you will probably be better off checking on a MySQL site.On saying that, I do not imagine the syntax for UNPIVOT will be much different than MS SQL for later versions of MySQL.In MS SQL server you would do something like:-- *** Test Data ***CREATE TABLE #t( [weight] varchar(20) NOT NULL ,[type] varchar(20) NOT NULL ,factory1 decimal(10,2) NOT NULL ,factory2 decimal(10,2) NOT NULL ,factory3 decimal(10,2) NOT NULL ,factory4 decimal(10,2) NOT NULL);INSERT INTO #tSELECT '1kg', 'goods', 5.00, 5.50, 5.20, 5.00UNION ALL SELECT '2kg', 'goods', 6.00, 6.20, 6.15, 6.30UNION ALL SELECT '3kg', 'goods', 4.00, 4.50, 5.00, 4.30;-- *** End Test Data ***SELECT producer, [type], [weight], priceFROM( SELECT * FROM #t) SUNPIVOT( price FOR producer IN ( factory1, factory2, factory3, factory4 --..., factory150 )) UORDER BY producer, [type], [weight];
If your version of MySQL does not support unpivot then the same effect can be produced by use of a number/tally table and the CASE statement.-- *** Extra Test Data ***CREATE TABLE #n( n int NOT NULL);INSERT INTO #nSELECT 1UNION ALL SELECT 2UNION ALL SELECT 3UNION ALL SELECT 4;-- ...-- UNION ALL SELECT 150;-- *** End Extra Test Data ***SELECT CASE N.n WHEN 1 THEN 'factory1' WHEN 2 THEN 'factory2' WHEN 3 THEN 'factory3' WHEN 4 THEN 'factory4' --... --WHEN 150 THEN 'factory150' END AS producer ,[type], [weight] ,CASE N.n WHEN 1 THEN factory1 WHEN 2 THEN factory2 WHEN 3 THEN factory3 WHEN 4 THEN factory4 --... --WHEN 150 THEN factory150 END AS priceFROM #t T, #n NORDER BY producer, [type], [weight];