As I said, you need to select the months, and then select your data.
The months you can generate numerical sequences from 1 to 12, or go giving select Union.
Considering the following scenario:
create table tblLoteTESTE
(
id integer,
DataAgendada datetime,
);
insert into tblLoteTESTE values (1, '01/01/2018');
insert into tblLoteTESTE values (2, '02/01/2018');
insert into tblLoteTESTE values (3, '03/01/2018');
insert into tblLoteTESTE values (4, '04/01/2018');
insert into tblLoteTESTE values (5, '04/01/2018');
insert into tblLoteTESTE values (6, '06/01/2018');
insert into tblLoteTESTE values (7, '07/01/2018');
insert into tblLoteTESTE values (8, '07/01/2018');
insert into tblLoteTESTE values (9, '07/01/2018');
insert into tblLoteTESTE values (10, '10/01/2018');
insert into tblLoteTESTE values (11, '11/01/2018');
insert into tblLoteTESTE values (12, '12/01/2018');
insert into tblLoteTESTE values (13, '12/01/2018');
insert into tblLoteTESTE values (14, '10/01/2018');
insert into tblLoteTESTE values (15, '06/01/2018');
Your query should look like this:
SELECT
DATENAME(month, DATEADD(month, m.mes-1, CAST('2008-01-01' AS datetime))) as
mes_nome,
m.mes,
count(t.id) as qtd
FROM
(SELECT 1 AS mes
UNION SELECT 2
UNION SELECT 3
UNION SELECT 4
UNION SELECT 5
UNION SELECT 6
UNION SELECT 7
UNION SELECT 8
UNION SELECT 9
UNION SELECT 10
UNION SELECT 11
UNION SELECT 12 ) AS m
LEFT OUTER JOIN tblLoteTESTE t ON MONTH(t.DataAgendada) = m.mes and YEAR(t.DataAgendada) = 2018
group by m.mes
Upshot:
mes_nome mes qtd
January 1 1
February 2 1
March 3 1
April 4 2
May 5 0
June 6 2
July 7 3
August 8 0
September 9 0
October 10 2
November 11 1
December 12 2
Note that it did not enter the year, because it would have to generate the sequence of the years, with a cross Join in the months, to then count the records. Which seems unnecessary to me. This type of information is usually seen year by year, so just inform the desired year as filter.
I put in the Sqlfiddle
sorry but did not understand, they seem to me equal the results, taking out the value of line 11 blurred
– Ricardo Pontual
So this red smudge I made as an example a zero ! Rs EX:if you do not have registration in November show me 0 @Ricardopunctual
– Luciano Ramos
you have to give the select in the months... with left Join with your records. See: https://answall.com/a/300026/69359 may even be a duplicate question, except for sgdb which is different
– Rovann Linhalis
@Rovannlinhalis But my records are on a single table
– Luciano Ramos
See the answer to the other question, there is no table of months there too
– Rovann Linhalis
@Rovannlinhalis Forgive my ignorance, but I’m layman in the area I haven’t been able to solve yet.
– Luciano Ramos
@Rovannlinhalis got otherwise thank you.
– Luciano Ramos
See if it helps, Union with "zero" data, artificial sequence https://social.msdn.microsoft.com/Forums/sqlserver/pt-BR/99190dd6-17a7-4c70-8d6a-b30a8910b8ac/gerar-sequenceof datas?forum=520
– Motta