SUBSELECT WITH COUNT(*) SQL

Asked

Viewed 120 times

0

SELECT CPF FROM ZPCF WHERE CPF IN
     (SELECT TELEFONE, COUNT(*) FROM ZCPF WHERE  
          GROUP BY TELEFONE HAVING COUNT(*) > 1)

I need you to bring Cpf along with phone numbers, there are phones registered in the same numbers, the subselect works the other says it has many values and did not find a way to do.

  • Here ... WHERE CPF IN (SELECT TELEFONE, COUNT() ... there is no field CPF´ na lista de campos do subselect. Creio que seu subselect deva retornar apenas uma lista de CPFs. Estude a cláusula IN`.

  • which database you are using and which version?

1 answer

1

The error of your query is related to the fact that the subquery is returning more than one column. For, the main query filter is just waiting for a set of cpfs. Don’t make the mistake you should do the following:

SELECT CPF FROM ZPCF 
WHERE CPF IN (SELECT CPF FROM ZCPF WHERE
GROUP BY CPF  HAVING COUNT() > 1)

However, by the description of what you expect I imagined that the correct query would be the following:

SELECT DISTINCT CPF,TELEFONE FROM ZPCF 

This query will return all phones related to a particular Cpf without duplicities.

EX:


GGG 11111
AAA 2222
GGG 11111
AAA 333333

The query return would be as follows:

GGG 11111
AAA 2222
AAA 333333

Browser other questions tagged

You are not signed in. Login or sign up in order to post.