Division between values - Query SQL Server 2012

Asked

Viewed 795 times

0

Guys, in the query below, is bringing the following way.

Ano  Mes  Horas  total_horas
2018  1    107    766 H
2018  1    225    766 H

Therefore, I want you to bring in another column the division of the column "Hours" by the column "total_hours", bringing this way.

Ano  Mes  Horas  total_horas    media
2018  1    107    766 H        0,139996
2018  1    225    766 H        0,293735

Follow the query below.

select
t1.Ano,t1.Mês,t1.horas,t2.total_horas
from
(select sum(f.C06) horas, DATEPART(month, TarFechamento) Mês, DATEPART(YEAR,     TarFechamento) Ano, FC.Valor Tipo 
 from  Tarefa t 
    INNER JOIN Usuario u on t.UsuIDResponsavel = u.UsuID and u.UsuIDGrupo = 7 
    INNER JOIN FRM_10 F ON F.TarefaID = T.TarID AND F.C04 IS NOT NULL
    INNER JOIN FormularioCampoValor FC ON FC.ValorID = F.C04
    where t.ProID in (9,3,12,7,10,4,5,2) AND T.TarTipID NOT IN (1048,1049,1050) and datepart(year,tarfechamento)>= 2018 and t.TarStatus = 9
 group by DATEPART(YEAR, TarFechamento),  datepart(month,TarFechamento),     FC.Valor)t1

left join

(select concat(cast(sum(f.C06) as varchar(100)),' ','H')  total_horas,     DATEPART(month, TarFechamento) Mês, DATEPART(YEAR, TarFechamento) Ano
 from  Tarefa t 
    INNER JOIN Usuario u on t.UsuIDResponsavel = u.UsuID and u.UsuIDGrupo = 7 
    INNER JOIN FRM_10 F ON F.TarefaID = T.TarID AND F.C04 IS NOT NULL
    INNER JOIN FormularioCampoValor FC ON FC.ValorID = F.C04
    where t.ProID in (9,3,12,7,10,4,5,2) AND T.TarTipID NOT IN (1048,1049,1050) and datepart(year,tarfechamento)>= 2018 and t.TarStatus = 9
    group by DATEPART(YEAR, TarFechamento),  datepart(month,TarFechamento))t2 on t1.Ano = t2.Ano and t1.Mês = t2.Mês

NOTE: I tried to perform in the external consultation above this way however, without success..

select
t1.Ano,t1.Mês,t1.horas / t2.total_horas
  • t1.hours and t2.total_hours are integer, time or other type?

  • @Arisantos, they are whole

  • tried in a bank I have for testing and the code select t1.Year,t1.Month,t1.hours / t2.total_hours should have worked which output when you use it?

  • select t1.Year,t1.Month,t1.hours,t2.total_hours,CAST(t1.hours AS FLOAT)/CAST(t2.total_hours AS FLOAT) AS MEDIA

1 answer

1


When you use

select t1.Ano,t1.Mês,t1.horas / t2.total_horas

by you.hours and t2.total_hours be integer we will have as answer integer values ie

Ano  Mes  Horas  total_horas    media
2018  1    107    766 H        0
2018  1    225    766 H        0

then divide as follows to get results in float

SELECT t1.Ano,t1.Mês,CAST(t1.horas AS float) / CAST(t2.total_horas AS float)

As total_hours have to be int in order to run the division substitute

concat(cast(sum(f.C06) as varchar(100)),' ','H')  total_horas

for

sum(f.C06) the total hours

and if you want the Concat to add it in select:

SELECT t1.Ano,t1.Mês,concat(cast(t2.total_horas as varchar(100)),' ','H')  total_horas,CAST(t1.horas AS float) / CAST(t2.total_horas AS float)
  • gave error. Message: "Error when converting data type varchar to float. Warning: the null value is deleted by an aggregation or other SET operation"

  • Which field gave error? it seems that one of them is varchar and not whole

  • Really your t2.total_hours is varchar and not integer: Concat(cast(sum(f.C06) as varchar(100),' ','H')

  • My suggestion is to take the Concat out of the subquery and run it in the main query only at the time of returning the results

  • It worked @Ari Santos, I removed the Concat and it worked. Thank you.

Browser other questions tagged

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