Buenas tardes, tengo 2 tablas y estoy haciendo un left join
de la T1
con la T2
para extraer el código de cliente. El problema es que existen 2 códigos de cliente para un mismo (RUC) en algunos casos (esto por la condición del cliente que en algunos casos tienen 2) y por consiguiente se duplican los registros en la T1
ya que en la T2
hay 2 códigos por ese cliente, solo necesito coger uno.
Presento el código para que me puedan guiar:
SELECT
'1' AS CO_TURN,
'1' AS CO_ESTA,
'1' AS CO_FPAG,T1.CO_PROD,
'1' AS CO_EMPR,T1.CO_TDOC,
T1.NU_NSER,
T1.NU_NDOC,
NULL AS FE_TURN,
LEFT(T1.FE_HORA,5) FE_HORA,
'0' AS NU_NVAL,
T1.FE_EMIS,
NULL AS FE_EMIS_HORA,
ROUND(T1.IM_TOTA/1.18,2) AS IM_VNET,
ROUND(T1.IM_TOTA/1.18,10) AS IM_VVEN,
ROUND (T1.IM_TOTA-ROUND(T1.IM_TOTA/1.18,4),2) IM_IGV,
'0.0000000000' AS IM_DESC,
T1.IM_TOTA,
'0.0000' AS NU_GALN,
'0.0000' AS NU_PREV,
T1.IM_TOTA AS IM_PAGO,
'0' AS NU_LADO,
'1' AS CO_TMON,
'N' AS ST_DVEN,
0 AS CO_CHOF,
CASE T2.CO_CLIE WHEN NULL THEN NULL WHEN 0 THEN NULL ELSE T2.CO_CLIE END CO_CLIE,
NULL AS NU_LIQI,
NULL AS ST_CANJ,
NULL AS ST_LIQI,
NULL AS ST_PERI,
NULL AS ST_ANNO,
NULL AS CO_TRAB,
'0' AS CO_VEHI,
'SISGES' AS CO_USUA_CREA,GETDATE() AS FE_USUA_CREA,
'0.1800' AS NU_IGV,
'' AS CO_TDES_DOCU,
NULL AS NO_CLIE_D1_DNI
FROM
MAR17_1 T1 LEFT JOIN TCCLIE T2 ON T1.CO_CLIE= T2.NU_RUC
WHERE
T1.CO_TDOC IN (1,15,2)