There is such a table

enter image description here

Actually we need the last entry in this table with Id2 = 1 and the last entry with Id2 = 2.

The problem is that there may be 3 and 4. And they will need to be included in the sample. I do not know in advance how much Id2 will be there.

I have a cycle option on my mind. Perhaps easier?

I can not guess here.

Id - primary key identifier.

MSSQL

  • the sorting order is known, the filtering criterion is known, how to select only one record, it should also be known how to reduce, what's the problem then? - teran
  • exactly. as an option, of course, the easiest. can be numbered by window functions and taken with the number 1 - teran
  • I apologize for the incomplete description of the problem, I added. - Tima
  • four
    SELECT MAX(id), id2 FROM [table] GROUP BY id2 ORDER BY 1 , no? - Yaant
  • 3
    Grouping by id2 and max (id), if I understood correctly. - msi

0