I am build a report, the output should be like (example):
['11/2012', 5, 2, 0]
['10/2012', 7, 1, 0]
['09/2012', 2, 3, 0]
['08/2012', 0, 0, 1]
['07/2012', 0, 2, 1]
['06/2012', 2, 1, 0]
['05/2012', 3, 1, 1]
['04/2012', 2, 2, 1]
['03/2012', 5, 0, 0]
['02/2012', 7, 1, 0]
['01/2012', 2, 0, 0]
['12/2011', 3, 0, 1]
For the month part, I need to get the actual month and than list one year back. I have done this with this code:
<%
DataAtual_PeriodoInicial= Now
DataFinal_PeriodoInicial= DateAdd("m", -13, Now)
DO WHILE Datediff("m", DataAtual_PeriodoInicial, DataFinal_PeriodoInicial) <> 0
%>
['<% Response.Write(Right("0" & Month(DataAtual_PeriodoInicial), 2)) %>/<% Response.Write(Year(DataAtual_PeriodoInicial)) %>', X, Y, Z]<br>
<%
DataAtual_PeriodoInicial= DateAdd("m", -1, DataAtual_PeriodoInicial)
LOOP
%>
So this is generating:
['11/2012', X, Y, Z]
['10/2012', X, Y, Z]
['09/2012', X, Y, Z]
['08/2012', X, Y, Z]
['07/2012', X, Y, Z]
['06/2012', X, Y, Z]
['05/2012', X, Y, Z]
['04/2012', X, Y, Z]
['03/2012', X, Y, Z]
['02/2012', X, Y, Z]
['01/2012', X, Y, Z]
['12/2011', X, Y, Z]
Now I have to throw some numbers instead of X, Y, and Z. I have a table with data like:
11 2012 X
10 2012 X
10 2012 X
10 2012 X
10 2012 Y
10 2012 Y
10 2012 Z
10 2012 Z
10 2012 Z
9 2012 X
9 2012 X
9 2012 X
9 2012 Y
9 2012 Z
9 2012 Z
8 2012 X
8 2012 X
8 2012 Y
8 2012 Y
8 2012 Y
8 2012 Y
8 2012 Y
...
And I start with this select:
SELECT
datepart(month,SentDate) AS Mes,
datepart(year,SentDate) AS Ano,
EventType
FROM
SomeTable
ORDER BY
SentDate DESC
I tried some group by, but didn’t sucessed. Do I need some kind of Count? SUM? And how can i finish the come to have the output like the one in the top of this question?
Thanks!
Try this
Demo at http://sqlfiddle.com/#!3/93a11/23/0
You could also use a PIVOT table
Demo at http://sqlfiddle.com/#!3/93a11/16/0