Bom dia,
Estou com a missão de realizar uma query que busca Orçado x Realizado x Saldo conforme a tarefa do projeto e período:
A questão é, existe alguma tabela de histórico de orçamento?
Como está a consulta hoje:
select
mtarefa.idprj [identificador do projeto],
mtarefa.codtrf [código da tarefa],
mtarefa.descricao [descrição da tarefa],
mtarefa.idpai,
format(isnull(mtarefa.custototal, 0), 'n', 'pt-br') as 'orçado',
format(isnull((select sum(titmmovratccu.valor)
from titmmovratccu
left join titmmov on titmmovratccu.codcoligada = titmmov.codcoligada
and titmmovratccu.idmov = titmmov.idmov
and titmmovratccu.nseqitmmov = titmmov.nseqitmmov
left join tmov on titmmov.codcoligada = tmov.codcoligada
and titmmov.idmov = tmov.idmov
where titmmovratccu.codcoligada = mtarefa.codcoligada
and titmmovratccu.idprj = mtarefa.idprj
and titmmovratccu.idtrf = mtarefa.idtrf
and (tmov.codtmv in ('1.1.06', '1.1.28', '1.1.10', '1.1.59') or tmov.codtmv is null)
and tmov.datacriacao >= '20230101' and tmov.datacriacao <= '20240125'
/*and tmov.codtmv <> '1.1.08'*/), 0), 'n', 'pt-br') as 'realizado',
format((isnull(mtarefa.custototal, 0) - isnull((select sum(titmmovratccu.valor)
from titmmovratccu
left join titmmov on titmmovratccu.codcoligada = titmmov.codcoligada
and titmmovratccu.idmov = titmmov.idmov
and titmmovratccu.nseqitmmov = titmmov.nseqitmmov
left join tmov on titmmov.codcoligada = tmov.codcoligada
and titmmov.idmov = tmov.idmov
where titmmovratccu.codcoligada = mtarefa.codcoligada
and titmmovratccu.idprj = mtarefa.idprj
and titmmovratccu.idtrf = mtarefa.idtrf
and (tmov.codtmv in ('1.1.06', '1.1.28', '1.1.10', '1.1.59') or tmov.codtmv is null)
and tmov.datacriacao >= '20230101' and tmov.datacriacao <= '20240125'
/*and tmov.codtmv <> '1.1.08'*/), 0)), 'n', 'pt-br') as 'saldo'
from
mtarefa
join
mtrfcompl on mtarefa.codcoligada = mtrfcompl.codcoligada
and mtarefa.idprj = mtrfcompl.idprj
and mtarefa.idtrf = mtrfcompl.idtrf
where
mtarefa.idprj = 53
and mtarefa.tipoplanilha <> 1
group by
mtarefa.idprj,
mtarefa.codtrf,
mtarefa.descricao,
mtarefa.idtrf,
mtarefa.custototal,
mtarefa.codcoligada