CREATE OR REPLACE VIEW v_tp_fin_group_link AS
 SELECT t1.tpfingroupid::character varying(36)::text || a1.tpfingroupid::character varying(36)::text AS tpfingrouplinkid,
    t1.tpfingroupid AS tpfingroup,
    a1.tpfingroupid AS linktpfingroup
   FROM fies.t_tp_fin_group t1
     JOIN fies.t_tp_fin_type_link t2 ON t2.tpfintypelinkid = t1.tpfintypelink
     JOIN fies.t_tp_fin_type t3 ON t3.tpfintypeid = t2.tpfintype
     JOIN fies.t_tp_link t4 ON t4.tpfintype = t3.tpfintypeid
     JOIN ( SELECT t10.tpfingroupid,
            t10.groupsum,
            t10.tpfin,
            t10.tpfintypelink,
            t10.basetariff,
            t10.soulcdintcount,
            t10.correctioncoefficent,
            t14.tptype
           FROM fies.t_tp_fin_group t10
             JOIN fies.t_tp_fin_type_link t12 ON t12.tpfintypelinkid = t10.tpfintypelink
             JOIN fies.t_tp_fin_type t13 ON t13.tpfintypeid = t12.tpfintype
             JOIN fies.t_tp_link t14 ON t14.tpfintype = t13.tpfintypeid) a1 ON a1.tpfin = t1.tpfin AND a1.tpfingroupid <> t1.tpfingroupid AND a1.tptype = t4.tptype;
