Error running SUM - SQL Server

Asked

Viewed 767 times

0

Guys, I’m trying to perform the SUM according to the query below, however, the following error occurs. " It is not possible to perform an aggregation function on an expression that contains an aggregation or a sub-allowance." The output is to exit this way.

Total_Horas   MesID        Mes     Ano
60             1         Janeiro   2018

Consultation

select 
sum(dbo.FN_CALC_HORAS_UTEIS(s.SolData,min(l.LogData))),datepart(month,s.SolData) MesID,datename(month,s.soldata) Mes,datepart(year,s.soldata) Ano
from 
Solicitacao S
left join usuario U 
on (U.UsuID = S.UsuIDResponsavel) 
left join Status ST
on S.SolStatus = ST.CodStatus
left join Log L on L.LogSolID = s.SolID and (l.LOGDESCRICAO like '%1057%' or l.LOGDESCRICAO like '%3343%')
where S.proid in (2) and S.UsuIDResponsavel in(1776) and s.SolStatus <> 9 and convert(date,s.soldata) between '01-01-2018' and getdate()--and s.SolID = 65513
group by datepart(month,s.SolData),datename(month,s.soldata) ,datepart(year,s.soldata)

1 answer

0

Try this, with an external query:

select
    *,
    sum(horas_uteis) as soma_horas
from (
    select
        dbo.FN_CALC_HORAS_UTEIS(s.SolData,min(l.LogData)) as horas_uteis
        datepart(month,s.SolData) MesID,
        datename(month,s.soldata) Mes,
        datepart(year,s.soldata) Ano
    from Solicitacao S
        left join usuario U on (U.UsuID = S.UsuIDResponsavel) 
        left join Status ST on S.SolStatus = ST.CodStatus
        left join Log L on L.LogSolID = s.SolID and (l.LOGDESCRICAO like '%1057%' or l.LOGDESCRICAO like '%3343%')
    where
        S.proid in (2)
        and S.UsuIDResponsavel in (1776)
        and s.SolStatus <> 9
        and convert(date,s.soldata) between '01-01-2018' and getdate()--and s.SolID = 65513
) as X
group by MesID, Mes, Ano
  • Faustino, the following message occurred in his first query: "The 'Request.Weld' column is invalid in the selection list because it is not contained in an aggregation function or in the GROUP BY clause." . And in the second.

  • I think we are close then! Soldata is at GROUP BY! So we have to pass to FUNNTION the whole date!

  • Just one more question! The function receives what in the first parameter?

  • I edited the answer, try without the line of the first GROUP BY

  • the function receives the working hours between a certain period, in which case it is between the opening date (s. weld) and the minimum log date(l. logdata).

Browser other questions tagged

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