Leetcode Problem 1179. Reformat Department Table

1179. Reformat Department Table

Leetcode Solutions

GROUP BY with Conditional Aggregation

  1. Use GROUP BY to group the records by the id column.
  2. For each month from January to December, use the SUM aggregate function combined with an IF statement to select the revenue for the corresponding month.
  3. Alias each aggregated column with the respective month name followed by '_Revenue'.
  4. Execute the query to obtain the final pivot table with one row per id and one column for each month's revenue.

erDiagram
    Department {
        int id
        int revenue
        varchar month
        id month PK
    }

LEFT JOIN for Each Month

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...