Sample Data Collection

Source: Internet
Author: User

---- Start

We often encounter this situation and want to see several data records in a table, such as 10 and 20. In DB2, we can write as follows:

Select * from <table_name> fetch first 10 rows only; 

I don't know if you noticed it. No matter how many times you execute the preceding statement, the result set remains unchanged. So what should I do if I want to query 10 records randomly? You can use the following SQL:

Select * from <table_name> order by rand () Fetch first 10 rows only; 

The above is the simplest way to collect sample data. In DB2, more professional is to use tablesample to collect sample data. So why should we collect sample data? The main reason is that grouping statistics on massive data is time-consuming and laborious. At this time, we can collect sample data and then collect statistics on the sample data to predict the overall trend.

I. Syntax

Select... from <br/> <Table-Name> tablesample [Bernoulli | System] (percent) repeatable (Num) <br/> where... 

Ii. Example

Select * <br/> from staff tablesample Bernoulli (8) repeatable (586) <br/> order by ID; </P> <p> -- Description <br/> 8% of the sample data is extracted from the staff table using the Bernoulli sampling method, repeatable indicates that the same result is returned when the same statement is executed multiple times.

Iii. Sampling Method

1. Bernoulli (Row-level bernuoli sampling): It checks each row, with high accuracy but poor performance.

2. System (system page-level sampling): It checks each data page (a data page contains several rows), high performance, but poor accuracy.

--- For more information, see:DB2 SQL

----Statement: indicate the source for reprinting.

---- Last updated on 2010.2.2

---- Written by shangbo on 2009.9.25

---- End

 

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.