Relative Content

Tag Archive for sqlunpivot

Converting multiple columns to rows using UNPIVOT

I am a beginner SQL user. I wrote a query to extract several Fuel emissions columns and wrote a display message to show if it meets the requirements (Previous emission – Fuel Emission = Fuel consumption). This resulted in many columns based on the 8 different fuel types and Google suggested using UNPIVOT. I want to convert the many columns to just Fuel Type, Previous Emission, Fuel Emission, Fuel Consumption, and Message (which I call Miscellaneous here).

Convert multiple rows to column (unpivot)

I am attempting to convert table1 to the output shown in table two. Is there a way to use multiple unpivot to accomplish this without skewing the data. Not sure what I’m missing.