Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
WITH RECURSIVE fibonacci(recursion_level, fibonacci_number, next_number)
AS (
# Base Case
SELECT 0 AS recursion_level,
0 AS fibonacci_number,
1 AS next_number
UNION ALL
# Recursion query
SELECT recursion_level + 1 AS recursion_level,
next_number AS fibonacci_number,
fibonacci_number + next_number AS next_number
FROM fibonacci
WHERE
recursion_level < 10
)
SELECT * FROM fibonacci
WITH RECURSIVE fibonacci(fibonacci_number, next_number)
AS (
SELECT
0 AS fibonacci_number,
1 AS next_number
UNION ALL
SELECT
next_number AS fibonacci_number,
fibonacci_number + next_number AS next_number
FROM fibonacci
)
SELECT * FROM fibonacci
LIMIT 10;
@gabidavila

This comment has been minimized.

Copy link
Owner Author

@gabidavila gabidavila commented Nov 8, 2017

+-----------------+------------------+-------------+
| recursion_level | fibonacci_number | next_number |
+-----------------+------------------+-------------+
|               0 |                0 |           1 |
|               1 |                1 |           1 |
|               2 |                1 |           2 |
|               3 |                2 |           3 |
|               4 |                3 |           5 |
|               5 |                5 |           8 |
|               6 |                8 |          13 |
|               7 |               13 |          21 |
|               8 |               21 |          34 |
|               9 |               34 |          55 |
|              10 |               55 |          89 |
+-----------------+------------------+-------------+
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.