Use a query statement to retrieve the price and the maximum of five consecutive records.

Source: Internet
Author: User

The cause is caused by a problem on csdn. (See http://topic.csdn.net/u/20071006/18/b5c31f64-ff1f-4f55-9caf-f3c680a469c7.html? Seed = 305126211)

Question 3: There is an ordertable (price money, Id INT) with 50 records. The price data is irregular, A query statement is required to retrieve the price and the maximum of five consecutive records.
For example:
1 10.00
2 5.00
3 6.00
4 8.00
5 1.00
6 20.00
7 60.00
8 4.00
9 3.00
10 2.00
11 80.00
12 120.00
13 1.00

And the maximum of five consecutive records is
8 4.00
9 3.00
10 2.00
11 80.00
12 120.00

I have seen a lot of people do not understand the meaning of the questions upstairs, so I have made n more solutions. Of course, this question is also difficult for me. Later I found a friend of mine to help me find it. I didn't fully understand it at the moment. Remember to understand it later.

Please read the question clearly-use a query statement to retrieve the price and the maximum five consecutive records

This question should be said to be able to understand the SQL statement at a level. It is not just a simple SELECT statement. Some answers are as follows...

Select distinct t1.id, t2.id, t3.id, t4.id, t5.id, t1.price + t2.price + t3.price + t4.price + t5.price
From [select T1. *, T2. *, T3. *, T4. *, T5. * From ordertable T1, ordertable T2, ordertable T3, ordertable T4, ordertable T5, ordertable T6
Where t1.id <t2.id and t2.id <t3.id and t3.id <t4.id and t4.id <t5.id
And not exists (select ID from ordertable A where (A. ID> t1.id and A. ID <t2.id) or (A. ID> t2.id and A. ID <t3.id)
Or (A. ID> t3.id and A. ID <t4.id) or (A. ID> t4.id and A. ID <t5.id)
)]. As P;

Download Attachment

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.