Bring only one product per branch on the sql server [duplicate]

4

Good morning I need to bring only one product per branch, however in my table I have several times the same product just changing the date, I would like to get the product from the last date.

What I have:

SELECT ID, CODFIL, DT, COLUNAVARIANTE FROM X; 

Result:

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
    
asked by anonymous 06.06.2018 / 15:15

1 answer

1

The query below should bring you what you need:

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

So you have only the longest date for each product returned.

    
06.06.2018 / 15:30