我有一个简单的查询,它产生以下结果:
SELECT month,transporttype,count(transporttype) as loads from deliveries group by month,transporttype
我想将行转置为列。
我了解mysql没有枢轴功能,因此需要工会,但不能100%确定。
先谢谢您的帮助。
您可以使用这样的交叉表来做到这一点-
SELECT `year`, `month`, SUM(IF(`transporttype` = 'inbound', 1, 0)) AS `inbound`, SUM(IF(`transporttype` = 'LocalPMB', 1, 0)) AS `LocalPMB`, SUM(IF(`transporttype` = 'Long Distance', 1, 0)) AS `Long Distance`, SUM(IF(`transporttype` = 'shuttle', 1, 0)) AS `shuttle`, SUM(IF(`transporttype` = 'export', 1, 0)) AS `export`, SUM(IF(`transporttype` = 'Extrusions-LongDistance', 1, 0)) AS `Extrusions-LongDistance`, SUM(IF(`transporttype` = 'Extrusions-Shuttle', 1, 0)) AS `Extrusions-Shuttle` FROM `deliveries` GROUP BY `year`, `month`
另外,您应该将transporttype值移动到查找表中,并在该表中包含transporttype_id。