Hi! Could we please enable some services and cookies to improve your experience and our website?

SQLize | PHPize | SQLtest

Online Sandbox for SQL and PHP: Write, Run, Test, and Share SQL Queries and PHP Code

A A A
Login    Share code      Blog   FAQ
Copy Format Clear
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;

Stuck with a problem? Got Error? Ask AI support!

Copy Clear