

  select t1."Oid" AS "Oid", 
         t1."RegistryHeader" AS "RegistryHeader", 
         t1."AmountHUF" AS "AmountHUF", 
         t1."AmountDEV" AS "AmountDEV", 
         t1."DatePayment" AS "DatePayment", 
         t1."IsBlocked" AS "IsBlocked", 
         t1."Note" AS "Note", 
         t1."BankSavedFileName" AS "BankSavedFileName",
         coalesce(t3."BallanceHUF",t2."F_AmountBruttoHUF") AS "BallanceHUF",
         coalesce(t3."BallanceDEV",t2."F_AmountBruttoDEV") AS "BallanceDEV",
         t1."AmountHUF"-coalesce(t3."BallanceHUF",t2."F_AmountBruttoHUF") AS "DifferenceHUF",
         t1."AmountDEV"-coalesce(t3."BallanceDEV",t2."F_AmountBruttoDEV") AS "DifferenceDEV",
         case when round(cast(coalesce(t3."BallanceHUF",t2."F_AmountBruttoHUF") as numeric),2)=0 then 0
              when coalesce(t3."BallanceHUF",t2."F_AmountBruttoHUF")>=t1."AmountHUF" then t1."AmountHUF"
              when coalesce(t3."BallanceHUF",t2."F_AmountBruttoHUF")<=t1."AmountHUF" then coalesce(t3."BallanceHUF",t2."F_AmountBruttoHUF")
         end AS "OfferedHUF",
         case when round(cast(coalesce(t3."BallanceDEV",t2."F_AmountBruttoDEV") as numeric),2)=0 then 0
              when coalesce(t3."BallanceDEV",t2."F_AmountBruttoDEV")>=t1."AmountDEV" then t1."AmountDEV"
              when coalesce(t3."BallanceDEV",t2."F_AmountBruttoDEV")<=t1."AmountDEV" then coalesce(t3."BallanceDEV",t2."F_AmountBruttoDEV")
         end AS "OfferedDEV",
	 t1."BankPrepareName" AS "BankPrepareName"                     
  from "RegistryBankTransfer" t1 
       join "RegistryHeader" t2 on t1."RegistryHeader"=t2."Oid" 
       left join "PCIGroup" t3 on t2."CustomersFrom"=t3."CustomersFrom" 
            and t2."Customers"=t3."Customers" and t3."PartnerType"=1 and t2."DocumentNumber"=t3."ConnectInfo"
  where t1."GCRecord" is null and t2."RegistryAcceptState"=0

