IF EXISTS (SELECT * FROM SYS.VIEWS WHERE NAME ='V_PDV_VD_DEV_EST_PROD_IMP')
BEGIN 
	DROP VIEW V_PDV_VD_DEV_EST_PROD_IMP
END 
GO 

CREATE VIEW [DBO].[V_PDV_VD_DEV_EST_PROD_IMP]
AS
 SELECT	 PDV_VD_DEV_EST_PROD_IMP.CD_EMP
		,PDV_VD_DEV_EST_PROD_IMP.CD_FILIAL
		,PDV_VD_DEV_EST_PROD_IMP.CD_VD
		,PDV_VD_DEV_EST_PROD_IMP.CD_PROD
		,PDV_VD_DEV_EST_PROD_IMP.CD_VEND
		,SUM(EST_PROD_IMP.QT_IMP) AS QT_DEVO
		,PDV_VD_DEV_EST_PROD_IMP.CD_IT
   FROM PDV_VD_DEV_EST_PROD_IMP
        INNER JOIN EST_PROD_IMP ON PDV_VD_DEV_EST_PROD_IMP.CD_EMP    = EST_PROD_IMP.CD_EMP
							   AND PDV_VD_DEV_EST_PROD_IMP.CD_FILIAL = EST_PROD_IMP.CD_FILIAL
							   AND PDV_VD_DEV_EST_PROD_IMP.CD_IMP    = EST_PROD_IMP.CD_IMP
							   AND PDV_VD_DEV_EST_PROD_IMP.CD_PROD   = EST_PROD_IMP.CD_PROD
GROUP BY PDV_VD_DEV_EST_PROD_IMP.CD_EMP
	    ,PDV_VD_DEV_EST_PROD_IMP.CD_FILIAL
	    ,PDV_VD_DEV_EST_PROD_IMP.CD_VD
	    ,PDV_VD_DEV_EST_PROD_IMP.CD_PROD
	    ,PDV_VD_DEV_EST_PROD_IMP.CD_VEND
	    ,PDV_VD_DEV_EST_PROD_IMP.CD_IT
