Doubt SQL Working Days Query

Asked

Viewed 2,459 times

2

Good night!

Guys, I need to know a following result. I want to bring the working days grouped by month and year from April to June of this year 2017. Example:

Ano    Mês    Dias Uteis
2017  Abril      19
2017  Maio       18
2017  Junho      17

NOTE: There is already a function in our bank called dias_uteis that performs the calculation normally of the working days and holidays. I tried to do with the query below, however, unsuccessfully.

 select (dbo.dias_uteis('01-04-2017','30-06-2017')) diasuteis, 
 DATEPART(month, dbo.dias_uteis('01-04-2017','30-06-2017') ) Mês,
 DATEPART(YEAR, dbo.dias_uteis('01-04-2017','30-06-2017')) Ano 
 group by DATEPART(YEAR, dbo.dias_uteis('01-04-2017','30-06-2017')), 
 DATEPART(MONTH, dbo.dias_uteis('01-04-2017','30-06-2017'))
  • What does Function dias_uteis return ? The number of working days of the range ? Your problem and knowing in a month , create a version of this function for this , can use the one that already exists the work would be simple in thesis.

  • Hello, the function is only to bring the working days in any period. In this example I put from April until today.

  • @Renanbessa: What is the SQL Server version? // How are the parameters set for the dias_uteis function: date? char(10)? other?

3 answers

0

I didn’t quite understand that function you said you had, I tried to do it in the simplest way possible.

SELECT YEAR(dias_uteis) AS 'Ano',
MONTH(dias_uteis) AS 'Mes',
dias_uteis AS 'Dias Uteis'
FROM dbo
WHERE dias_uteis BETWEEN '01-04-2017' AND '30-06-2017'
GROUP BY MONTH(dias_uteis);
  • my friend, well.. this function is to bring the working days regardless of the period I choose.

  • The command which I presented in reply worked?

  • No, since dbo.dias_uteis is a function, it needs to receive some parameter. Example: select (dbo.dias_uteis('01-04-2017','30-06-2017')). This consultation he takes every useful day from April to today, but wanted to group them per month.

  • Try to do it the way I left it in the answer, though, with the parameters.

  • I did so as you said, however, it does not recognize this table dbo q vc mentioned. Saying that the dbo object name is invalid.

  • tá aqui a consulta já com os parâmetros.SELECT YEAR(dbo.dias_uteis('01-04-2017','30-06-2017')) AS 'Ano',
MONTH(dbo.dias_uteis('01-04-2017','30-06-2017')) AS 'Mes',
dbo.dias_uteis('01-04-2017','30-06-2017') AS 'Dias Uteis'
FROM dbo
WHERE dbo.dias_uteis('01-04-2017','30-06-2017') BETWEEN '01-04-2017' AND '30-06-2017' GROUP BY MONTH(DBO.dias_uteis('01-04-2017','30-06-2017'));

  • Take off the FROM dbo and see what returns. And second, what is the name of the table? It is not dbo?

  • This, having already done taking the from, however, occurs the following message: Each GROUP BY expression must contain at least one column that is not external reference.

  • SELECT YEAR(dbo.dias_uteis('01-04-2017','30-06-2017')) AS Ano,
MONTH(dbo.dias_uteis('01-04-2017','30-06-2017')) AS Mes,
dbo.dias_uteis('01-04-2017','30-06-2017') AS DiasUteis
WHERE dbo.dias_uteis('01-04-2017','30-06-2017') BETWEEN '01-04-2017' AND '30-06-2017' GROUP BY MONTH(DBO.dias_uteis('01-04-2017','30-06-2017'));

  • What is the table name?

Show 5 more comments

0

Select dbo.dias_uteis('01-04-2017','30-04-2017') abril,
       dbo.dias_uteis('01-05-2017','31-05-2017') maio,
       dbo.dias_uteis('01-06-2017','30-06-2017') junho

I believe this is it

0


On the issue of working days I suggest reading the article "Business day operations on SQL Server"that details ways to get the calculation, including with the optional use of calendar table.

Here is the code that receives as values the period to be issued. From these values the months are generated and calculated the number of working days for each monthly period.

-- código #1 v3
--> informe a data inicial e final (formato dd/mm/aaaa)
declare @DataInicial date, @DataFinal date;
set @DataInicial= convert(date, '1/4/2017', 103);
set @DataFinal= convert(date, '30/6/2017', 103);

-- garante sequência nos parâmetros
IF @DataInicial > @DataFinal
  goto _FIM;

--
with cteData as (
SELECT @DataInicial as InícioMês,
      case when eomonth(@DataInicial) > @DataFinal
           then @DataFinal 
           else eomonth(@DataInicial) end as FinalMês
union all
SELECT dateadd(day, +1, FinalMês), 
       case when eomonth(dateadd(day, +1, FinalMês)) > @DataFinal
            then @DataFinal 
            else eomonth(dateadd(day, +1, FinalMês)) end
  from cteData
  where FinalMês < @DataFinal
)
SELECT year(InícioMês) as Ano, month(InícioMês) as Mês,
       dbo.dias_uteis (InícioMês, FinalMês) as [Dias úteis]
  from cteData;

_FIM:
go

The code uses the function eomonth(), available as of 2012 version of SQL Server.

Version 3 of the code also works for cases where the review period does not involve full months. For example, you can search from 5/3/2017 to 20/6/2017 or even by 10/2/2017 to 20/2/2017.

  • my friend, I really appreciate it. It worked pretty. You’re the guy! I didn’t know this function eomonth.

  • @Renanbessa: Since the 2012 version of SQL Server several functions have been added. It is worth consulting the documentation of functions.

  • Done my dear.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.