Today in the review project code to see such a problem, there is a number table, each time you need to randomly take 6 free numbers from such a table, that is, each time the 6 numbers are taken out should be different. And then I saw this SQL.
Select t.*
From Tel_number_tbl t
where t.status = ' idle '
and T.area_code = ' 0571 '
and T.DELETE_FLG = ' not deleted '
and RowNum <= 6
Order BY Trunc (Dbms_random.value (1,7))
Analysis: Obviously, this is not correct, because each time it will be sorted by the default first 6, and then the six data are randomly sorted, which will result in each fetch six data is the same, although they each time the order is different. The correct wording should be
SELECT * FROM (
Select t.*
From Tel_number_tbl t
where t.status = ' idle '
and T.area_code = ' 0571 '
and T.DELETE_FLG = ' not deleted '
Order BY Trunc (Dbms_random.value (1,7))) Temp
where RowNum <= 6
Report:
I.use of the Oracle Trunc () function
TRUNC (for dates)
Precision to day Select Trunc (sysdate, ' DD ') from dual result is: 2010-9-17
Accurate to Month select Trunc (sysdate, ' mm ') from dual result: 2010-9-1
Accurate to Year select Trunc (sysdate, ' yy ') from dual result: 2010-1-1
TRUNC (for number)
The TRUNC function returns the processed value, and its working mechanism is very similar to the round function, except that the function does not make the corresponding rounding selection processing of the specified fractional number before or after it, and it is truncated altogether.
Its specific syntax format is as follows
TRUNC (number, [decimals])
which
Number to be intercepted and processed
Decimals indicates the number of digits after the decimal point to be retained. Optional, ignore it to truncate all the decimal parts
Here's how this function is used:
Select TRUNC (899.985,2) from dual results are: 899.98
Select TRUNC (899.985) from dual results are: 899
Select TRUNC (899.985) from dual results are: 800
Note: The second parameter can be a negative number, which means that the portion of the digits to the left of the decimal point is truncated to 0.
Second,dbms_random.value function
Dbms_random is a package that can generate random numbers or strings. This package has several functions such as initialize (), seed (), terminate (), value (), Normal (), random (), string (), but value () is the most common.
The use of value () generally has two types,
The first function value return number;
This usage has no parameters and returns a numeric value with a 38-bit precision ranging from 0.0 to 1.0, but not 1.0, as follows:
Select Dbms_random.value () from dual results: 0.265729284748573
The second value has two parameters, the first is the lower limit, the second is the upper limit, and the number between the lower bound and the upper bound is generated, but not the upper limit, as follows:
Select Dbms_random.value (1,7) from dual results are: 3.38380283953849
The two combine together with Trunc (Dbms_random.value (1,7)) which is randomly obtained as an integer X, 1<=x<7, as follows:
Select Trunc (Dbms_random.value (1, 7)) from dual result: 3