SELECT A.*, M.KEY_VALUE NUM_DOCUMENTO_HASH, P.KEY_VALUE NUM_RUC_HASH
FROM(
    SELECT NUM_DOCUMENTO,
           CASE WHEN MAX(S.NUMRUC) IS NOT NULL THEN MAX(S.NUMRUC) 
                WHEN LENGTH(MAX(A.NUM_DOCUMENTO)) = 11 THEN MAX(A.NUM_DOCUMENTO)
           ELSE NULL END NUM_RUC
	FROM E_DW_VIEWS.V_FEEDBACK_TELEVENTAS A
	LEFT JOIN E_DW_VIEWS.V_DATOS_PRINCIPALES_SUNAT S
	ON (CASE WHEN SUBSTR(A.NUM_DOCUMENTO,1,2) = '10' AND LENGTH(A.NUM_DOCUMENTO) = 11 THEN SUBSTR(A.NUM_DOCUMENTO,3,8) ELSE A.NUM_DOCUMENTO END) =
    (CASE WHEN SUBSTR(S.NUMRUC,1,2) = '10'  AND LENGTH(S.NUMRUC) = 11 THEN SUBSTR(S.NUMRUC,3,8) ELSE S.NUMRUC END)
	WHERE GESTION = '202303'
	GROUP BY NUM_DOCUMENTO
) A
LEFT JOIN E_DW_VIEWS.V_MST_ENCRIP_NRO_DOC_MDL_HISTDIA M
ON A.NUM_DOCUMENTO = M.NRO_DOC
LEFT JOIN E_DW_VIEWS.V_MST_ENCRIP_NRO_DOC_MDL_HISTDIA P
ON A.NUM_RUC = P.NRO_DOC
WHERE A.NUM_RUC IS NOT NULL
AND A.NUM_RUC NOT LIKE ''
AND A.NUM_RUC <> &#039;.&#039;



SELECT A.*, M.KEY_VALUE NUM_DOCUMENTO_HASH, M.KEY_VALUE NUM_RUC_HASH
FROM(
    SELECT NUM_RUC_CD NUM_DOCUMENTO,
           NUM_RUC_CD NUM_RUC
	FROM E_DW_VIEWS.V_VPC_NEG_EMPRESAS_VIG A
	WHERE A.NUM_RUC_CD IS NOT NULL
	AND A.NUM_RUC_CD NOT LIKE &#039;&#039;
	GROUP BY NUM_RUC_CD
) A
LEFT JOIN E_DW_VIEWS.V_MST_ENCRIP_NRO_DOC_MDL_HISTDIA M
ON A.NUM_DOCUMENTO = M.NRO_DOC
WHERE A.NUM_DOCUMENTO IS NOT NULL
AND A.NUM_DOCUMENTO NOT LIKE &#039;&#039;
AND A.NUM_DOCUMENTO <> &#039;.&#039;

Add a code snippet to your website: www.paste.org