USE [SISTEST]
GO

/****** Object:  View [dbo].[vv_PartnerConnectionInfoDetailGroup]    Script Date: 05/18/2012 09:19:51 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



CREATE view [dbo].[vv_PartnerConnectionInfoDetailGroup]
as
   select MAX(convert(varchar(100),t1.Oid)) Oid,
          t1.CustomersFrom CustomersFrom,
          t1.Customers Customers,
          t1.PartnerType PartnerType,
          t1.ConnectInfo ConnectInfo,
          max(t2.ShortName) Currency,
          sum(t1.DebitTotalHUF) DebitTotalHUF,
          SUM(t1.CreditTotalHUF) CreditTotalHUF,
          sum(t1.DebitTotalDEV) DebitTotalDEV,
          SUM(t1.CreditTotalDEV) CreditTotalDEV,
          SUM(t1.BallanceHUF) BallanceHUF,
          SUM(t1.BallanceDEV) BallanceDEV,
          case when t1.PartnerType=1 then SUM(t1.BallanceHUF)*-1 else 0 end BallanceHUF_P,
          case when t1.PartnerType=1 then SUM(t1.BallanceDEV)*-1 else 0 end BallanceDEV_P,
          case when t1.PartnerType=0 then SUM(t1.BallanceHUF) else 0 end BallanceHUF_R,
          case when t1.PartnerType=0 then SUM(t1.BallanceDEV) else 0 end 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) 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) BallanceDEV_B,
          max(t3.ShortName) Currency_Account,
          sum(t1.AmountHUF_Account) AmountHUF_Account,
          sum(t1.AmountDEV_Account) AmountDEV_Account,
          case when max(t3.ShortName)='HUF' then 
               case when round(SUM(t1.BallanceHUF),0)=0 then 1
               else 0 
               end 
          else  
               case when round(SUM(t1.BallanceDEV),2)=0 then 1
               else 0 
               end
          end BallanceState,
          case when max(t3.ShortName)='HUF' then -1
               when t1.CurrencyName_Account IS null then -1
          else
               case when round(SUM(t1.BallanceDEV),2)=0 and round(SUM(t1.BallanceHUF),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


GO

