SELECTTOPN problem (2) _ MySQL

Source: Internet
Author: User
SELECTTOPN problem (2) Hi, everyone:

We will continue to discuss the select top n issue today.
1. DB2's M 2
As mentioned in the previous article, because DB2's SUBQUERY does not support the order by clause, it is impossible to construct M2. in fact, DB2 provides a new set of FUNCTIONS called olap functions, the top n issue can be well supported.
M2:
SELECT * FROM
(Select mykey, RANK () OVER (order by ORD_COL DESC) AS MYRANK
From mytable) AS RANKED_TABLE
Where myrank <= N

Using OLAP functions, in addition to the method mentioned in the previous article, the M1 of DB2 can be expressed:
SELECT * FROM
(Select mykey, ROW_NUMBER () OVER (order by ORD_COL DESC) AS MYRANK
From mytable) AS RANKED_TABLE
Where myrank <= N

2. apart from M1 and M2 top n, DB2 also called DENSE_RANK and (M3.
M1: TOP 3 -- 1 (4,101), 2 (5,101), 3 (1,100) or 1 (4,101), 2 (5,101), 3 (2,100)
M2: TOP 3 -- 1 (4,101), 1 (5,101), 3 (1,100), 3 (2,100)
M3: TOP 3 -- 1 (4,101), 1 (5,101), 2 (1,100), 2 (2,100), 3)
In DB2, M3 is implemented as follows:
SELECT * FROM
(Select mykey, DENSE_RANK () OVER (order by ORD_COL DESC) AS MYRANK
From mytable) AS RANKED_TABLE
Where myrank <= N

3. to avoid DBMSs dialect problems, M3 can have the following standard (SQL 92:
M2:
SELECT * from mytable M1 where n>
(Select count (*) from mytable M2
WHERE M2.ORD _ COL> M1.ORD _ COL );
Note: Both comparison operators are>
M3:
SELECT * from mytable M1 where n>
(Select count (DISTINCT M2.ORD _ COL) from mytable M2
WHERE M2.ORD _ COL> M1.ORD _ COL );
Note: The difference with M2 lies in the use of DISTINCT.
As for M1, I haven't thought of a proper standard writing method yet. please add.

4. about MYSQL
Compared with mainstream RDBMS, MYSQL is much simpler and more naive. when running MYSQL, it is found that SUBQUERY is not supported. Therefore, the top n problem in MYSQL is more complicated.
SELECT * from mytable, MYTABLE M2
WHERE M2.ORD _ COL> = M1.ORD _ COL
Group by M1.MYKEY
Having count (M2.ORD _ COL) <= N
However, this statement is problematic. an extreme example: when all ORD_COL values are the same, if anyone thinks about how to solve this problem, please add.

----------------------------------------------------------------------------------
Each statement has been tested. EXAMPLE:
Create table mytable (mykey int, ord_col int );
Insert into mytable values (1,100 );
Insert into mytable values (2,100 );
Insert into mytable values (3, 99 );
Insert into mytable values (4,101 );
Insert into mytable values (5,101 );

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.