|
-- Vypis cisla od nula do 100 |
|
-- kostra pre kazdy priklad |
|
with recursive sum() as ( |
|
select |
|
union |
|
select |
|
) |
|
select * from sum |
|
|
|
-- riesenie priklady, n je VYSTUP funkcie, nie vstup |
|
with recursive sum(n) as ( |
|
-- inital term |
|
select 1 |
|
|
|
union -- toto je len syntax, ktora oddeluje inital term od recursive |
|
|
|
-- recursive term |
|
select n+1 from sum where n < 100 |
|
) |
|
select * from sum |
|
|
|
|
|
-- faktorial cisel po 10 |
|
with recursive faktorial(n, fakt) as ( |
|
select 0, 1 |
|
|
|
union |
|
|
|
select n+1, (n+1)*fakt from faktorial where n < 10 |
|
) |
|
select * from faktorial |
|
|
|
-- autobus zo zastavky "Nad luckami" |
|
with recursive path(hop, line, start_stop_id, end_stop_id, start_name, end_name, route, transfers) as ( |
|
select 1, c.line, c.start_stop_id, c.end_stop_id, ss.name, es.name, array[ss.name, es.name], array[c.line] |
|
from connections c |
|
join stops ss on ss.id = c.start_stop_id |
|
join stops es on es.id = c.end_stop_id |
|
where ss.name = 'Nad lúčkami' |
|
|
|
union |
|
|
|
select p.hop + 1, c.line, c.start_stop_id, c.end_stop_id, ss.name, es.name, p.route || es.name, |
|
case when c.line = any(p.transfers) |
|
then p.transfers |
|
else p.transfers || c.line |
|
end |
|
from path p |
|
join connections c on p.end_stop_id = c.start_stop_id |
|
join stops ss on ss.id = c.start_stop_id |
|
join stops es on es.id = c.end_stop_id |
|
|
|
where not es.name = any(p.route) |
|
and p.line = c.line or not c.line = any(p.transfers) |
|
and ss.name != 'Zochova' |
|
and array_length(p.transfers, 1) < 4 |
|
and c.line not like 'N%' |
|
) |
|
select * from path p where p.end_name = 'Zochova' |
|
order by array_length(p.transfers, 1) |
|
limit 100 |
|
|
|
|
|
|
|
------------------------------------------- |
|
with recursive nabytok(p_id, part) as ( |
|
select id, name from product_parts where part_of_id in |
|
(select id from product_parts where name = 'chair') |
|
|
|
union |
|
|
|
select p.id, p.name from nabytok n |
|
join product_parts p on p.part_of_id = n.p_id |
|
) |
|
select part from nabytok |
|
|
|
|
|
|
|
|
|
with recursive nabytok(p_id, part, st) as ( |
|
select id, name, shipping_time as st from product_parts where part_of_id in |
|
(select id from product_parts where name = 'chair') |
|
|
|
union |
|
|
|
select p.id, p.name, p.shipping_time as st from nabytok n |
|
join product_parts p on p.part_of_id = n.p_id |
|
) |
|
select part, st from nabytok |
|
order by st desc |
|
limit 1 |
|
|
|
|
|
|
|
|
|
|
|
select (with recursive nabytok(p_id, part, st) as ( |
|
select id, name, shipping_time as st from product_parts where part_of_id in |
|
(select id from product_parts where name = 'chair') |
|
|
|
union |
|
|
|
select p.id, p.name, p.shipping_time as st from nabytok n |
|
join product_parts p on p.part_of_id = n.p_id |
|
|
|
) |
|
select sum(st) from nabytok |
|
where p_id not in (select a.part_of_id from product_parts a where a.part_of_id is not NULL) |
|
) |