Go to account number, nickname Repeat, and take the latest binding record

Source: Internet
Author: User

The code is as follows:

Selectt1.* from(SELECT ut. Transactionaccount,u.nickname,p.professorname'bound teacher', UB. UpdateTime as'Change Time'From dbo. Userbind UB JOIN dbo. UserTransaction UT on UB. UserId=UT. Useridjoin dbo. Users u on UB. UserId=u.useridjoin dbo. Professor P on P.id=UB. Professoridwhere u.isenable=1and UB. UpdateTime<'2016-01-18') T1inner join (SELECT UT. Transactionaccount,u.nickname,max (UB. UpdateTime) as'Change Time'From dbo. Userbind UB JOIN dbo. UserTransaction UT on UB. UserId=UT. Useridjoin dbo. Users u on UB. UserId=u.useridjoin dbo. Professor P on P.id=UB. Professoridwhere u.isenable=1and UB. updatetime<'2016-01-18'GROUP by UT. Transactionaccount,u.nickname) ast2on t1. Transactionaccount=T2. Transactionaccountand t1. Time of Change=t2. Time of Change

Writing ideas:

Take the maximum time column when you have a time column
With a self-increment ID, go to the maximum ID.

You can do this if you want to return a recordset and have a time column.
Select repeating columns, time columns, remaining columns
Inner JOIN (select repeating column, max (Time column) as Time column from table group by repeating column) as T2
on T1. Repeating column =t2. Repeating column and T1. Time column =t2. Time column
From table as T1

Summarize:

The code snippet is repeated a lot, what good method do we have???

Go to account number, nickname Repeat, and take the latest binding record

Related Article

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.