Reputation: 2174
I have a sub query and I need to join it with itself. It looks something like this :
SELECT
*
FROM
( ..-the same sub query-.. ) SQ1,
( ..-the same sub query-.. ) SQ2,
( ..-the same sub query-.. ) SQ3
WHERE
..-some joins between SQ1, SQ2, SQ3-..
Is there anyway to resolve this issue of not writing the same query multiple times. (The original query is too long to post but essentially I need to do this)
Thanks in advance.
Upvotes: 1
Views: 1361
Reputation: 142720
Try the WITH factoring clause, such as
with sq as
(select ... from ... where)
select *
from sq sq1,
sq sq2,
sq sq3
where sq1.id = sq2.id
and ...
Upvotes: 4