tags:

views:

151

answers:

1

Hi,

Im trying to create a easy sine curv in SQL which alternates between the values (0-23).

I have the following variables: x, which is the current offset. y which is the destinated offset, and z which is my max value(23).

Anyone that could help me with the expression? Have googled and read about it, but havent got it to work yet..

Would be greatful for any answers..

Best Regards, Tom

+4  A: 

You need some kind of a dummy rowset to do this.

In Oracle:

SELECT  level - 1, SIN((level - 1) * 3.141592653 / 12)
FROM    dual
CONNECT BY
        level <= 24

In SQL Server 2005+:

WITH    rows AS
        (
        SELECT  0 AS level
        UNION ALL
        SELECT  level + 1
        FROM    rows
        WHERE   level <= 23
        )
SELECT  level, SIN((level) * 3.141592653 / 12)
FROM    rows

In PostgreSQL:

SELECT  level, SIN(level * 3.141592653 / 12)
FROM    generate_series(0, 23) level

In MySQL:

SELECT  level, SIN(level * 3.141592653 / 12)
FROM    (
        SELECT  0 AS level
        UNION ALL
        SELECT  1 AS level
        UNION ALL
        …
        /*
        Yes, you need to repeat it 24 times.

        Everyone who reads it: please call MySQL and ask them
        to implement generate_series.

        Thanks.
        */
        UNION ALL
        SELECT  23 AS level
        ) q
Quassnoi
That really helped me! Thanks!