This is not an answer, but the instruction how to make your query working.
1. Instead of this:
CONVERT(char(7), date, 120) As [year]
use:
YEAR([date]) As [year]
You don't need to convert date to string to get year. There's in-built function,
YEAR (Transact-SQL) - SQL Server | Microsoft Docs[
^]
BTW:
date
is ms sql server data type, so i'd suggest to put the field name between
[]
brackets.
2. Instead of this:
SUM(CONVERT(INT, CONVERT(VARCHAR(MAX),quantity)))
use:
SUM(quantity)
Double conversion from integer to varchar and back is redunant!
3. Instead of this:
WHERE CONVERT(char(7), date, 120) Between '2020-01' And '" 2020-07"'
use:
WHERE [date] Between '2020-01-01' And '2020-07-31'
If the [date] field is used to group data, you need to "convert it" into single date by using
DATEFROMPARTS[
^] or
DATEADD[
^] function.
DATEFROMPARTS(YEAR([date]), MONTH([date]), 1)
DATEADD(DD, - DAY([date]) +1, [date])
[EDIT]
If you would like to get distinct values as a comma separated list, check this out:
Converting row values in a table to a single concatenated string - SQLMatters[
^]
Good luck!