I have 2 tables that stores different sales type, and I need to add everything to get the value to generate the collection, but I'm not able to mount the sql query, as follows:
SELECT CL.NOME,CL.id_cadastro_cliente, sum (L.VALOR_LANCAMENTO) as valor_total
from (
(SELECT CL.NOME,CL.id_cadastro_cliente, sum (L.VALOR_LANCAMENTO) as valor_total
FROM CADASTRO_ATENDIMENTO AS C
INNER JOIN CADASTRO_ATENDIMENTO_ITEM AS L ON (C.id_cadastro_atendimento = L.id_cadastro_atendimento)
INNER JOIN CADASTRO_Cliente AS CL ON (CL.id_cadastro_cliente = C.id_cadastro_cliente)
Where C.TIPO_OPERACAO = 'A VISTA' AND L.FATURA_NUMERO = 0 AND L.FATURA_ANO = 0 AND L.FATURA_PAGA = 'FALSE' AND C.REMETENTE LIKE '%'
AND C.DATA_ATENDIMENTO BETWEEN '2014-01-01' and '2016-01-01'
group by CL.id_cadastro_cliente
HAVING SUM(L.VALOR_LANCAMENTO) > 100
ORDER BY CL.NOME)
union all
(SELECT CL.nOME, CL.id_cadastro_cliente, sum (contador_final - contador_inicial) as valor_total
FROM cadastro_maquina_franquia_atendimento AS M
INNER JOIN CADASTRO_Cliente AS CL ON (CL.id_cadastro_cliente = M.id_cliente)
WHERE CARTAO IS NULL AND ID_CLIENTE > 0
group by CL.id_cadastro_cliente)
) as CONSULTA
INNER JOIN CADASTRO_Cliente AS CL
group by CL.id_cadastro_cliente
I'm having the following error:
ERROR: syntax error at or near "group" LINE 30: group by CL.id_cadastro_cliente