0
I have an SQL query in the following format:
SELECT XXXX AS duracao,
calcula_horas_manutencao({data_inicio_f}, {data_final}, contrato.id) as horas_manutencao,
calcula_horas_sistema({data_inicio_f},{data_final}, contrato.id) as horas_sistema, [...]
I want the result of duracao
is the sum of horas_manutencao
and of horas_sistema
. Would it be possible for me to put the two together without having to repeat the function call?
Complete Query:
SELECT SUM(X.horas_manutencao + X.horas_sistema) AS duracao, meses meses, horas_manutencao horas_manutencao, horas_sistema horas_sistema,
Limite_horas Limite_horas, Nome Nome,
cliente_id cliente_id, Valor Valor, valor_real valor_real, diferenca diferenca
FROM (
SELECT contrato.id AS contrato,
calcula_meses_atendimentos({data_inicio_f} , {data_final}) AS meses,
calcula_horas_manutencao ({data_inicio_f},{data_final},contrato.id) AS horas_manutencao,
calcula_horas_sistema({data_inicio_f},{data_final} ,contrato.id) AS horas_sistema,
sec_to_time(time_to_sec(contrato.limite_horas) * calcula_meses_atendimentos({data_inicio_f} , {data_final})) AS Limite_horas,
cliente.nome Nome,
cliente.id cliente_id,
format_money(contrato.valor) Valor,
calcula_valor_real(contrato.id) AS valor_real,
(calcula_valor_real(contrato.id) - contrato.valor) AS diferenca
FROM sis_contratos contrato
LEFT JOIN sis_atendimentos atendimento ON contrato.id = atendimento.contrato
LEFT JOIN sis_clientes cliente ON contrato.cliente = cliente.id
LEFT JOIN sis_modulos modulos ON contrato.id = modulos.id
WHERE contrato.id <>24
AND contrato.status='Ativo Mensal' [ AND atendimento.data_inicio BETWEEN {data_inicio_f} AND {data_final} ]
GROUP BY contrato.id
ORDER BY cliente.id,
cliente.nome,
atendimento.data_inicio DESC
)
AS X
Assuming that the
[...]
is the rest of my SQL it would have to stay out of SUBSELECT right? I tried to doSELECT SUM(X.horas_manutencao + X.horas_sistema) AS duracao FROM (
 SELECT calcula_horas_manutencao('2018-01-01', '2018-11-05', contrato.id) as horas_manutencao,
 calcula_horas_sistema('2018-01-01','2018-11-05', contrato.id) as horas_sistema
) AS X, contrato.id AS contrato, [...]
and returned syntax error pointing to the next line after this code.– Christian Jorge
No, you have to stay inside the subselect
– maiconfriedel
This is exactly what I needed, only I had a while in the rest of the query, this seems to limit to a record right?
– Christian Jorge
puts the whole query to see...
– maiconfriedel
I put the full Query above, between
{ }
is variable.– Christian Jorge
Where’s the while you quoted?
– maiconfriedel
In vdd it is not limiting to a record, it is summing everything up, if you want to show more records, it must indicate which columns you want outside of subselect a at the end use a
group by
– maiconfriedel