/****** Object:  View [dbo].[V_PG_CRED_VALOR_BAIXADO]    Script Date: 07/04/2013 16:57:47 ******/
IF  EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[V_PG_CRED_VALOR_BAIXADO]'))
DROP VIEW [dbo].[V_PG_CRED_VALOR_BAIXADO]
GO
/*
SELECT 
	*
FROM 
	V_PG_CRED_VALOR_BAIXADO 
WHERE 
	CD_EMP =1 
*/
CREATE VIEW V_PG_CRED_VALOR_BAIXADO
WITH ENCRYPTION
AS
SELECT     
		dbo.PG_CRED.CD_EMP, 
		dbo.PG_CRED.CD_FILIAL, 
		dbo.PG_CRED.CD_PG_CRED,
		dbo.PG_CRED.NR_DP, 
		dbo.PG_CRED.ANO,  
		SUM((dbo.PG_DEB.VL_PGTO+PG_DEB.VL_JUR)-(PG_DEB.VL_DESC + PG_DEB.VLR_DEVOLUCAO)) AS VLR_DUP_BAIXA
FROM         
	dbo.PG_CRED 
	INNER JOIN dbo.PG_DEB ON 
	dbo.PG_CRED.CD_EMP = dbo.PG_DEB.CD_EMP AND 
	dbo.PG_CRED.CD_FILIAL = dbo.PG_DEB.CD_FILIAL AND 
    dbo.PG_CRED.CD_PG_CRED = dbo.PG_DEB.CD_PG_CRED 
GROUP BY
	dbo.PG_CRED.CD_EMP, 
	dbo.PG_CRED.CD_FILIAL, 
	dbo.PG_CRED.NR_DP, 
	dbo.PG_CRED.ANO,
	dbo.PG_CRED.CD_PG_CRED
