图上的正弦曲线 - SQL

时间:2009-08-04 12:00:57

标签: sql math

我试图在SQL中创建一个简单的正弦曲线,它在值(0-23)之间交替。

我有以下变量:x,这是当前的偏移量。 y是指定偏移量,z是我的最大值(23)。

任何可以帮助我表达的人?谷歌搜索并阅读它,但还没有让它工作..

对任何答案都会很高兴。

最诚挚的问候, 汤姆

1 个答案:

答案 0 :(得分:4)

您需要某种虚拟行集来执行此操作。

Oracle

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

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

PostgreSQL

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

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