lundi 2 novembre 2015

Simple Pivot Table Transposition in SQL

I'm trying really hard to understand how to use the pivot operator to transpose a table but I can't figure it out. Any help would be great. Here is my query that I need to transpose.

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

And here is the table version. enter image description here

I need it to read:

'TypeOfDelay','Frequency'
-------------------------
Carrier Delay | 32093
Weather Delay | 4887
Air Traffic Delay | 40730
Security Delay | 215

Sorry I know this question has been asked a lot and I've looked at many of them but I just can't seem to gain any intuition. Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire