Хабр Курсы для всех
РЕКЛАМА
Практикум, Хекслет, SkyPro, авторские курсы — собрали всех и попросили скидки. Осталось выбрать!
with bottle(id, ingredient, measure, quantity) as (
select 1, 'Банан', 'Штука', 3 from dual union all
select 2, 'Петрушка', 'Ветка', 2 from dual union all
select 3, 'Вода', 'Литр', 3 from dual union all
select 4, 'Соль', 'Ложка', 1 from dual union all
select 5, 'Уксус', 'Ложка', 2 from dual
)
select *
from bottle
model
partition by (id)
dimension by(1 n)
measures(ingredient, measure, quantity)
rules(
ingredient[for n from 1 to quantity[1] increment 1] = ingredient[1]
,measure [for n from 1 to quantity[1] increment 1] = measure [1]
,quantity [for n from 1 to quantity[1] increment 1] = quantity [1]
)
<code> / Делаю табличку. Только первые буквы беру для простоты q)t:([] i:`b`p`w`s`u; m:`sh`ve`li`lo`lo; q:3 2 3 1 2) q)t i m q ------ b sh 3 p ve 2 w li 3 s lo 1 u lo 2 / решение q)ungroup update til each q from t i m q ------ b sh 0 b sh 1 b sh 2 p ve 0 p ve 1 w li 0 w li 1 w li 2 s lo 0 u lo 0 u lo 1 </code>
SELECT ingredient, measure, 1 AS quantity
FROM (select t.*,generate_series(1,t.quantity,1) from bottle AS t) AS t;
select b.ingredient, b.measure, 1 as quantity
from bottle b
inner join generate_series(1, b.quantity) g (s)
on g.s <= b.quantity
order by b.id;
Перед generate_series подразумевается lateral, но функция и так может обращаться к полям таблицы, указанной в from ранее.select ingredient, measure, 1 as quantity
from bottle, generate_series(1, quantity)
order by id;
Oracle, типичные задачи SQL. Размножение строк таблицы в зависимости от значения числа в колонке