I have a query, in it, I have a subquery, and I only want to execute the subquery always when the current code field does not repeat with the next.
I will show a sql pseudocode so that I understand what I want to do, especially in the CASE:
SELECT
@rownum:=@rownum+1 AS nro,
0 AS 'Encab',
pa.codigo,
pa.partida,
CASE
WHEN pa.codigo = pa.codigo siguiente THEN '' 'aqui deseo comparar el pa.codigo actual con el siguiente'
ELSE
(SELECT SUM(presupuestosa.valor + presupuestosa.valorm) FROM presupuestosa
WHERE presupuestosa.obra=18
AND presupuestosa.empresa=1 AND presupuestosa.codigo=pa.codigo)
END AS tvalor
FROM presupuestosa pa , (SELECT @rownum:=0) r WHERE pa.obra=18
AND pa.empresa=1 ORDER BY codigo ASC,Encab DESC,partida ASC
I have tried to make this solution by placing indicators to the repeated codes, but it has not worked for me. Thank you in advance.