Example 3: Monthly Sales Trends
Suppose we want to represent the monthly sales trends for each product. The transposing of the data will have each month as a column but sales revenue of each product across the columns.
SELECT
product,
MAX(CASE WHEN month = 'January' THEN revenue END) AS "January",
MAX(CASE WHEN month = 'February' THEN revenue END) AS "February",
MAX(CASE WHEN month='March' THEN revenue END) AS "March"
FROM sales
GROUP BY product
Output:
Explanation: This query uses the CASE statement inside the MAX function to pivot data from rows to columns, showing the maximum revenue for each product (A, B) in each month (January, February, March). The results are grouped by product, displaying the maximum revenue for each month in separate columns.
How to Ttranspose Rows to Columns in SQLite?
Transposing rows to columns in SQLite involves converting data from a row-based format to a column-based format. This operation can be useful for pivoting data, and transforming rows into columns for better analysis or reporting. SQLite does not have a built-in PIVOT function like some other databases but can achieve this result using SQL queries.
In this article, We will learn about How to Transpose Rows to Columns in SQLite by understanding various queries with the help of examples and so on.
Contact Us