SQL Server's Random Functions newid () and rand ()

Source: Internet
Author: User
Tags floor function

Obtain two random records from table A and use select top 10 * From ywle order by newid ()
Order by is generally sorted by a certain field. The return value of newid () is uniqueidentifier. How does order by newid () randomly select records?
Newid () generates a value when scanning each record, and the generated value is random, with no case sequence. so the final result is sorted by this sort, and the sorting result is of course unordered.
Or
Select top 10 *, newid () as random from ywle where ywlename = '001' ordey by random
Lower efficiency
Because newid () returns a unique value of the uniqueidentifier type. The values produced by newid () are different each time. Therefore, the results of each sort are different based on such values.

The principle is to extract all IDs and then use the random function to retrieve one of them, and then use the random obtained ID to retrieve records from the database. The cost is a little high.

 

SQL Server's Random Functions newid () and rand ()

Select * From northwind... orders order by newid ()
-- Random sorting

Select top 10 * From northwind .. orders order by newid ()
-- Randomly retrieve 10 records from the orders table

Example

A. Use the newid function for variables
The following example uses newid () to assign values to variables declared as uniqueidentifier data types. Output this value before testing the value of the uniqueidentifier data type variable.
-- Creating a local variable with declareset syntax.
Declare @ myid uniqueidentifier
Set @ myid = newid ()
Print 'value of @ myid is '+ convert (varchar (255), @ myid)
The following is the result set:
Value of @ myid is 6f9619ff-8b86-d011-b42d-00c04fc964ff
Note:
Newid returns different values for each computer. The displayed number only serves as an explanation.

Random Function: rand ()
Run select rand () in the query analyzer and you can see that the result is a random decimal like this: 0.36361513486289558. decimal places like this are not used much in practical applications, generally, random numbers are random integers. Let's look at the following two random integer acquisition methods:

1,
A: Select floor (RAND () * n) --- The generated number is as follows: 12.0
B: Select cast (floor (RAND () * n) as INT) --- The generated number is as follows: 12

2,
A: Select ceiling (RAND () * n) --- The generated number is as follows: 12.0
B: Select cast (ceiling (RAND () * n) as INT) --- The generated number is as follows: 12

The N in it is an integer you specify, such as 100. We can see that the method of the two methods contains. the decimal value of 0, and the B method is the real integer.
Generally, there is no difference between the two methods? There is actually one thing, that is, their range of random numbers:
The number range of Method 1: 0 to N-1, for example cast (floor (RAND () * 100) as INT) will generate any integer between 0 and 99
The number range of Method 2: 1 to n. For example, cast (ceiling (RAND () * 100) as INT) generates any integer between 1 and 100.
For this difference, you can see the online help of SQL:
Bytes ------------------------------------------------------------------------------------

Compare ceiling and floor

The ceiling function returns the smallest integer greater than or equal to the given numeric expression. The floor function returns the largest integer less than or equal to the given numeric expression. For example, for the numeric expression 12.9273, ceiling returns 13 and floor returns 12. The data types returned by floor and ceiling are the same as those of the input numeric expression.
----------------------------------------------------------------------------------
Now, you can use these two methods as needed to obtain the random number ^_^.

In addition, I would like to remind you that the method for randomly obtaining any N records in the table is very simple, just use newid ():
Select Top N * From table_name order by newid () ---- N is a specified integer, and the table is the number of records obtained.

Related Article

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.