SQL takes the first few lines of a record statement

Source: Internet
Author: User
Tags sqlite database oracle database

SQLite database:

The code is as follows Copy Code
SELECT * FROM table limit N


DB2 Database:

The code is as follows Copy Code

SELECT * FROM tab fetch the only

Oracle Database:

The code is as follows Copy Code

SELECT * from tab where RowNum <=10

SQL Server database:

The code is as follows Copy Code

Select Top * from tab


1 if there is a primary key in the table, you can use in the way.

The code is as follows Copy Code

SELECT * from Tmp_trans t
WHERE ID in (
SELECT Top 2 ID
From Tmp_trans
WHERE Po_no=t.po_no
ORDER BY Trans_date DESC
)

2 If there is no primary key, you can use to determine how many records before this record. However, if you encounter trans_date the same situation will not be allowed when using this method. If there are three of the largest records in the Trans_date, these three records will be found.

The code is as follows Copy Code

SELECT *
From Tmp_trans t
WHERE (
SELECT COUNT (*)
From Tmp_trans
WHERE Po_no=t.po_no and Trans_date>t.trans_date
) <2

3) Use the CROSS APPLY clause. CROSS Apply is a new feature that comes out of SQL Server 2005 and is used to pass parameters when a table joins.

The code is as follows Copy Code

SELECT DISTINCT b.*
From Tmp_trans A
CROSS APPLY
(
SELECT Top (2) * from Tmp_trans WHERE a.po_no=po_no order by Trans_date DESC
) b

2. Use the automatically generated row number. You can group with the partition by clause when using row_number (). It is recommended that this approach be used.

The code is as follows Copy Code

SELECT * FROM (
Select Row_number () over (PARTITION by po_no ORDER by Trans_date DESC) as rowid,*
From Tmp_trans
) A
where rowid<=2


MySQL database:

The code is as follows Copy Code

SELECT * from tab limit 10

Note: If you have the following ORDER BY clause for Oracle queries, the query data may not be what you want
Oracle processing process is: First search out the rownum<2 information, and then sort

The code is as follows Copy Code

Select table_name from T_tables where ROWNUM<2 ORDER BY table_name DESC;

It needs to be written in the following ways:

  code is as follows copy code

Select t2.* From (SELECT * to t_tables T ORDER by table_name DESC) T2 where rownum<2

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.