

   SELECT MAX(CAST(t1."Oid" AS CHAR(100))) As "Oid",
          t1."CustomersFrom" AS "CustomersFrom",
          t1."Customers" AS "Customers",
          t1."PartnerType" AS "PartnerType",
          t1."ConnectInfo" AS "ConnectInfo",
          max(t2."ShortName") AS "Currency",
          sum(t1."DebitTotalHUF") AS "DebitTotalHUF",
          SUM(t1."CreditTotalHUF") AS "CreditTotalHUF",
          sum(t1."DebitTotalDEV") AS "DebitTotalDEV",
          SUM(t1."CreditTotalDEV") AS "CreditTotalDEV",
          SUM(t1."BallanceHUF") AS "BallanceHUF",
          SUM(t1."BallanceDEV") AS "BallanceDEV",
          case when t1."PartnerType"=1 then SUM(t1."BallanceHUF")*-1 else 0 end AS "BallanceHUF_P",
          case when t1."PartnerType"=1 then SUM(t1."BallanceDEV")*-1 else 0 end AS "BallanceDEV_P",
          case when t1."PartnerType"=0 then SUM(t1."BallanceHUF") else 0 end AS "BallanceHUF_R",
          case when t1."PartnerType"=0 then SUM(t1."BallanceDEV") else 0 end AS "BallanceDEV_R",
          (case when t1."PartnerType"=0 then SUM(t1."BallanceHUF") else 0 end) - 
               (case when t1."PartnerType"=1 then SUM(t1."BallanceHUF")*-1 else 0 end) AS "BallanceHUF_B",
          (case when t1."PartnerType"=0 then SUM(t1."BallanceDEV") else 0 end) - 
               (case when t1."PartnerType"=1 then SUM(t1."BallanceDEV")*-1 else 0 end) AS "BallanceDEV_B",
          max(t3."ShortName") AS "Currency_Account",
          sum(t1."AmountHUF_Account") AS "AmountHUF_Account",
          sum(t1."AmountDEV_Account") AS "AmountDEV_Account",
          case when max(t3."ShortName")='HUF' then 
               case when round(cast(SUM(t1."BallanceHUF") as numeric),0)=0 then 1
               else 0 
               end 
	  else  
               case when round(cast(SUM(t1."BallanceDEV") as numeric),2)=0 then 1
               else 0 
               end
          end AS "BallanceState",
          case when max(t3."ShortName")='HUF' then -1
               when t1."CurrencyName_Account" IS null then -1
               else
                   case when round(cast(SUM(t1."BallanceDEV")as numeric),2)=0 and round(cast(SUM(t1."BallanceHUF")as numeric),0)<>0 then 1
                        else 0 
                   end
          end "BallanceRateDiff",                  
          max(t4."DateExecution") DateExecution_Account,
          MAX(t4."DateCreated") DateCreated_Account                  
   from "vv_PartnerConnectionInfoDetail" t1 
        left join "CurrencyName" t2 on t1."CurrencyName"=t2."Oid" 
        left join "CurrencyName" t3 on t1."CurrencyName_Account"=t3."Oid"
        left join "vv_PartnerConnectionInfoAccount" t4 on 
        t1."CustomersFrom"=t4."CustomersFrom" and
        t1."Customers"=t4."Customers" and
        t1."PartnerType"=t4."PartnerType" and
        t1."ConnectInfo"=t4."ConnectInfo" and
        t4."MainType"='01-Account'
        
   group by t1."CustomersFrom",t1."Customers",t1."PartnerType",t1."ConnectInfo",t1."CurrencyName_Account"