/****** Object:  View [dbo].[V_EST_NF_SAI_IT_PIS_COFINS]    Script Date: 12/27/2013 10:28:06 ******/
IF  EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[V_EST_NF_SAI_IT_PIS_COFINS]'))
DROP VIEW [dbo].[V_EST_NF_SAI_IT_PIS_COFINS]
GO
GO
GO
GO
--SELECT top 10 * FROM V_EST_NF_SAI_IT_PIS_COFINS
CREATE VIEW [dbo].[V_EST_NF_SAI_IT_PIS_COFINS] AS
--Versao 2465
SELECT
	EST_NF_SAI_IT.CD_EMP,
	EST_NF_SAI_IT.CD_FILIAL,
	EST_NF_SAI_IT.CD_NF,
	ISNULL(SUM(EST_NF_SAI_IT.VLR_BASE_PIS),0) AS VLR_BASE_PIS_COFINS,
	CASE WHEN ISNULL(SUM(EST_NF_SAI_IT.VLR_BASE_SUBS), 0) = 0	THEN
		ROUND(SUM(EST_NF_SAI_IT.VLR_ICMS_IT),2)	
	ELSE 
		0
	END AS VLR_ICMS_ITENS
FROM   
	EST_NF_SAI_IT INNER JOIN EST_PROD ON 
	EST_NF_SAI_IT.CD_EMP = EST_PROD.CD_EMP AND 
	EST_NF_SAI_IT.CD_PROD = EST_PROD.CD_PROD
GROUP  BY 
	EST_NF_SAI_IT.CD_EMP,
	EST_NF_SAI_IT.CD_FILIAL,
	EST_NF_SAI_IT.CD_NF,
	EST_NF_SAI_IT.PIS_COFINS

