SQL Sequence Cross Function

The seq_cross() function returns a sequence of values indicating where the input sequence crosses zero. The second argument specifies whether to indicate the position where the sequence crosses zero in the positive or negative direction: a negative value indicates that zero-crossings in the downward direction are desired; a positive value indicates that zero-crossings in the upward direction are desired; a value of 0 indicates zero-crossings in either direction are desired.

Following is an example script demonstrating the seq_cross() function:

 
    INSERT INTO SimpleSequence(testNumber,iVal1,iVal2)
    VALUES(8,'{-1,0,1,-3,2}');
     
    SELECT iVal1,seq_cross(iVal1,-1) AS "cross-neg",seq_cross(iVal1,1) AS "cross-pos",seq_cross(iVal1,0) AS "cross-0"
    FROM SimpleSequence WHERE testNumber=8;
     
    iVal1{}
    cross-neg{}
    cross-pos{}
    cross-0{}
    --------------------------------------------------
    {-1, 0, 1, -3, 2}
    {3}
    {1, 4}
    {1, 3, 4}
     

So, in the example above the result sequences are obtained as follows:

Sample script

A sample script to demonstrate this select statement using xSQL can be run from the samples/xsql/scripts/financial directory with the following command:

     
    f 5