/****** Object:  View [dbo].[V_EST_PROD_DETER]    Script Date: 11/13/2013 10:57:44 ******/
IF  EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[V_EST_PROD_DETER]'))
DROP VIEW [dbo].[V_EST_PROD_DETER]
GO
GO
GO
--SELECT * FROM V_EST_PROD_DETER WHERE CD_EMP = 1 AND CD_FILIAL = 999 AND CD_PROD =62353
--Versao 2338
CREATE VIEW [dbo].[V_EST_PROD_DETER]
AS
SELECT
	Rs.CD_EMP, 
	Rs.CD_FILIAL, 
	Rs.CD_LOTE, 
	Rs.DT_CAD, 
	Rs.CD_PROD, 
	Rs.DS_PROD, 
	EST_PROD_PRECO.VLR_TABELA,
    Rs.VLR_CUSTO, 
	Rs.QT_PROD, 
	Rs.DS_LOTE
FROM
	(SELECT     
		EST_PROD_DETE.CD_EMP, 
		EST_PROD_DETE.CD_FILIAL, 
		EST_PROD_DETE.CD_LOTE, 
		EST_PROD_DETE.DT_CAD, 
		EST_PROD.CD_PROD, 
		EST_PROD.DS_PROD, 	
		ISNULL(EST_PROD_DETE_IT.VLR_CUSTO_PROD,0)  AS VLR_CUSTO, 
		EST_PROD_DETE_IT.QT_PROD, 
		EST_PROD_DETE.DS_LOTE
	FROM         
		EST_PROD_DETE_IT INNER JOIN EST_PROD_DETE ON 
		EST_PROD_DETE_IT.CD_EMP = EST_PROD_DETE.CD_EMP AND 
		EST_PROD_DETE_IT.CD_LOTE = EST_PROD_DETE.CD_LOTE 
		INNER JOIN EST_PROD ON 
		EST_PROD_DETE_IT.CD_EMP = EST_PROD.CD_EMP AND 
		EST_PROD_DETE_IT.CD_PROD = EST_PROD.CD_PROD) Rs  
		
		INNER JOIN EST_PROD_PRECO ON 
		Rs.CD_EMP = EST_PROD_PRECO.CD_EMP AND
		Rs.CD_FILIAL = EST_PROD_PRECO.CD_FILIAL AND
		Rs.CD_PROD = EST_PROD_PRECO.CD_PROD