sqlserver
PIVOT Monthly Sales
A long-format sales table pivoted into one column per month. Lineage shows every Jan..Dec column tracing to the same amount source.
The lineage
The SQL
CREATE TABLE sales_long (
region NVARCHAR(50),
sale_month INT,
amount DECIMAL(12,2)
);
CREATE VIEW v_sales_wide AS
SELECT region, [1] AS jan, [2] AS feb, [3] AS mar, [4] AS apr,
[5] AS may, [6] AS jun, [7] AS jul, [8] AS aug,
[9] AS sep, [10] AS oct, [11] AS nov, [12] AS dec_
FROM (
SELECT region, sale_month, amount FROM sales_long
) src
PIVOT (
SUM(amount) FOR sale_month IN ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12])
) p;
What the lineage shows
- Every month column (jan..dec_) is sourced from sales_long.amount, partitioned by sale_month.
- region is a pure pass-through.
- PIVOT explodes one column into many — lineage shows the same source on every output.