WITH RECURSIVE as second part CTE in query. Postgres - postgresql

How I can write a query like that:
with t1 as
(
select id
from table1
),
RECURSIVE t2(
select * from t2
union
...
)
Currently it's not allowed?

The recursive needs to be right after the WITH regardless on where you put the recursive CTE:
with recursive t1 as
(
select id
from table1
), t2 (
select *
from t2
union
...
)
...

Related

Is there a way to alias a query of the type "table.column" inside a function?

I created a CTE, the second part of the CTE contains a select with a st_contains. In this one, i've two columns of diferent tables with the same name. I want to alias one of this 'table.column' combination becouse when I do the selection at the end of the CTE outputs an error; column reference " " is ambiguous.
with
table1 as (
...
),
table2 as (
select*
from table3, table4
where st_contains (table3.atribute1, table4.atribute1)
)
select
table1.atribute1
table2.atribute1 #here i need somethig like table2.table3.atribute1
from table1
join table2 on table1.atribute2=table2.atribute2
;
I hope i explined the problem well.
Thanks!
Alias the table3 and table4 columns in your table2 CTE to resolve the ambiguity.
with
table1 as (
...
),
table2 as (
select table3.attribute1 table3atrr1, table4.attribute1 table4attr1
from table3, table4
where st_contains (table3.atribute1, table4.atribute1)
)
select
table1.atribute1
table2.table3atrr1 -- use the aliased column name
from table1
join table2 on table1.atribute2=table2.atribute2
;

How get field from set of fiels defined in CTE field alias?

with t as (
select
(select t1 from table1 t1 limit 1) t,
'foo' x
)
select
t.id, t.code, x
from t
How can i get t.id and t.code in result query?
You need to add an extra parenthesis to go within the resultset.
I have renamed your CTE to distinguish that the parenthesis refers to the table selected within the CTE, and not the CTE itself.
with cte as (
select
(select t1 from table1 t1 limit 1) t,
'foo' x
)
select
(t).id, (t).code, x
from cte

POSTGRESQL ERROR:more than one row returned by a subquery used as an expression

SELECT
(SELECT vch_message
FROM TABLE1
WHERE vch_message like 'DONE%'
) as Status,
(SELECT vch_Type
FROM TABLE2
WHERE vch_Type like 'Account'
) as Type
From Table3
where Condition
Why not :
SELECT
Status.vch_message,
Type.vch_Type
From
Table3,
(
SELECT
vch_message
FROM
TABLE1
WHERE
vch_message like 'DONE%'
)
as Status,
(
SELECT
vch_Type
FROM
TABLE2
WHERE
vch_Type like 'Account'
)
as Type
WHERE
Condition

Is it possible to reference the same CTE in more than one unrelated query?

I have this CTE1 that I'd like to reference it in 2 or more unrelated queries
WITH CTE1
AS
(
SELECT col1, col2, col3
FROM Table1
WHERE condition
)
SELECT * FROM CTE1;
SELECT * FROM CTE1 c // This is not working -- Invalid object name 'CTE1'.
JOIN TABLE2 t
ON c.colx = t.xolx;
Is there a way to accomplish this?
Thanks for helping

t-sql WITH on WITH

I have to make query on WITH query, something like
; WITH #table1
(
SELECT id, x from ... WHERE....
UNION ALL
SELECT id, x from ... WHERE...
)
WITH #table2
(
SELECT DISTINCT tbl_x.*,ROW_NUMBER() OVER (order by id) as RowNumber
WHERE id in ( SELECT id from #table1)
)
SELECT * FROM #table2 WHERE RowNumber > ... and ...
So I have to use WITH on WITH and then SELECT on second WITH, How I can do that?
You can define multiple CTEs after the WITH keyword by separating each CTE with a comma.
WITH T1 AS
(
SELECT id, x from ... WHERE....
UNION ALL
SELECT id, x from ... WHERE...
)
, T2 AS
(
SELECT DISTINCT tbl_x.*, ROW_NUMBER() OVER (order by id) as RowNumber
WHERE id in ( SELECT id from T1 )
)
SELECT * FROM T2 WHERE RowNumber > ... and ...
https://web.archive.org/web/20210927200924/http://www.4guysfromrolla.com/webtech/071906-1.shtml