SeriesInt
Updated 2023-10-18 20:12:42.147000
Syntax
SELECT * FROM [westclintech].[wct].[SeriesInt] (
<@StartValue, float,>
,<@StopValue, float,>
,<@StepValue, float,>
,<@MaxIterations, float,>
,<@SeriesType, nvarchar(4000),>)
Description
Use the table-valued function SeriesInt to generate a range of integer values.
Arguments
@StopValue
the upper (or lower) limit of the integer 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. Non-integer values are truncated to integer.
@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. Non-integer values are truncated to integer.
@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 number that are multiples of @StepValue until @MaxIterations is reached.
@StartValue
the lower (or upper) limit of the integer 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. Non-integer values are truncated to integer.
Return Type
table
{"columns": [{"field": "colName", "headerName": "Name", "header": "name"}, {"field": "colDatatype", "headerName": "Type", "header": "type"}, {"field": "colDesc", "headerName": "Description", "header": "description", "minWidth": 1000}], "rows": [{"id": "47eab310-31ce-438d-8764-a57def6fe9ca", "colName": "Seq", "colDatatype": "int", "colDesc": "A monotonically increasing sequence number"}, {"id": "c9a20b89-03c8-409d-aa1e-62b864d86310", "colName": "SeriesValue", "colDatatype": "int", "colDesc": "The int value"}]}
Remarks
To generate a series for non-integer values use the SeriesFloat function.
To generate a series for dates 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 100.
SELECT *
FROM wct.SeriesInt(1, 100, 1, NULL, 'L');
Here is the resultant table.
{"columns":[{"field":"Seq","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"SeriesValue","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"}],"rows":[{"Seq":"1","SeriesValue":"1"},{"Seq":"2","SeriesValue":"2"},{"Seq":"3","SeriesValue":"3"},{"Seq":"4","SeriesValue":"4"},{"Seq":"5","SeriesValue":"5"},{"Seq":"6","SeriesValue":"6"},{"Seq":"7","SeriesValue":"7"},{"Seq":"8","SeriesValue":"8"},{"Seq":"9","SeriesValue":"9"},{"Seq":"10","SeriesValue":"10"},{"Seq":"11","SeriesValue":"11"},{"Seq":"12","SeriesValue":"12"},{"Seq":"13","SeriesValue":"13"},{"Seq":"14","SeriesValue":"14"},{"Seq":"15","SeriesValue":"15"},{"Seq":"16","SeriesValue":"16"},{"Seq":"17","SeriesValue":"17"},{"Seq":"18","SeriesValue":"18"},{"Seq":"19","SeriesValue":"19"},{"Seq":"20","SeriesValue":"20"},{"Seq":"21","SeriesValue":"21"},{"Seq":"22","SeriesValue":"22"},{"Seq":"23","SeriesValue":"23"},{"Seq":"24","SeriesValue":"24"},{"Seq":"25","SeriesValue":"25"},{"Seq":"26","SeriesValue":"26"},{"Seq":"27","SeriesValue":"27"},{"Seq":"28","SeriesValue":"28"},{"Seq":"29","SeriesValue":"29"},{"Seq":"30","SeriesValue":"30"},{"Seq":"31","SeriesValue":"31"},{"Seq":"32","SeriesValue":"32"},{"Seq":"33","SeriesValue":"33"},{"Seq":"34","SeriesValue":"34"},{"Seq":"35","SeriesValue":"35"},{"Seq":"36","SeriesValue":"36"},{"Seq":"37","SeriesValue":"37"},{"Seq":"38","SeriesValue":"38"},{"Seq":"39","SeriesValue":"39"},{"Seq":"40","SeriesValue":"40"},{"Seq":"41","SeriesValue":"41"},{"Seq":"42","SeriesValue":"42"},{"Seq":"43","SeriesValue":"43"},{"Seq":"44","SeriesValue":"44"},{"Seq":"45","SeriesValue":"45"},{"Seq":"46","SeriesValue":"46"},{"Seq":"47","SeriesValue":"47"},{"Seq":"48","SeriesValue":"48"},{"Seq":"49","SeriesValue":"49"},{"Seq":"50","SeriesValue":"50"},{"Seq":"51","SeriesValue":"51"},{"Seq":"52","SeriesValue":"52"},{"Seq":"53","SeriesValue":"53"},{"Seq":"54","SeriesValue":"54"},{"Seq":"55","SeriesValue":"55"},{"Seq":"56","SeriesValue":"56"},{"Seq":"57","SeriesValue":"57"},{"Seq":"58","SeriesValue":"58"},{"Seq":"59","SeriesValue":"59"},{"Seq":"60","SeriesValue":"60"},{"Seq":"61","SeriesValue":"61"},{"Seq":"62","SeriesValue":"62"},{"Seq":"63","SeriesValue":"63"},{"Seq":"64","SeriesValue":"64"},{"Seq":"65","SeriesValue":"65"},{"Seq":"66","SeriesValue":"66"},{"Seq":"67","SeriesValue":"67"},{"Seq":"68","SeriesValue":"68"},{"Seq":"69","SeriesValue":"69"},{"Seq":"70","SeriesValue":"70"},{"Seq":"71","SeriesValue":"71"},{"Seq":"72","SeriesValue":"72"},{"Seq":"73","SeriesValue":"73"},{"Seq":"74","SeriesValue":"74"},{"Seq":"75","SeriesValue":"75"},{"Seq":"76","SeriesValue":"76"},{"Seq":"77","SeriesValue":"77"},{"Seq":"78","SeriesValue":"78"},{"Seq":"79","SeriesValue":"79"},{"Seq":"80","SeriesValue":"80"},{"Seq":"81","SeriesValue":"81"},{"Seq":"82","SeriesValue":"82"},{"Seq":"83","SeriesValue":"83"},{"Seq":"84","SeriesValue":"84"},{"Seq":"85","SeriesValue":"85"},{"Seq":"86","SeriesValue":"86"},{"Seq":"87","SeriesValue":"87"},{"Seq":"88","SeriesValue":"88"},{"Seq":"89","SeriesValue":"89"},{"Seq":"90","SeriesValue":"90"},{"Seq":"91","SeriesValue":"91"},{"Seq":"92","SeriesValue":"92"},{"Seq":"93","SeriesValue":"93"},{"Seq":"94","SeriesValue":"94"},{"Seq":"95","SeriesValue":"95"},{"Seq":"96","SeriesValue":"96"},{"Seq":"97","SeriesValue":"97"},{"Seq":"98","SeriesValue":"98"},{"Seq":"99","SeriesValue":"99"},{"Seq":"100","SeriesValue":"100"}]}
Generate a series of numbers from 100 to 1 in decrements of 3.
SELECT *
FROM wct.SeriesInt(100, 1, -3, NULL, 'L');
Here is the resultant table.
{"columns":[{"field":"Seq","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"SeriesValue","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"}],"rows":[{"Seq":"1","SeriesValue":"100"},{"Seq":"2","SeriesValue":"97"},{"Seq":"3","SeriesValue":"94"},{"Seq":"4","SeriesValue":"91"},{"Seq":"5","SeriesValue":"88"},{"Seq":"6","SeriesValue":"85"},{"Seq":"7","SeriesValue":"82"},{"Seq":"8","SeriesValue":"79"},{"Seq":"9","SeriesValue":"76"},{"Seq":"10","SeriesValue":"73"},{"Seq":"11","SeriesValue":"70"},{"Seq":"12","SeriesValue":"67"},{"Seq":"13","SeriesValue":"64"},{"Seq":"14","SeriesValue":"61"},{"Seq":"15","SeriesValue":"58"},{"Seq":"16","SeriesValue":"55"},{"Seq":"17","SeriesValue":"52"},{"Seq":"18","SeriesValue":"49"},{"Seq":"19","SeriesValue":"46"},{"Seq":"20","SeriesValue":"43"},{"Seq":"21","SeriesValue":"40"},{"Seq":"22","SeriesValue":"37"},{"Seq":"23","SeriesValue":"34"},{"Seq":"24","SeriesValue":"31"},{"Seq":"25","SeriesValue":"28"},{"Seq":"26","SeriesValue":"25"},{"Seq":"27","SeriesValue":"22"},{"Seq":"28","SeriesValue":"19"},{"Seq":"29","SeriesValue":"16"},{"Seq":"30","SeriesValue":"13"},{"Seq":"31","SeriesValue":"10"},{"Seq":"32","SeriesValue":"7"},{"Seq":"33","SeriesValue":"4"},{"Seq":"34","SeriesValue":"1"}]}
Generate 10 random numbers, in multiples of 5, between 1 and 1000. Your results will vary.
SELECT *
FROM wct.SeriesInt(1, 1000, 5, 10, 'R');
This produces the following results.
{"columns":[{"field":"Seq","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"SeriesValue","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"}],"rows":[{"Seq":"1","SeriesValue":"135"},{"Seq":"2","SeriesValue":"435"},{"Seq":"3","SeriesValue":"160"},{"Seq":"4","SeriesValue":"705"},{"Seq":"5","SeriesValue":"660"},{"Seq":"6","SeriesValue":"945"},{"Seq":"7","SeriesValue":"670"},{"Seq":"8","SeriesValue":"555"},{"Seq":"9","SeriesValue":"685"},{"Seq":"10","SeriesValue":"15"}]}
You can simulate 25 rolls of a die with the following statement. Your results will vary.
SELECT *
FROM wct.SeriesInt(1, 6, '', 25, 'R');
Here’s the resultant table:
{"columns":[{"field":"Seq","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"SeriesValue","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"}],"rows":[{"Seq":"1","SeriesValue":"5"},{"Seq":"2","SeriesValue":"2"},{"Seq":"3","SeriesValue":"4"},{"Seq":"4","SeriesValue":"1"},{"Seq":"5","SeriesValue":"1"},{"Seq":"6","SeriesValue":"4"},{"Seq":"7","SeriesValue":"1"},{"Seq":"8","SeriesValue":"2"},{"Seq":"9","SeriesValue":"6"},{"Seq":"10","SeriesValue":"4"},{"Seq":"11","SeriesValue":"1"},{"Seq":"12","SeriesValue":"2"},{"Seq":"13","SeriesValue":"3"},{"Seq":"14","SeriesValue":"3"},{"Seq":"15","SeriesValue":"6"},{"Seq":"16","SeriesValue":"3"},{"Seq":"17","SeriesValue":"3"},{"Seq":"18","SeriesValue":"5"},{"Seq":"19","SeriesValue":"3"},{"Seq":"20","SeriesValue":"5"},{"Seq":"21","SeriesValue":"2"},{"Seq":"22","SeriesValue":"5"},{"Seq":"23","SeriesValue":"2"},{"Seq":"24","SeriesValue":"1"},{"Seq":"25","SeriesValue":"5"}]}
Here’s a simulation of 25 throws of a pair of dice. Your results will vary.
SELECT n.D1,
seriesvalue as D2
FROM
(SELECT seq, seriesvalue as D1 FROM wct.SeriesInt(1, 6, '', 25, 'R') ) n ,
wct.SeriesInt(1, 6, '', 25, 'R') m
WHERE m.seq = n.seq;
Which produces the following results.
{"columns":[{"field":"D1","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"D2","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"}],"rows":[{"D1":"1","D2":"5"},{"D1":"5","D2":"1"},{"D1":"5","D2":"6"},{"D1":"4","D2":"2"},{"D1":"6","D2":"2"},{"D1":"1","D2":"2"},{"D1":"1","D2":"2"},{"D1":"5","D2":"1"},{"D1":"5","D2":"3"},{"D1":"6","D2":"2"},{"D1":"1","D2":"1"},{"D1":"3","D2":"2"},{"D1":"5","D2":"3"},{"D1":"3","D2":"5"},{"D1":"1","D2":"4"},{"D1":"1","D2":"4"},{"D1":"3","D2":"2"},{"D1":"3","D2":"1"},{"D1":"3","D2":"4"},{"D1":"6","D2":"3"},{"D1":"4","D2":"1"},{"D1":"2","D2":"3"},{"D1":"3","D2":"4"},{"D1":"2","D2":"1"},{"D1":"3","D2":"2"}]}
Here’s the results of a simulation of 36,000 throws of a pair of dice. Your results will vary.
SELECT D1 + D2,
COUNT(*)
FROM
(
SELECT n.D1,
seriesvalue as D2
FROM
(
SELECT seq,
seriesvalue as D1
FROM wct.SeriesInt(1, 6, '', 36000, 'R')
) n ,
wct.SeriesInt(1, 6, '', 36000, 'R') m
WHERE m.seq = n.seq
) o
GROUP BY D1 + D2
ORDER BY 1;
This produced the following result.
----------- -----------
2 969
3 2057
4 3085
5 4075
6 5026
7 6091
8 4850
9 4032
10 2966
11 1911
12 938
You can combine SeriesInt with other functions to perform statistical analysis. The following query uses the SeriesInt function in the calculation of the chi-squared test for 36,000 rolls of the dice.
SELECT wct.CHITEST2_q('
SELECT COUNT(*)
FROM (SELECT n.D1
,seriesvalue as D2
FROM (SELECT seq
,seriesvalue as D1
FROM wct.SeriesInt(1, 6,' + wct.QUOTES('') + ',36000,' + wct.QUOTES('R') + ')) n,
wct.SeriesInt(1, 6,' + wct.QUOTES('') + ',36000,' + wct.QUOTES('R') + ') m
WHERE m.seq = n.seq) o
GROUP BY D1 + D2
ORDER BY D1 + D2',
'SELECT 1000 UNION ALL
SELECT 2000 UNION ALL
SELECT 3000 UNION ALL
SELECT 4000 UNION ALL
SELECT 5000 UNION ALL
SELECT 6000 UNION ALL
SELECT 5000 UNION ALL
SELECT 4000 UNION ALL
SELECT 3000 UNION ALL
SELECT 2000 UNION ALL
SELECT 1000') as CHITEST;
This produced the following result (your results will vary).
{"columns":[{"field":"CHITEST","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"}],"rows":[{"CHITEST":"0.875174792873638"}]}