The latest record of each group after the MSSQL group is taken

Source: Internet
Author: User
Tags mssql

Database in two tables, the user table and the Bonus record table, the bonus record table in a user has more than one message, there is an effective time, now to query:

Bonus is valid three days before each user takes the latest bonus record and the user is locked

The previous method was written directly in the C # code:

For (all end-locked users)

Query the bonus record table top 1 where uid=??? ORDER BY effective Time DESC

If (the record queried above takes effect three days ago)

Output

Today, I spent most of my time researching and finally finding a SQL statement to find out the results:

That row_number function in MSSQL2008 above will have!!!

The latest record of each group after the MSSQL group is taken

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.