SQL statement-Sort by adding sequence number and then calculating the desired item

Source: Internet
Author: User

Select a.ID as aid,b.id as bid,a.city,a.cang,a.sid,a.time as atime,b.time as btime,a.price as aprice,b.price as BPRICE,A.P M as apm,b.pm as BPM from (select Id,city,cang,sid,time,price, @rank: [e-mail protected]+1 as PM from Cai, (select @rank: =0) B GROUP BY Id,city,cang,time Order by City,cang,sid,time) as a, (select Id,city,cang,sid,time,price, @ranks: [Email protect Ed]+1 as PM from Cai, (SELECT @ranks: =0) b GROUP by Id,city,cang,time ORDER by City,cang,sid,time) as B where A.sid=b.sid and A.cang = B.cang and a.city = b.city and A.id<b.id and ABS (A.PM-B.PM) =1 and (ABS (A.price-b.price)/a.price<0.2 or ABS (A.price-b.price)/b.price<0.2)

Results:

Data collation:

1. Insert serial number after sorting

http://blog.csdn.net/k8080880/article/details/11253305

2. Adjacent to do bad reference

Temperature difference between two days adjacent to http://bbs.csdn.net/topics/390887686

http://bbs.csdn.net/topics/300153858 two-day minimum difference between dates

Http://zhidao.baidu.com/link?url=ic00M2Rne5CSeB9tFBizT-o_ Jfvrjorgn-pcovel5syhrqqskpj-u5bsy70wj9hev3e4ube6ohhrslyir0nphwvn5vh43awgvotj2cqxfzo to solve a similar problem

3. The original issue

http://tieba.baidu.com/p/4200604602?pid=80233503055&cid=0#80233503055

SQL statement-Sort by adding sequence number and then calculating the desired item

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.