4 ms·
I prefer the syntax of recursive CTEs (as many others have mentioned). A simple use case that I have in my work is generating a date table, and a recursive CTE
by greggyb 12y ago
I prefer the syntax of recursive CTEs (as many others have mentioned).
A simple use case that I have in my work is generating a date table, and a recursive CTE sits at the core of this script:
TSQL Syntax:
WITH dateCTE (FullDate) AS
(SELECT '20100101'
UNION ALL
SELECT DATEADD(DAY, 1, FullDate)
FROM dateCTE
WHERE FullDate < 20101231
)
In the example above, we generate a table of all dates in 2010.
I don't get much chance to play with Postgres, but from their documentation[0] it seems the concept transfers over readily by using WITH RECURSIVE.
[0]http://www.postgresql.org/docs/9.4/static/queries-with.html http://www.postgresql.org/docs/9.4/static/queries-with.html
- JonnieCache 12y agoIn postgres you would use generate_series for that. http://www.postgresql.org/docs/current/static/functions-srf.html http://www.postgresql.org/docs/current/static/functions-srf....
- greggyb 12y agoNeat. It seems we can simulate this behavior in TSQL[0] by using recursive CTEs in a function. [0]https://developer42.wordpress.com/2014/09/17/t-sql-generate-series-getting-a-list-of-numbers-in-a-given-range/ https://developer42.wordpress.com/2014/09/17/t-sql-generate-...