-1

How can I select last 10 rows with unique string1 entries and order them by id? Rows has following structure:

id | string1 | string2 | ... | stringN

Using GROUP BY string1 along with ORDER BY id DESC result contains unique rows, but not from the tail of table.

Thanks

Vladimir Kishlaly
  • 1,872
  • 1
  • 16
  • 26

1 Answers1

1

You should use grouped ranking on string1 column order by ID column, then select all the top 10 records whose rank is 1, order by ID desc (if DESC ordering by ID gives you the tail of the table). If you need assistance in writing a group-ranking SQL for MySQL, you can refer to this. SQL Server provides the RANK() function out-of-the-box.

Community
  • 1
  • 1
Vikdor
  • 23,934
  • 10
  • 61
  • 84