0
I have a Mysql query that would need to convert to SQL Server.
I tried with datepart
SQL Server but it does not maintain the main feature of the Mysql query, which is to maintain the range month by month but also take into account the trainings that extend for more than a month.
Follow 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 has remained so far 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 training that began and ended in January 2017. I need to be counted for example training that began in October 2016 and have been completed in August 2017.
You could post an example of the data you are trying to use the SQL commands, because then it is clearer to understand how the result was and compare with the result of sqlserver. If you can use a tool like sqlfiddle.com, it’s easier for the community to help you out.
– Camilo Santos