Postgres. Блок из нескольких записей в одну, с повторениями в view таблице
Пардон за заголовок, лучше придумать не смог.
Есть таблица в которой хранятся данные (допустим по 9 строк, которые относятся к одной опции group_cls_id)
Количество строк на опцию, пока что, не меняется и будет 9, но что будет в дальнейшем не знаю. Для более удобного вывода в фронт, сделал вью таблицу и спользовал такой скрипт
create or replace view group_options_history
as
select row_number() over () AS ID,
c.id as classifier ,
s.id as shed ,
o.id as organization ,
av.active_from ,
av.active_to,
max (case when a.code = 'FALL_PERCENT' then cast(av.value as text) end) as fall,
max (case when a.code = 'DEFECT_PERCENT' then cast(av.value as text) end) as
defect_percentage,
max (case when a.code = 'DEFECT_AGE' then cast(av.value as text) end) as defect_age,
max (case when a.code = 'WEIGHT_GAIN' then cast(av.value as text) end) as weight_gain,
max (case when a.code = 'DAY_EXPENSES' then cast(av.value as text) end) as
day_expenses,
max (case when a.code = 'AGE' then cast(av.value as text) end) as group_age,
max (case when a.code = 'INCREASE_IN_VALUE' then cast(av.value as text) end) as
group_value
from attribute_value_history av
join "attributes" a on a.id = av.atb_id
join "classifiers" c on c.id = av.cls_id
join "sheds" s on s.id = av.shed_id
join "organizations" o on o.id = av.org_id
where a.code in ('FALL_PERCENT',
'DEFECT_PERCENT',
'DEFECT_AGE',
'WEIGHT_GAIN',
'DAY_EXPENSES',
'AGE',
'INCREASE_IN_VALUE')
group by c.id , s.id , o.id , av.active_from , av.active_to
;
И вопрос. Что в это скрипт надо добавить, чтобы в вью таблице появились (в данном случае) 3 записи, а не одна как у меня?



