Purpose | Used to aggregate rows that have the same values into summary rows, like “sum” or “average”. | Used to transform rows into columns, effectively rotating a table. |
Use Case | Suitable for simple aggregation based on one or more columns. | Ideal for converting unique row values into columns for better data comparison. |
Aggregation | Directly supports aggregation functions like SUM, AVG, MAX, MIN, COUNT. | Requires an aggregation function as part of the pivot operation to summarize values. |
Complexity | Generally simpler and more straightforward to use for aggregation. | More complex and used for advanced data transformation and presentation. |
SQL Syntax | SELECT column1,SUM(column2) FROM table GROUP BY column1 | SELECT * FROM (SELECT column1, column2, column3 FROM table) PIVOT (SUM(column3) FOR column2 IN ([value1], [value2], ...)) AS pivot_table |
Output Format | Produces a summary result set grouped by one or more columns. | Produces a wide format where each row is a unique combination from one column, and each column represents a unique value from another column, with summarized data at their intersection. |