我有4個查詢,每一個具有CTE開始:如何用單個cte得到結果?
with stg as
(
select columns
from stage
where conditions1
)
這種差異開始後。在第一種情況下,我需要在某些條件下加入兩張桌子
select cols
from stg
inner join table1 on cond2
inner join table2 on cond3
where con4
第二。我需要加入到表2,但在不同的條件下
select cols
from stg
inner join table2 on cond5
where cond6
在第三,我只需要適合cond7
select cols
from stg
where cond7
在第四,再次不同的表和電導率
所有列select cols
from stg
left join table3 on cond8
where cond9
事情是,每次查詢後我需要將更改的數據返回。它基本上是
insert into table stage
select *
from query(1-4)
所以我不能鏈這個CTEs。並且在每次插入之後,cte返回更少的行。例如,
第一次它將返回100行,第二次返回85,依此類推。我想要的只是一次使用CTE的方式。起初我還以爲要改變全部加入到左的連接,將它們添加到CTE本身並創造各種
with stg as
(
select
cols,
table1.col as flag1,
table2.col as flag2,
table3.col as flag3
from
stage
left join
table1 on cond
left join
table2 on cond
left join
table3 on cond
where
many conditions
)
的一個標誌,但這個變得非常凌亂,非常迅速,甚至沒有工作的權利。然後我想說的是,如果它們存在,實際上將它鏈接起來,然後聯合4個CTE來擺脫雙打。但這似乎也不是一個明智的選擇。還有其他的變種嗎?
已投票結束。這個問題很混亂。 CTE的使用對結果中的重複有什麼影響?將其包含在子查詢中並使用不同的 –
您的示例太抽象了,並沒有對您的問題提供多少幫助。使用示例數據進行更真實的查詢會很有幫助。 –