Как добавить в select переменную из declare в pgsql

Есть переменная data в которую мы помещаем некий jsonb. Как мне использовать data в выборке. Постгрес ругается ERROR: column reference \"data\" is ambiguous. Может есть какой то указатель, что переменная именно из declare.

select jsonb_agg(
            jsonb_build_object(
                'document_title_id',dt.document_title_id,
                'distr',c.customer,
                'count_pos',ttc.count_pos,
                'sum_opt_w_nds',ttc.summ_pos,
                'customer', c1.customer,
                'address_f', c1.address_f,
                'data', json_agg(data)
                )
            ) 

Весь код процедуры.

declare 
    data jsonb;
    title jsonb;
begin
    create temp table tmp_data(data jsonb) on commit drop;
    create temp table tmp_table_count (document_title_id bigint, count_pos int, summ_pos numeric) on commit drop;

    insert into tmp_data(data)
    select v.value
    from jsonb_array_elements((param->>'doc_list')::jsonb) v;

    insert into tmp_table_count(document_title_id,count_pos,summ_pos)
    select 
        dd.document_title_id,
        count(dd.document_data_id),
        sum(dd.sum_opt_w_nds)
    from public.document_data dd
    where not dd.disable and dd.document_title_id  in (
        select
        (td.data->>'id')::bigint
        from tmp_data td
    ) group by dd.document_title_id;


    select jsonb_agg(
            jsonb_build_object(
                'document_data_id',dd.document_data_id,
                'document_title_id',dd.document_title_id,
                'tovname',r.tovname,
                'fabr', r.fabr,
                'seria',dd.seria,
                'fakt_seria',ad.fakt_seria,
                'srok_g',dd.srok_g,
                'fakt_srok_g',ad.fakt_srok_g,
                'price_opt_w_nds',dd.price_opt_w_nds,
                'qnt',dd.qnt,
                'fakt_qnt',COALESCE(ad.fakt_qnt,'0'),
                'barcode_fabr',dd.barcode_fabr,
                'defekt', ad.defekt,
                'doc_date', dt.doc_date
            )
        )
     from public.document_data dd
    left join public.v_registry r on r.reg_id = dd.reg_id
    left join public.document_title dt on dt.document_title_id = dd.document_title_id
    left join public.apn_data ad on ad.document_data_id = dd.document_data_id
    where dd.document_title_id in (
        select
        (td.data->>'id')::bigint
        from tmp_data td) and not dd.disable into data;

    select jsonb_agg(
        jsonb_build_object(
            'document_title_id',dt.document_title_id,
            'distr',c.customer,
            'count_pos',ttc.count_pos,
            'sum_opt_w_nds',ttc.summ_pos,
            'customer', c1.customer,
            'address_f', c1.address_f,
            'data', json_agg(data)
            )
        ) 
    from tmp_data td
    left join public.document_title dt on  dt.document_title_id = (td.data->>'id')::bigint
    left join public.customer c on c.customer_id = dt.distr_id
    left join public.customer c1 on c1.customer_id = dt.customer_id
    left join tmp_table_count ttc on ttc.document_title_id = dt.document_title_id
    where not dt.disable into title;


 return jsonb_build_object('title',title);

Ответы (0 шт):