T-SQL - How to get one row on a table witjh multiple records

  • Hi to all.

    I have this trouble.

    I have a table called TBLCataloghi

    I have multiple records with colunms codpro and codcat equal

    They differ only by a date called catalog.datfin

    I'd like to select all rows but with the same codpro,codcat, obtaining ONLY the row with MIN () field datfin

    Field datfin is a date..

    Ex. codpro = 'PIPPO'

    codcat = 'MK'

    DATFIN = 01/01/2010

    codpro = 'PIPPO'

    codcat = 'MK'

    DATFIN = 10/07/2014

    I'd like to read both records but in SELECT obtain only the record with datfin MIN (01-10-2010)

    I did the query but i was not able to do nothing of good.

    I obtain all times both records...

    SELECT catalog.codpro AS CodProdotto,

    catalog.codcat AS CodiceCatalogo,

    MIN(catalog.datfin)

    FROM pub.catalog

    WHERE catalog.codcat = 'MK'

    GROUP BY catalog.codpro,catalog.codcat ,catalog.datfin

    I hope in your good help.

    Regards to all.

  • Remove ",catalog.datfin" from GROUP BY.

    If you include it in the group by, you'll getting the minimum value for each date and that will be each date.

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • Thanks for your kind reply.

    Yes i solved my problem.

    It works...

    Many thanks.:-)

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply