0
create table flight_costs as
with
costs(day, curr, prev) as (
select 1, 20, 0 union
select 1.5, 30, 20 UNION
select 3, 40, 30 UNION
select day + 1, (curr + prev)/2 + ((day + 1) % 7) * 5, curr from costs
where day < 2500 and day > 1
)
select day as day, curr as price from costs;
將返回4999行數據。這對我來說很有意義導致基本情況將是既具有多個基本案例的遞歸SQL
select 1.5, 30, 20 UNION
select 3, 40, 30 UNION
不過,我不明白爲什麼
create table flight_costs as
with
costs(day, curr, prev) as (
select 1, 20, 0 union
select 2, 30, 20 UNION
select 3, 40, 30 UNION
select day + 1, (curr + prev)/2 + ((day + 1) % 7) * 5, curr from costs
where day < 2500 and day > 1
)
select day as day, curr as price from costs;
只會返回我2500點的行,仍然
select 2, 30, 20 UNION
select 3, 40, 30 UNION
應被循環並返回
day 1 2 3 3 4 4 5 5 6....
而不是
day 1 2 3 4 5 6....
但是當我使用'select 3,30,20 UNION選擇5,40,30 UNION'作爲我的兩個基地時,結果日將如同第1天3 5 4 6 5 7 ...消除重複。如果我稍微改變一下數據,它也不會消除即時重複。這似乎很奇怪。我想我錯過了一些重要的東西。 – jen007
1.5不會產生與其他行相同的行,因爲1.5 + 1的任何數量的時間永遠不會產生整數;任何整數+1任意次數永遠不會產生非整數。 – Uueerdo
UNION消除重複結果**行**;只要前一天或當前行在同一天不同,它將允許重複的日期。 '3,30,20'和'5,40,30'不會生成相同的後代。 – Uueerdo