SQL nested Query

Source: Internet
Author: User
Tags time and date

 

A problem that has been resolved for a long time today,

The cause is
Obtain the data with the maximum time and date.

Previously, we thought that only the MSX function could solve the problem,

Select * From tablename where date = (select max (date) from tablename) and time = (select max (time) from tablename)

 

During the test, we found that a piece of data was taken out. Because the test was performed on two pieces of data in a day without a different date, we thought it was correct on that day. However, the data was written the next day, if you want to retrieve the data, but find that there is no data, and return NULL rows, you think it is all code and there is a problem. You haven't found the data for half a day. You have taken a closer look at the code in the stored procedure, the returned data is indeed empty.

This is a nested query statement. The statement of external query is executed first.

 

For example, there are three pieces of information. Use the statement written above to execute it in the SQL analyzer.

 

 

 

 

Analyze such a queryThe first query is the date, and the maximum date is the following two statements. In the comparison time. It is found that the maximum time is only one

Number of entriesSo that the second data is taken out of course. This is the test result at the time.

 

 

 

But then I modified the data. The next day, the test showed that the data was empty. No data.The analysis is like this.

The maximum number of days queried is. The third line. The most time is 21:12:21.
Is the second data entry.

In this way, there is no intersection with the result. It is null.

 

 

 

 

Later, I searched for textbooks and asked others. The problem is solved,The sorting method is used.

Order by DESC is sorted in descending order, which can be overlapped in different ways.

If the preceding statement is corrected as follows, the result is expected.

 

Select top 1 * From t_checkbill order by date DESC, time DESC

 

 

 

The above are your findings. Communication is necessary, or your own ideas are limited.

 

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.