Write a query that returns the sum of the sales by month and year – SQL

I'm stuck on getting the sales months to add together instead of show up as separate lines. Here's the question:

Write a query that returns the sum of the sales by month and year. There will be only two columns returned “Month Year” and “Sales”. Format the Sales with a dollar sign and two decimal places. List the sales amount high to low.

My Code:

SELECT date_format(salesdate, '%M,%Y') AS 'Month Year', CONCAT('$', salesamt) AS 'Sales' FROM sales GROUP BY invoiceid ORDER BY salesamt DESC;

I'm pretty sure the group by is the issue, but none of the other columns work. Any help would be greatly appreciated.

Table: 
                                 
CREATE TABLE sales (
  invoiceid INT PRIMARY KEY, 
  depid INT, 
  salesamt DECIMAL(10,2), 
  salesdate DATETIME;

Values: 

insert into sales values (101, 2, 2111.02, '20160102');
insert into sales values (102, 2, 421.00, '20160202');
insert into sales values (103, 2, 675.00, '20160202');
insert into sales values (104, 2, 4355.00, '20160302');
insert into sales values (105, 2, 975.00, '20160304');
insert into sales values (106, 2, 1021.00, '20160402');
insert into sales values (107, 2, 2106.00, '20160425');
insert into sales values (108, 2, 2799.81, '20160501');
insert into sales values (109, 2, 4335.75, '20160502');
insert into sales values (110, 2, 12006.00, '20160521');
insert into sales values (111, 2, 5220.00, '20160602');
insert into sales values (112, 2, 7198.02, '20160618');
insert into sales values (113, 2, 4795.00, '20160625');
insert into sales values (114, 2, 5341.00, '20160706');
insert into sales values (115, 2, 5795.00, '20160718');
insert into sales values (116, 2, 6400.00, '20160725');
insert into sales values (117, 2, 14795.00, '20160812');
insert into sales values (118, 2, 43395.00, '20160825');
insert into sales values (119, 2, 47595.00, '20160914');
insert into sales values (120, 2, 46795.00, '20160930');
insert into sales values (121, 2, 6223.00, '20161010');
insert into sales values (122, 2, 7702.00, '20161012');
insert into sales values (123, 2, 11292.00, '20161107');
insert into sales values (124, 2, 33211.00, '20161126');
insert into sales values (125, 2, 16430.00, '20161206');
insert into sales values (126, 2, 87010.00, '20161221');
insert into sales values (127, 2, 2111.02, '20170102');
insert into sales values (128, 2, 421.00, '20170202');
insert into sales values (129, 2, 675.00, '20170202');
insert into sales values (130, 2, 4355.00, '20170302');
insert into sales values (131, 2, 975.00, '20170304');
insert into sales values (132, 2, 1021.00, '20170402');
insert into sales values (133, 2, 2106.00, '20170425');
insert into sales values (134, 2, 2799.81, '20170501');
insert into sales values (135, 2, 4335.75, '20170502');
insert into sales values (136, 2, 12006.00, '20170521');
insert into sales values (137, 2, 5220.00, '20170602');
insert into sales values (138, 2, 7198.02, '20170618');
insert into sales values (139, 2, 4795.00, '20170625');
insert into sales values (140, 2, 5341.00, '20170706');
insert into sales values (141, 2, 7004.00, '20170718');
insert into sales values (142, 2, 14991.00, '20170725');
insert into sales values (143, 2, 34076.00, '20170812');
insert into sales values (144, 2, 47950.00, '20170825');
insert into sales values (145, 2, 40795.00, '20170914');
insert into sales values (146, 2, 41795.00, '20170930');
insert into sales values (147, 2, 47295.00, '20171010');
insert into sales values (148, 2, 47395.00, '20171012');
insert into sales values (149, 2, 41795.00, '20171107');
insert into sales values (150, 2, 47895.00, '20161126');
insert into sales values (151, 2, 87666.00, '20161206');
insert into sales values (152, 2, 9401.00, '20161221');


Read more here: https://stackoverflow.com/questions/67377506/write-a-query-that-returns-the-sum-of-the-sales-by-month-and-year-sql

Content Attribution

This content was originally published by Mitch S. at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: