0

I wrote the follow recursive query that gives me the list of IDs that I need and it is working just fine:

 with rec_table (emailid) as
 ((select email.id  as emailid from email, addressee, address, use, user 
   where email.id = addressee.EMAIL and addressee.to = address.ID 
   and address.id = use.ADDRESS and use.user = user.id and user.id = user_t(121))
 union
 (select email.id as emailid from email, address, use, user 
  where email.from = address.id 
  and address.ID = use.ADDRESS and use.user = user.id and user.id = user_t(121))

union all

select email.id as emailid from email, rec_table as x
 where email.contained_in = x.emailid)

select * from rec_table

However, if I need to do something more elaborated with the result of the query, like in the query below, I received an error

 with rec_table (emailid) as
 ((select email.id  as emailid from email, addressee, address, use, user 
   where email.id = addressee.EMAIL and addressee.to = address.ID 
   and address.id = use.ADDRESS and use.user = user.id and user.id = user_t(121))
 union
 (select email.id as emailid from email, address, use, user 
  where email.from = address.id 
  and address.ID = use.ADDRESS and use.user = user.id and user.id = user_t(121))

union all

select email.id as emailid from email, rec_table as x 
where email.contained_in = x.emailid)

select user.id, address.id from rec_table as x, addressee, address, use, user  
where  x.emailid = addressee.EMAIL and addressee.to = address.ID 
and address.id = use.ADDRESS and use.user = user.id

I received sql error code -340:

SQL Error [42726]: The common table expression "" has the same identifier as another occurrence of a common table expression definition within the same statement.. SQLCODE=-340, SQLSTATE=42726, DRIVER=4.16.53 SQL Error [56098]: An error occurred during implicit system action type "2". Information returned for the error includes SQLCODE "-340", SQLSTATE "42726" and message tokens "".. SQLCODE=-727, SQLSTATE=56098, DRIVER=4.16.53 The common table expression "" has the same identifier as another occurrence of a common table expression definition within the same statement.. SQLCODE=-340, SQLSTATE=42726, DRIVER=4.16.53

Any ideas?

0

1 Answer 1

0

Try moving the recursion initialization part into its own CTE:

with blah (emailid) as 
 ((select email.id  as emailid from email, addressee, address, use, user 
   where email.id = addressee.EMAIL and addressee.to = address.ID 
   and address.id = use.ADDRESS and use.user = user.id and user.id = user_t(121))
 union
 (select email.id as emailid from email, address, use, user 
  where email.from = address.id 
  and address.ID = use.ADDRESS and use.user = user.id and user.id = user_t(121))
),
rec_table (emailid) as (

  select emailid from blah
 union all
  select email.id as emailid from email, rec_table as x 
  where email.contained_in = x.emailid)

select user.id, address.id from rec_table as x, addressee, address, use, user  
where  x.emailid = addressee.EMAIL and addressee.to = address.ID 
and address.id = use.ADDRESS and use.user = user.id

NB: not tested.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.