2
DECLARE @Year INT = 2005, @Month INT = 7
SELECT --SequenceNo = ROW_NUMBER() OVER(ORDER BY OrderDate),
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),
[Running Total] = convert(money,
(SELECT sum(convert(money,TotalDue,1))
FROM Sales.SalesOrderHeader as Header
WHERE SalesOrderID <= soh.SalesOrderID
AND year(OrderDate) = @Year
AND month(OrderDate) = @Month),
1)
FROM Sales.SalesOrderHeader soh
WHERE year(OrderDate) = @Year
AND month(OrderDate) = @Month
group by Year, Month
order by 1, 2
When I try to run the group by appears:
Error Msg 207, Level 16, State 1, Line 31 Invalid column name 'Year'. Msg 207, Level 16, State 1, Line 31 Invalid column name 'Month'.
What is the version of SQL Server? There are more efficient ways to calculate running Totals, mainly from 2012 version.
– José Diz
the version is the 2012
– Joana Gonçalves
Was any of the answer helpful? Don’t forget to choose one and mark it so it can be used if someone has a similar question!
– Sorack