DECLARE @MES INT;
DECLARE @TOTAL_GERAL DECIMAL(18, 2);
DECLARE CURSOR2 CURSOR FOR
SELECT
MES,
SUM(TOTAL) AS TOTAL_GERAL
FROM (
SELECT
MONTH(NF.DATA) AS MES,
NF.NUMERO AS NUMERO_NF,
SUM(INF.QUANTIDADE * INF.[PRECO]) AS TOTAL
FROM
[dbo].[ITENS NOTAS FISCAIS] INF
INNER JOIN
[dbo].[NOTAS FISCAIS] NF ON NF.NUMERO = INF.NUMERO
GROUP BY
MONTH(NF.DATA), NF.NUMERO
) AS SUBQUERY
GROUP BY
MES
ORDER BY
MES;
OPEN CURSOR2
FETCH NEXT FROM CURSOR2 INTO @MES, @TOTAL_GERAL
WHILE @@FETCH_STATUS = 0
BEGIN
PRINT 'Mês: ' + CAST(@MES AS VARCHAR) + ', Total Geral: ' + CAST(@TOTAL_GERAL AS VARCHAR);
FETCH NEXT FROM CURSOR2 INTO @MES, @TOTAL_GERAL
END
CLOSE CURSOR2
DEALLOCATE CURSOR2