Logo

SQL Server SeriesFloat Function

Updated 2023-10-18 19:59:18.197000

Description

Use the table-valued function SeriesFloat to generate a range of floating point values.

Syntax

SELECT * FROM [westclintech].[wct].[SeriesFloat] (
  <@StartValue, float,>
 ,<@StopValue, float,>
 ,<@StepValue, float,>
 ,<@MaxIterations, float,>
 ,<@SeriesType, nvarchar(4000),>)

Arguments

@StartValue

the lower (or upper) limit of the floating point range. The first value may be the maximum or the minimum value, depending on the @StepValue. @StartValue must be of the type float or of a type that implicitly converts to float.

@StopValue

the upper (or lower) limit of the floating point range. The last value may be the maximum or the minimum value, depending on the @StepValue. @StopValue must be of the type float or of a type that implicitly converts to float.

@StepValue

the increment (or decrement) used to determine the next value in a linear series (see @SeriesType). In a random series, this is the multiple used to calculate a random number between the @StartValue and the @StopValue. @StepValue must be of the type float or of a type that implicitly converts to float.

@MaxIterations

the number of results to return. For linear series (see @SeriesType), a value of NULL will return all the values between the @StartValue and the @StopValue specified by the @StepValue. For random series, NULL is not permitted. @MaxIterations must be of the type float or of a type that implicitly converts to float. Non-integer values are truncated to integer.

@SeriesType

the letter 'L' or 'l' for linear series, or the letter 'R' or 'r' for random series. A linear series will generate numbers from the @StartValue incremented by the @StepValue until either the number of iterations is equal to @MaxIterations or the greatest value less than or equal to the @StopValue has been reached.

A random series will randomly generate numbers that are multiples of @StepValue until @MaxIterations is reached.

Return Type

table

colNamecolDatatypecolDesc
SeqintA monotonically increasing sequence number
SeriesValuefloatThe float variable

Remarks

To generate a series for integer values use the SeriesInt function.

To generate a series for date values use the SeriesDate function.

If the @StepValue > @StopValue and @SeriesType = 'L', then only one row will be returned with SeriesValue equal to @StartValue.

If the @StepValue > @StopValue and @SeriesType = 'R', then @MaxIterations rows will be returned with SeriesValue equal to the lesser of @StartValue and @StopValue.

If @SeriesType is NULL, then @SeriesType is set to 'L'.

If @StartValue is NULL, it will be calculated from @StopValue, @MaxIterations and @StepValue.

If @StopValue is NULL, it will be calculated from @StartValue, @MaxIterations and @StepValue.

If @StepValue is NULL and @SeriesType is 'L' and @StopValue > @StartValue then @StepValue is set to 1.

If @StepValue is NULL and @SeriesType is 'L' and @StopValue < @StartValue then @StepValue is set to -1.

If @SeriesType is 'L' and SIGN(@StepValue) <> SIGN(@StopValue - @StartValue) then only one row will be returned.

If @SeriesType is 'R', @MaxIterations must be greater than 0 and not equal to NULL.

If @SeriesType is 'R', @StartValue cannot be NULL

If @SeriesType is 'R', @StopValue cannot be NULL.

Examples

Generate a list of numbers from .1 to 10.

SELECT *
FROM wct.SeriesFloat(0.1, 10.0, 0.1, NULL, 'L');

This produces the following result.

SeqSeriesValue
10.1
20.2
30.3
40.4
50.5
60.6
70.7
80.8
90.9
101
111.1
121.2
131.3
141.4
151.5
161.6
171.7
181.8
191.9
202
212.1
222.2
232.3
242.4
252.5
262.6
272.7
282.8
292.9
303
313.1
323.2
333.3
343.4
353.5
363.6
373.7
383.8
393.9
404
414.1
424.2
434.3
444.4
454.5
464.6
474.7
484.8
494.9
505
515.1
525.2
535.3
545.4
555.5
565.6
575.7
585.8
595.9
606
616.1
626.2
636.3
646.4
656.5
666.6
676.7
686.8
696.9
707
717.1
727.2
737.3
747.4
757.5
767.6
777.7
787.8
797.9
808
818.1
828.2
838.3
848.4
858.5
868.6
878.7
888.8
898.9
909
919.1
929.2
939.3
949.4
959.5
969.6
979.7
989.8
999.9
10010

Generate a series of numbers from 10 to -10 in decrements of 3.3333.

SELECT *
FROM wct.SeriesFloat(10, -10, -3.3333, NULL, 'L');

Here is the resultant table:

SeqSeriesValue
110
26.6667
33.3334
49.99999999997669E-05
5-3.3332
6-6.6665
7-9.9998

Generate 10 random numbers, between 0 and 1. Your results will vary.

SELECT *
FROM wct.SeriesFloat(0, 1, '', 10, 'R');

This produces the following results (your results will be different).

SeqSeriesValue
10.0787300244340347
20.0393758332540169
30.237071212957181
40.275765785144533
50.765305606073377
60.506849329688982
70.654947900052624
80.0535740512672691
90.774494760099097
100.73788640263392

Generate 10 random numbers, between 1 and 100 in multiples of .03125. Your results will vary.

SELECT *
FROM wct.SeriesFloat(1, 100, .03125, 10, 'R');

This produces the following results.

SeqSeriesValue
185.625
258.59375
396.28125
497.6875
587.3125
670.90625
742.46875
898.9375
963.40625
1078.875

You can use the SeriesFloat function in conjunction with other functions to generate random numbers consistent with known statistical distributions. For example, we could randomly generate 15 numbers from the standard normal distribution with the following statement:

SELECT seq,
       wct.NORMSINV(seriesvalue) as NORMSINV
FROM wct.SeriesFloat(0, 1, '', 15, 'R');

This produces the following result:

seqNORMSINV
10.115115246564977
20.0861232660335375
3-0.51211327862208
40.0833024753960621
5-1.1040088954424
60.665494336529001
7-0.980701775124289
80.445878666345121
90.300888521636836
10-0.197957795305958
11-0.0368872432918503
12-1.81611011290773
130.397721052691346
14-0.0960160763401366
150.8706113574682

We could generate 15 random numbers for a normal distribution with a mean of 100 and a standard deviation of 15 with the following statement:

SELECT seq
,wct.NORMINV(seriesvalue, 100, 15)
FROM wct.SeriesFloat(0,1,'',15,'R');

This produces the following result.

seq
1106.529474006228
2123.00857181537
380.099070876261
498.0276399646496
591.7737036409259
6108.09737139573
785.050164912141
8122.951511599054
9115.077771376198
1093.4072825124728
1186.2396638843069
1292.7253168560602
13103.122155353833
1491.1914297396313
15103.55078537015

We could generate 15 random numbers for a gamma distribution with a shape parameter of 9 with the following statement:

SELECT seq
,wct.GAMMAINV(seriesvalue, 9, 1)
FROM wct.SeriesFloat(0,1,'',15,'R');

This produces the following result.

seq
113.5629377694821
211.1932994911787
310.9438339532567
49.90162997348385
514.6773466847694
610.3188014158663
710.7589410774399
810.637936085491
910.1797678083148
108.82692712547266
1110.700105146452
1210.4919966511707
135.94455182109008
1410.9149500274106
158.01495898641042