IF  EXISTS (SELECT * FROM SYS.VIEWS WHERE OBJECT_ID = OBJECT_ID(N'[DBO].[V_EST_PROD_CPL_TRIB_INTER_DAR]'))
DROP VIEW [DBO].[V_EST_PROD_CPL_TRIB_INTER_DAR]
GO
GO
--SELECT * FROM V_EST_PROD_CPL_TRIB_INTER_DAR WHERE CD_PROD = 3229 AND CD_FILIAL = 1 AND UF = 'SC'
CREATE VIEW V_EST_PROD_CPL_TRIB_INTER_DAR AS
SELECT
        EST_TRIB_CONF_GRP.CD_EMP,
        PRC_REGIAO_FISCAL_PRC_FILIAL.CD_FILIAL,
        EST_PROD_EST_TRIB_CONF_GRP.CD_PROD,
        EST_TRIB_CONF_GRP_CPL.UF COLLATE SQL_Latin1_General_CP1_CI_AS AS UF,
        EST_TRIB_CONF_GRP_CPL.TP_MOV,
        EST_TRIB_CONF_GRP.CD_CTR,
		0 AS PERC_REDU_SUBS_PMC
FROM
        EST_TRIB_CONF_GRP INNER JOIN EST_PROD_EST_TRIB_CONF_GRP ON
        EST_TRIB_CONF_GRP.CD_EMP = EST_PROD_EST_TRIB_CONF_GRP.CD_EMP AND
        EST_TRIB_CONF_GRP.CD_CTR = EST_PROD_EST_TRIB_CONF_GRP.CD_CTR 
        INNER JOIN EST_TRIB_CONF_GRP_CPL ON
        EST_TRIB_CONF_GRP.CD_EMP = EST_TRIB_CONF_GRP_CPL.CD_EMP AND
        EST_TRIB_CONF_GRP.CD_CTR = EST_TRIB_CONF_GRP_CPL.CD_CTR 
        INNER JOIN PRC_REGIAO_FISCAL_PRC_FILIAL ON
        EST_TRIB_CONF_GRP.CD_REGIAO_FISCAL = PRC_REGIAO_FISCAL_PRC_FILIAL.CD_REGIAO_FISCAL
WHERE
        EST_TRIB_CONF_GRP.FLAG_TRIB_DARE = 1
        AND EST_TRIB_CONF_GRP_CPL.TP_TRANSA=0
UNION 
SELECT
        CD_EMP,
        CD_FILIAL,
        CD_PROD,
        UF,
        EST_PROD_CPL_TRIB_INTER_DAR.TP_MOV,
        0 AS CD_CTR,
		ISNULL(PERC_RED_SUBS_TRIB,0) PERC_REDU_SUBS_PMC
FROM
        EST_PROD_CPL_TRIB_INTER_DAR