How do you pivot rows into columns in SQL?

SQL aggregates and GROUP BY · Freshers and experienced

Model answer

Use conditional aggregation: SUM(CASE WHEN month = 'Jan' THEN amount END) AS jan, and so on, grouped by the key. Some databases have PIVOT syntax, but conditional aggregation works almost everywhere.

Learn this topic

GROUP BY and HAVING: Summaries for Each Group in the course SQL for Beginners: Learn Databases Step by Step.

Sign in to add your own answer.

More questions