
            select
                sum(COALESCE(p.Kolvo*p.Price, 0)) as "DrugRequestPersonOrder_Summa",

                sum(case when p.DrugFinance_id = '3' then COALESCE(p.Kolvo*p.Price, 0) else 0 end) as "DrugRequestPersonOrder_FedSumma",

                sum(case when p.DrugFinance_id = '27' then COALESCE(p.Kolvo*p.Price, 0) else 0 end) as "DrugRequestPersonOrder_RegSumma"

            from (
                select
                    drug_list.DrugComplexMnn_id,
                    drug_list.Tradenames_id,
                    drug_list.DrugFinance_id,
                    drug_list.Kolvo,
                    dlr.Price
                from
                    v_DrugRequestPersonOrder drpo

                    LEFT JOIN LATERAL (

                        select
                            i_drpo.DrugComplexMnn_id,
                            i_drpo.Tradenames_id,
                            i_drr.DrugFinance_id,
                            sum(i_drpo.DrugRequestPersonOrder_OrdKolvo) as Kolvo
                        from
                            v_DrugRequestPersonOrder i_drpo

                            LEFT JOIN LATERAL (

                                select
                                    ii_drr.DrugFinance_id
                                from
                                    v_DrugRequestRow ii_drr

                                where
                                    ii_drr.DrugRequest_id = '500801000001628' and
                                    COALESCE(ii_drr.DrugComplexMnn_id, 0) = COALESCE(i_drpo.DrugComplexMnn_id, 0) and

                                    COALESCE(ii_drr.TRADENAMES_id, 0) = COALESCE(i_drpo.Tradenames_id, 0)

                                order by
                                    ii_drr.DrugFinance_id desc, ii_drr.DrugRequestRow_id
                            ) i_drr ON true
                        where
                            i_drpo.DrugRequest_id = '500801000001628' and
                            i_drpo.Person_id = drpo.Person_id and
                            i_drpo.DrugRequestPersonOrder_OrdKolvo > 0 and
                            (
                                i_drpo.DrugComplexMnn_id is not null or
                                i_drpo.Tradenames_id is not null
                            )
                        group by
                            i_drpo.DrugComplexMnn_id,
                            i_drpo.Tradenames_id,
                            i_drr.DrugFinance_id
                    ) drug_list ON true
                    LEFT JOIN LATERAL (

                        select
                            (case
                                when i_dlrt.DrugRequest_Price > 0 then i_dlrt.DrugRequest_Price
                                else i_dlr.DrugListRequest_Price
                            end) as Price
                        from
                            v_DrugListRequest i_dlr

                            left join v_DrugListRequestTorg i_dlrt  on i_dlrt.DrugListRequest_id = i_dlr.DrugListRequest_id and i_dlrt.TRADENAMES_id = drug_list.Tradenames_id

                        where
                            i_dlr.DrugComplexMnn_id = drug_list.DrugComplexMnn_id
                                and (
                                ('500801000000024' is not null and i_dlr.DrugRequestProperty_id  = '500801000000024') or
                                (drug_list.DrugFinance_id = '3' and i_dlr.DrugRequestProperty_id  = NULL) or
                                (drug_list.DrugFinance_id = '27' and i_dlr.DrugRequestProperty_id  = NULL)
                            )
                        order by
                            DrugListRequest_insDT desc
                        limit 1
                    ) dlr ON true
                where
                    drpo.DrugRequest_id = '500801000001628' and
                    drpo.DrugComplexMnn_id is null and
                    drpo.Tradenames_id is null
            ) p;
        
