Use DISTINCT SQL Server clause

Asked

Viewed 688 times

0

Hi, I need some help ! I need to do a query and bring in the Nifs without repeating. I do it like this:

SELECT DISTINCT NIF FROM CustomerRegistrations

But I also want to bring more columns from this table. But I can’t do that:

SELECT DISTINCT NIF, Name, RegistrationDate FROM CustomerRegistrations

Because it will bring repeated NIF’s. I mean, there are NIF’s with different names, but I just want to bring one. Thanks to those who help !

  • 1

    If any of the other columns contain different data they will have to repeat even because the data is different comparing the row.

  • @Marconi because I understand, I wanted to know if there was possibility to do what I was asking but apparently not.

  • If you add the structure of your table with some data and explain what you want to leave in the result try to help in a solution @DC =D

  • 1

    @Marconi I have found another solution, thank you very much for the availability !!

  • @DC, share this solution as an answer with us

1 answer

1

select distinct in temporal table sql server

It’s basically the same problem I had: the author of that code is @joséDiz, I’ll modify it to meet your demand:

WITH Sinc_2
AS (SELECT*,
        seq = ROW_NUMBER() OVER (PARTITION BY NIF ORDER BY NIF)
        FROM CustomerRegistrations)
SELECT nomerepresentante, codigorepresentante, datasinc
FROM Sinc_2
WHERE seq = 1;

The more columns you put in PARTITION BY, less distinction he will make

Browser other questions tagged

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