我很难理解如何使用pivot运算符来转置表,但我无法理解。任何帮助都很好。这是我需要转置的查询。
SELECT
sum(delayCarrier>0) AS 'Carrier Delay',
sum(delayWeather>0) AS 'Weather Delay',
sum(delayTraffic>0) AS 'Air Traffic Delay',
sum(delaySecurity>0) AS 'Security Delay'
FROM flights
这是桌子的版本。
我需要它读一下:
'TypeOfDelay','Frequency'
-------------------------
Carrier Delay | 32093
Weather Delay | 4887
Air Traffic Delay | 40730
Security Delay | 215
抱歉,我知道这个问题被问了很多,我看过很多,但我似乎没有任何直觉。提前谢谢。
最佳答案
这是一个查询,它将提供您想要的输出,尽管我不能说它是一个轴心点,或者说它很漂亮。
SELECT 'Carrier Delay' AS TypeOfDelay, SUM(delayCarrier > 0) AS Frequency
FROM flights
UNION ALL
SELECT 'Weather Delay' AS TypeOfDelay, SUM(delayWeather > 0) AS Frequency
FROM flights
UNION ALL
SELECT 'Air Traffic Delay' AS TypeOfDelay, SUM(delayTraffic > 0) AS Frequency
FROM flights
UNION ALL
SELECT 'Security Delay' AS TypeOfDelay, SUM(delaySecurity > 0) AS Frequency
FROM flights
关于mysql - SQL中的简单数据透视表转换,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33481322/