sql query showing a repeated record

0

I have the following query that I do in SQL server

select celular, razon_social FROM clientes where 
celular <> '' AND LEN(celular) = '10' 
AND ind_estado = '1' AND ISNUMERIC(celular) > 0 
ORDER BY celular

it brings me records normally, what happens is that there are some records that are repeated in the field celular but not in the field razon_social

for example these records

As you can see in the image there are 5 records: the record 13 and 14 have the same number celular but different razon_social , the records 15 , 16 and 17 have the same number celular but different razon_social .

What do I want?

that instead of bringing me those 5 records, bring me 2, I mean that of the repeated ones, it only brings 1 no matter which of the repeated ones I take, what interests me is that it brings me all records except if it is repeated on the cell phone, if it is repeated on the cell phone, it only brings me 1 of the repeated ones, but I do not want it to be excluded.

try modifying the query like this

select celular, razon_social FROM clientes where 
celular <> '' AND LEN(celular) = '10' 
AND ind_estado = '1' AND ISNUMERIC(celular) > 0 
AND celular in (SELECT celular from clientes GROUP 
BY celular HAVING count(*)=1) 
ORDER BY celular

but I exclude the repeated ones

It should be said that there are many other records that are repeated 2, 3 or even more times in the cell, which I put are just some examples.

    
asked by The_pacha 23.10.2018 в 23:45
source

1 answer

0

If you really do not care about the "business name", consider using a sub-query.

In this case, I assume that your "customers" table has a column called "CustomerID".

Example:

SELECT CLI.celular, (SELECT TOP 1 SUBCL.razon_social FROM clientes AS SUBCL WHERE SUBCL.IdCliente = CLI.IdCliente)
FROM clientes AS CLI
WHERE CLI.celular <> '' AND LEN(CLI.celular) = '10' 
AND CLI.ind_estado = '1' AND ISNUMERIC(CLI.celular) > 0 
ORDER BY CLI.celular
    
answered by 23.10.2018 в 23:53