CREATE TABLE ProductSales (
Product VARCHAR(50), -- Name of the product (e.g., Laptop, Phone, Tablet)
Year INT, -- Year of sales (e.g., 2021, 2022)
Sales INT -- Sales amount (e.g., 500, 600)
);
INSERT INTO ProductSales (Product, Year, Sales)
VALUES
('Laptop', 2021, 500),
('Laptop', 2022, 600),
('Phone', 2021, 300),
('Phone', 2022, 400),
('Tablet', 2021, 200),
('Tablet', 2022, 300);
SELECT Product, [2021] AS Sales_2021, [2022] AS Sales_2022
FROM (
SELECT Product, Year, Sales
FROM ProductSales
) AS SourceTable
PIVOT (
SUM(Sales)
FOR Year IN ([2021], [2022])
) AS PivotTable;