История изменений
Исправление alex_the_v, (текущая версия) :
Блин, сломал пока туда-сюда гонял, заодно немного покрасивее переделал. Вот правильный вариант:
with work_aggr as (
select wp.workotdel_id,
sum(wp.valuefact) as valuefact_sum
from workpeople wp
group by wp.workotdel_id
)
select wu.id,
wu.name,
coalesce(wa.valuefact_sum, wo.valuefact) as valuefact
from workunit wu
inner join workotdel wo on wo.workunit_id = wu.id
left join work_aggr wa on wa.workotdel_id = wo.id and wu.manualinput = true;
Исправление alex_the_v, :
Блин, сломал пока туда-сюда гонял. Вот правильный вариант:
with work_aggr as (
select wp.workotdel_id,
sum(wp.valuefact) as valuefact_sum
from workpeople wp
group by wp.workotdel_id
)
select wu.id,
wu.name,
case wu.manualinput
when true then wo.valuefact
else wa.valuefact_sum
end as valuefact
from workunit wu
inner join workotdel wo on wo.workunit_id = wu.id
left join work_aggr wa on wa.workotdel_id = wo.id;
Исправление alex_the_v, :
Блин, сломал пока туда-сюда гонял. Вот правильный вариант:
with work_aggr as (
select wp.workotdel_id,
sum(wp.valuefact) as valuefact_sum
from workpeople wp
group by wp.workotdel_id
)
select wu.id,
wu.name,
case wu.manualinput
when true then wo.valuefact
else wa.valuefact_sum
end as valuefact
from workunit wu
inner join workotdel wo on wo.workunit_id = wu.id
inner join work_aggr wa on wa.workotdel_id = wo.id;
Исходная версия alex_the_v, :
Блин, сломал пока туда-сюда гонял. Вот правильный вариант:
with work_aggr as (
select wp.workotdel_id,
sum(wp.valuefact) as valuefact_sum
from workpeople wp
group by wp.workotdel_id
)
select wu.id,
wu.name,
case wu.manualinput
when true then wo.valuefact
else wa.valuefact_sum
end
from workunit wu
inner join workotdel wo on wo.workunit_id = wu.id
inner join work_aggr wa on wa.workotdel_id = wo.id;