Bring only one product per affiliate to sql server

Asked

Viewed 111 times

4

Good day I need to bring only one product per affiliate, but in my table I have several times the same product just changing the date, I would like to take the product of the last date.

What do I have:

SELECT ID, CODFIL, DT, COLUNAVARIANTE FROM X; 

Upshot:

ID  CODFIL    DT    COLUNAVARIANTE
1    1       XXXXX        X
1    1       YYYYY        Y
1    2       XXXXX        X
1    3       XXXXX

What I need

ID  CODFIL     DT   COLUNAVARIANTE
1    1       XXXXX       X
1    2       XXXXX       X
1    3       XXXXX       X

1 answer

1


The query below should bring you what you need:

SELECT ID, CODFIL, max(DT)
FROM X
GROUP BY ID, CODFIL

Thus, you will have only the longest date for each returned product.

  • Yes, indeed, forgive me for my mistake but I had not put the column variant in the select "SELECT ID, CODFIL, DT, COLUNAVARIANTE FROM X; " in this case put the max will not meet me

Browser other questions tagged

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