This happens because the grouping is given by all the columns of the GROUP BY and consequently as the column CurrencyRateID
has 4 different values for the value AUD
of the column ToCurrencyCode
, it is separated in these 4 "sub groups"
You simply need to remove the% column from% from the grouping
SELECT cr.ToCurrencyCode,
SUM(TotalDue) as [cantidad facturada],
MONTH(OrderDate) as mes
FROM sales.SalesOrderHeader sh
JOIN sales.CurrencyRate cr
ON sh.CurrencyRateID = cr.CurrencyRateID
GROUP BY cr.ToCurrencyCode, MONTH(OrderDate)
If you also want to delete the month column, you can do two things.
One, delete CurrencyRateID
of GROUP BY and SELECT, so you will have the total sum of all months.
The other is filtering for a specific month and you will have the total sum of a given month; This is optional, removing the row from WHERE alanza to get the historical total.
SELECT cr.ToCurrencyCode,
SUM(TotalDue) as [cantidad facturada]
FROM sales.SalesOrderHeader sh
JOIN sales.CurrencyRate cr
ON sh.CurrencyRateID = cr.CurrencyRateID
WHERE MONTH(OderDate) = 7
GROUP BY cr.ToCurrencyCode