 
IF  EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[V_EST_PED_ELTR_FILIAL_ENTR_PROD]'))
DROP VIEW [dbo].[V_EST_PED_ELTR_FILIAL_ENTR_PROD]
GO
 
 
CREATE VIEW [dbo].[V_EST_PED_ELTR_FILIAL_ENTR_PROD]
AS
SELECT DISTINCT 
  dbo.V_EST_PED_ELTR_FORN.CD_EMP, dbo.V_EST_PED_ELTR_FORN.CD_FIL_ENTR, dbo.V_EST_PED_ELTR_FORN.CD_FIL_ENTR AS CD_FILIAL, 
  ISNULL(dbo.V_EST_PED_ELTR_FORN.CD_FORN, 0) AS CD_FORN, ISNULL(dbo.EST_PED_ELTR_FORN_PED_CP.CD_PED, 0) AS CD_PED, 
  ISNULL(dbo.EST_PED_CP_IT.QT_IT, 0) AS QT_IT, dbo.V_EST_PED_ELTR_FORN.CD_PED_SOL, dbo.V_EST_PED_ELTR_FORN.CD_PROD, 
  dbo.V_EST_PED_ELTR_FORN.QT_IT_UN, dbo.V_EST_PED_ELTR_FORN.DT_PED, dbo.V_EST_PED_ELTR_FORN.DT_ENTR, 
  dbo.V_EST_PED_ELTR_FORN.CD_FIL_FAT, dbo.V_EST_PED_ELTR_FORN.STS_PED, dbo.V_EST_PED_ELTR_FORN.DT_ULT_ATU, 
  dbo.V_EST_PED_ELTR_FORN.CD_USU, dbo.V_EST_PED_ELTR_FORN.DT_CAD, dbo.V_EST_PED_ELTR_FORN.VLR_PROD, 
  dbo.V_EST_PED_ELTR_FORN.QT_IT_CP, dbo.V_EST_PED_ELTR_FORN.TP_REQ, V_EST_PED_ELTR_FORN.FLAG_FECHAR,
	V_EST_PED_ELTR_FORN.FLAG_PERC_AUMENTO,
	V_EST_PED_ELTR_FORN.PERC_AUMENTO,
	V_EST_PED_ELTR_FORN.FLAG_ESTOQUE_ZERADO,
	V_EST_PED_ELTR_FORN.HORA_DEMANDA,
	V_EST_PED_ELTR_FORN.QTDE_MEDIAF,
	V_EST_PED_ELTR_FORN.COBERTURA,
	V_EST_PED_ELTR_FORN.QTDE_BASE_ALCADA,
	V_EST_PED_ELTR_FORN.COBERTURA_ALCADA,
	V_EST_PED_ELTR_FORN.VLR_ALCADA,
	V_EST_PED_ELTR_FORN.QTDE_ALCADA,
	V_EST_PED_ELTR_FORN.PERC_VLR_ALCADA,
	V_EST_PED_ELTR_FORN.PERC_COBERTURA_ALCADA

FROM         dbo.V_EST_PED_ELTR_FORN LEFT OUTER JOIN
                      dbo.EST_PED_CP_IT INNER JOIN
                      dbo.EST_PED_CP ON dbo.EST_PED_CP_IT.CD_EMP = dbo.EST_PED_CP.CD_EMP AND 
                      dbo.EST_PED_CP_IT.CD_FILIAL = dbo.EST_PED_CP.CD_FILIAL AND dbo.EST_PED_CP_IT.CD_PED = dbo.EST_PED_CP.CD_PED INNER JOIN
                      dbo.EST_PED_ELTR_FORN_PED_CP ON dbo.EST_PED_CP.CD_EMP = dbo.EST_PED_ELTR_FORN_PED_CP.CD_EMP AND 
                      dbo.EST_PED_CP.CD_FILIAL = dbo.EST_PED_ELTR_FORN_PED_CP.CD_FIL_ENTR AND 
                      dbo.EST_PED_CP.CD_PED = dbo.EST_PED_ELTR_FORN_PED_CP.CD_PED ON 
                      dbo.V_EST_PED_ELTR_FORN.CD_PROD = dbo.EST_PED_CP_IT.CD_PROD AND 
                      dbo.V_EST_PED_ELTR_FORN.CD_EMP = dbo.EST_PED_CP_IT.CD_EMP AND 
                      dbo.V_EST_PED_ELTR_FORN.CD_EMP = dbo.EST_PED_ELTR_FORN_PED_CP.CD_EMP AND 
                      dbo.V_EST_PED_ELTR_FORN.CD_PED_SOL = dbo.EST_PED_ELTR_FORN_PED_CP.CD_PED_SOL AND 
                      dbo.V_EST_PED_ELTR_FORN.CD_FORN = dbo.EST_PED_ELTR_FORN_PED_CP.CD_FORN AND 
                      dbo.V_EST_PED_ELTR_FORN.CD_FIL_ENTR = dbo.EST_PED_ELTR_FORN_PED_CP.CD_FIL_ENTR


GO


