I'm new to sql server
and would like a help with an exercise I'm doing if possible.
I have a query that returns me the region of a reseller, the name of the reseller, the cnpj, the date of registration and and the amount of sending sales per month, I set up the query for the month of January, but the instead of giving a Ctrl + C and Ctrl + V I would like to create a function that would add the months to me, could anyone give a help?
Here's the query:
SELECT vc.vc AS [VC]
, r.ds_razaosocial AS [Revenda]
, r.ds_cnpj AS [Cnpj]
, CONVERT(VARCHAR, r.dt_cadastro, 103) AS [Data de cadastro]
, (
SELECT COUNT(*)
FROM enviovendas AS ei
WHERE ei.ds_cnpjrev = r.ds_cnpj
AND YEAR(ei.dt_envio) = 2018
AND MONTH(ei.dt_envio) = 1
) AS [Jan]
, (
SELECT COUNT(*)
FROM enviovendas AS ei
WHERE ei.ds_cnpjrev = r.ds_cnpj
AND YEAR(ei.dt_envio) = 2018
) AS [Total]
FROM revendas AS r
INNER JOIN usuarios AS u ON r.id_system_user = u.id_usuario
INNER JOIN vc_das_revendas AS vc ON r.ds_cnpj = vc.ds_cnpjrev
WHERE u.fl_ativo_s_n = 'S'
AND r.e_uma_revenda_fun = 1
AND EXISTS (
SELECT *
FROM enviovendas AS ee
WHERE ee.ds_cnpjrev = r.ds_cnpj
AND YEAR(ee.dt_envio) = 2018
)
ORDER BY vc.vc
, [Revenda]