Example 2: Quarterly Sales Summary
Suppose we want to recall quarterly sales data. So we can do this by having quarterly columns with aggregated sales income for each product.
SELECT
SUBSTR(month, 1, INSTR(month, ' ') - 1) AS quarter,
SUM(CASE WHEN product = 'Product A' THEN revenue END) AS 'Product A',
SUM(CASE WHEN product = 'Product B' THEN revenue ELSE NULL END) AS "Product B"
FROM sales
GROUP BY quarter;
Output:
Explanation: This query fetch the quarter from the “month” column and sums the revenue for “Product A” and “Product B” accordingly. The results are grouped by quarter, showing the total revenue for each product in that quarter.
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.