I have a MySQL query that I would need to convert to SQL Server.
I tried with datepart
of SQL Server but it does not maintain the main feature of the MySQL query, which is to maintain the month-to-month range but also to take into account the training that extends for more than a month. >
Here is the SELECT used in MySQL:
SELECT DISTINCT
*
FROM
treinamentos,usuario_x_treinamento,usuario
WHERE
status_treinamento = 'REALIZADO'
AND
'201705' BETWEEN
EXTRACT(YEAR_MONTH FROM treinamentos.data_inicio_treinamento) AND
EXTRACT(YEAR_MONTH FROM treinamentos.data_fim_treinamento)
AND
usuario_x_treinamento.id_usuario = usuario.id_usuario
AND
usuario_x_treinamento.id_treinamento = treinamentos.id_treinamentos;
In SQL Server, something like:
SELECT count(DISTINCT usuario.id_usuario) as TREINADO,
FROM treinamentos, usuario_x_treinamento, usuario
WHERE
datepart(YEAR, treinamentos.data_inicio_treinamento) = '2017'
and datepart(YEAR, treinamentos.data_fim_treinamento) = '2017'
AND DATEPART(MONTH, treinamentos.data_inicio_treinamento) = '01'
and DATEPART(month, treinamentos.data_fim_treinamento) = '01'
AND usuario_x_treinamento.id_usuario = usuario.id_usuario
and status_treinamento = 'REALIZADO'
AND usuario_x_treinamento.id_treinamento = treinamentos.id_treinamentos;
But as I had already said, this select is only returning me training that started and ended in January 2017. I need to be counted for example training that started in October 2016 and have been finalized in August 2017.