I have a Serviços(Nro_Serv, Data, Cliente, Valor)
table and would like to get customers who did service with me in a year X (ex: 2011) and who also did in a year Y (ex: 2012). I tried to use COUNT(*)
and then GROUP BY Cliente HAVING COUNT(*) > 1
. But it's not giving real value.
Code of SQL
:
SELECT cliente.nmcliente, COUNT(*)
FROM ordemservico
INNER JOIN cliente
ON ordemservico.cdcliente = cliente.cdcliente
WHERE ordemservico.auxordemservico = '/12'
OR ordemservico.auxordemservico = '/13'
GROUP BY cliente.nmcliente
HAVING COUNT(*) > 1
The auxiliary field is a suffixed field of the year.
Editing
I changed SQL
and did the following:
SELECT cliente.nmcliente, ordemservico.auxordemservico, COUNT(*)
FROM ordemservico
INNER JOIN cliente
ON ordemservico.cdcliente = cliente.cdcliente
WHERE ordemservico.auxordemservico = '/12'
OR ordemservico.auxordemservico = '/13'
GROUP BY cliente.nmcliente, ordemservico.auxordemservico
HAVING COUNT(*) > 1
Now it clearly returns me everyone customers who have served in the two years. I need to first take out the ones that are not duplicated, because I care who did services in the two years. And after that apply a "Distinct".