0
DECLARE @Year int
DECLARE db_cursor CURSOR FOR
Select distinct Year(OrderDate) as Year
From Sales.SalesOrderHeader
Order by Year(OrderDate)
OPEN db_cursor FETCH NEXT FROM db_cursor INTO @Year
WHILE @@FETCH_STATUS = 0
BEGIN
--INSERT INTO xxxxx ("Year", "Month", "Total", "RunningTotal")
SELECT a.Year, a.Month, Sum(a.TotalDue) as Total, Sum(a.RunningTotal) as RunningTotal
From (
select Year = Year(convert(int,OrderDate,111)),
case Month(convert(int,OrderDate,111))
when 1 then 'Janeiro'
when 2 then 'Fevereiro'
when 3 then 'Março'
when 4 then 'Abril'
when 5 then 'Maio'
when 6 then 'Junho'
when 7 then 'Julho'
when 8 then 'Agosto'
when 9 then 'Setembro'
when 10 then 'Outubro'
when 11 then 'Novembro'
when 12 then 'Dezembro'
else 'unknown'
end as "Month1",
Month = Month(convert(int,OrderDate,111)),
TotalDue = convert(money,TotalDue,1),
RunningTotal = convert(money,
(SELECT sum(convert(money, TotalDue,1))
FROM Sales.SalesOrderHeader as Header
WHERE SalesOrderID <= soh.SalesOrderID
AND year(OrderDate) '2005' between '2008'
),
1)
FROM Sales.SalesOrderHeader soh
WHERE year(OrderDate) '2005' between '2008'
) a
group by a.Year, a.Month
order by 1, 2;
FETCH NEXT FROM db_cursor INTO @Year
END
CLOSE db_cursor
DEALLOCATE db_cursor
--End Cursor
Remove the
distinct
from the beginning of the query, I just made a question aboutGroup by e Distinct
together and I believe there is no use where the two are needed. I cannot understand why theCONVERT
within the functionYEAR
, the same for the FunctionMONTH
(Couldn’t put the date directly inside the function?) They are simple things, but they help in the performance.– Marconi
solved your problem?
– Marco Souza