Retrieves the last record of each group

Like the title, retrieve the last record of each group.

For example, prepare some data first:

Share a picture

share picture Source Code

Requirements Group by [Category] and check the record with the largest data in [Qty].

CTE query grouping table:

share picture

SELECT data:

share picture

As long as I get the record with row number 1, it is enough, because in CTE processing, I have already done grouping and sorting by [Category].

share picture

< img alt="Share a picture" id="code_img_closed_f30a2356-1e96-4a8d-9616-c291f4c28343" class="code_img_closed" src="/wp-content/uploads/images/architecture/search-engines/1626814062251.gif"> Source Code

Leave a Comment

Your email address will not be published.