0
I am wanting to perform the calculation in sql query where the result is the number of open calls divided by the number of working days chosen period. Ex: They were opened 200 calls in the month and there are 17 working days in the month of May, then you will have to realize that average.
NOTE: There is already a working day function listed below:
USE [0800net_PRD]
GO
/****** Object: UserDefinedFunction [dbo].[dias_uteis] Script Date: 30/06/2017 11:06:51 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER FUNCTION [dbo].[dias_uteis] (@DATA_INICIAL DATETIME, @DATA_FINAL DATETIME)
RETURNS INT
AS
BEGIN
DECLARE @CONT INT
SET @DATA_INICIAL = (SELECT CAST(@DATA_INICIAL AS DATE) )
SET @DATA_FINAL = (SELECT CAST(@DATA_FINAL AS DATE ))
SET @CONT = 0
WHILE (@DATA_INICIAL <= @DATA_FINAL)
BEGIN
IF (DATEPART(DW, @DATA_INICIAL ) IN(1,7) OR @DATA_INICIAL IN (SELECT CAST(FerData AS DATE) FROM Feriado))
BEGIN
SET @DATA_INICIAL = @DATA_INICIAL+1
END
IF DATEPART(DW, @DATA_INICIAL ) NOT IN(1,7)
BEGIN
SET @CONT = @CONT+1
SET @DATA_INICIAL = @DATA_INICIAL+1
END
END
RETURN @CONT
END
Below is the query I’m trying to accomplish this.
select
s1.Ano ,s1.Mês , CASE s1.Mês
WHEN 1 THEN 'Janeiro'
WHEN 2 THEN 'Fevereiro'
WHEN 3 THEN 'Março'
WHEN 4 THEN 'Abril'
WHEN 5 THEN 'Maio'
WHEN 6 THEN 'Junho'
WHEN 7 THEN 'Julho'
WHEN 8 THEN 'Agosto'
WHEN 9 THEN 'Setembro'
WHEN 10 THEN 'Outubro'
WHEN 11 THEN 'Novembro'
WHEN 12 THEN 'Dezembro'
END AS Mês_Extenso, s1.[Média de Abertura Diária], s2.fechamento
from
(select COUNT(SolData)/(SUM(dbo.dias_uteis('01/04/2017', '26/06/2017') + SUM(dbo.dias_uteis('01/04/2017', '26/06/2017'))))[Média de Abertura Diária],
DATEPART(month, SolData) Mês, DATEPART(YEAR, SolData) Ano
from Solicitacao S inner join usuario U on U.UsuID = S.UsuIDResponsavel and S.UsuIDResponsavel = 29
where S.ProID = 2 and CONVERT(DATE,SolData) BETWEEN '01/04/2017' and '26/06/2017' group by DATEPART(YEAR, SolData), DATEPART(month,SolData)) s1
inner join
(select COUNT(SolDataFechamento) Fechamento, DATEPART (month, SolDataFechamento) Mês, DATEPART(YEAR, SolDataFechamento) Ano
from Solicitacao S inner join usuario U on U.UsuID = S.UsuIDResponsavel and S.UsuIDResponsavel = 29
where S.ProID = 2 and CONVERT(DATE, SolDataFechamento) BETWEEN '01/04/2017' and '26/06/2017' group by DATEPART(YEAR, SolDataFechamento), DATEPART(MONTH, SolDataFechamento)) s2
on s1.Mês=s2.Mês and s1.Ano = s2.Ano
order by
s1.Ano, s1.Mês
OBS: When executing also gives an error message "It is not possible to execute an aggregation function in an expression that contains an aggregation or a sub-query."
Put the table structure please, and the calculation should be in your result, I say in your select and not in subselect, no ?!
– Rovann Linhalis
I’ve tried to do it without the sub-consultations, but it makes the same mistake.
– Renan Bessa