Необходимо скорректировать запрос в соответствии с незаявленными ранее условиями + убрать закомментированные строки + заменить константы дат на параметры
with params as (
select '2026-01-01'::date as start_date
, '2026-03-31'::date + interval '1' day as end_date
)
select dfa.id as dfa_id
, dfa.ticker_symbol
, hist.status
, dfa_category.digital_asset_category_description as digital_asset_category
, g.guarantee_description as guarantee
, decision.type_of_law
, decision.issue_currency
, (hist.status in ('emitment_success', 'mature',
'waiting_for_mature', 'mature_confirmed')
and (hist.dt_end_status >= params.end_date
or hist.dt_end_status is null))
or (hist.status = 'redeemed'
and hist.dt_start_status >= params.start_date
and hist.dt_start_status < params.end_date
and to_timestamp(decision.issue_date)::date >= params.start_date
and to_timestamp(decision.issue_date)::date <= params.end_date
) as is_placed
, hist.status = 'issue_failure' as is_not_completed
, exists(
select 1 from release_status_history h
where h.ticker_symbol = dfa.ticker_symbol
and h.status = 'emitment_success'
and h.dt_start_status >= params.start_date
and h.dt_start_status < params.end_date
) as is_completed_in_quarter
, decision.price_per_dfa / 100.0 as price_per_dfa
, dfa.min_supply_invested
, dfa.max_supply_invested
--НОВОЕ ПОЛЕ ДЛЯ СТОЛБЦОВ F и L (стоимость выпусков)
, decision.original_amount
, (
select d.price_per_dfa / 100.0
from deals d
where d.asset_ticker_symbol = dfa.ticker_symbol
and d.type = 'secondary'
and d.executed_at < params.end_date
and d.executed_at = (
select max(d2.executed_at)
from deals d2
where d2.asset_ticker_symbol = dfa.ticker_symbol
and d2.type = 'secondary'
and d2.executed_at < params.end_date
)
and params.end_date::date - d.executed_at::date <= 90
limit 1
) as price_secondary_less90
, (
select sum(d.total_price * d.amount_dfa / 100.0)
from deals d
where d.asset_ticker_symbol = dfa.ticker_symbol
and d.to_user_uuid = d.emitter_id
and d.type in ('offer', 'secondary', 'transfer')
and d.executed_at < params.end_date
) as outstand_dfa_price
from params, release_dfas dfa
inner join release_decisions decision on decision.id = dfa.decision_id
inner join lateral (
select h.status, h.dt_start_status, h.dt_end_status
from release_status_history h
where h.ticker_symbol = dfa.ticker_symbol
and h.dt_start_status <= params.end_date
and (h.dt_end_status >= params.end_date
or h.dt_end_status is null)
order by h.dt_start_status desc
limit 1
) hist on true
left join digital_assets_categories dfa_category
on dfa_category.digital_asset_category = decision.digital_asset_category
left join guarantees g on g.guarantee = decision.guarantee
order
by dfa.ticker_symbol
;
ДОПОЛНИТЕЛЬНО:
Для столбцов F и L необходимо рассчитывать значение (decision.original_amount*decision.price_per_dfa / 100.0)