Smooth nested posts in postgres

How to smooth a column fooin an external select (in PostgreSQL)?

WITH RECURSIVE t AS (
    SELECT row(d.*) as foo FROM some_multicolumn_table as d
UNION ALL
    SELECT foo FROM t WHERE random() < .5
)
SELECT foo FROM t

I want to output all columns (horizontally, that is, as several columns) some_multicolumn_tablein the outer select, and not just a single column "record".

How to do it?

+3
source share
1 answer

You do not need a constructor ROW, and you can expand the record using (foo).*:

WITH RECURSIVE t AS (
    SELECT d as foo FROM some_multicolumn_table as d
UNION ALL
    SELECT foo FROM t WHERE random() < .5
)
SELECT (foo).* FROM t;

Although this request can be simply written as:

WITH RECURSIVE t AS (
    SELECT d.* FROM some_multicolumn_table as d
UNION ALL
    SELECT t.* FROM t WHERE random() < .5
)
SELECT * FROM t;

And I recommend trying to keep it as simple as possible. But I guess this is just an example.

+2
source

All Articles