SQLize Online / PHPize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
create table tbl (Year int, Quarter int, Amount float); insert into tbl(Year, Quarter, Amount) values (1996, 1, 5279.89); insert into tbl(Year, Quarter, Amount) values (1996, 1, 5279.89); insert into tbl(Year, Quarter, Amount) values (1996, 2, 4879.19); insert into tbl(Year, Quarter, Amount) values (1996, 3, 6324.89); insert into tbl(Year, Quarter, Amount) values (1996, 4, 5227.22); insert into tbl(Year, Quarter, Amount) values (1997, 1, 6678.39); insert into tbl(Year, Quarter, Amount) values (1997, 2, 7234.54); insert into tbl(Year, Quarter, Amount) values (1997, 3, 7104.38); insert into tbl(Year, Quarter, Amount) values (1997, 4, 6930.17); insert into tbl(Year, Quarter, Amount) values (1998, 2, 7804.60); insert into tbl(Year, Quarter, Amount) values (1998, 2, 8046.60); insert into tbl(Year, Quarter, Amount) values (1998, 3, 8145.19); insert into tbl(Year, Quarter, Amount) values (1998, 4, 7909.80); insert into tbl(Year, Quarter, Amount) values (1999, 1, 9512.10); insert into tbl(Year, Quarter, Amount) values (1999, 2, 9893.70); insert into tbl(Year, Quarter, Amount) values (1999, 3, 8907.39); SELECT Year, SUM(CASE WHEN Quarter = 1 THEN Amount ELSE 0 END) AS Q1, SUM(CASE WHEN Quarter = 2 THEN Amount ELSE 0 END) AS Q2, SUM(CASE WHEN Quarter = 3 THEN Amount ELSE 0 END) AS Q3, SUM(CASE WHEN Quarter = 4 THEN Amount ELSE 0 END) AS Q4, SUM(Amount) AS Total FROM tbl GROUP BY Year;

Stuck with a problem? Got Error? Ask ChatGPT!

Copy Clear