How can I use LIKE for a comparison of two different table fields? I need to compare the first 5 characters of each field. I tried SUBSTRING and with LEFT , however, the performance gets pretty bad.
Here are two ways I applied, with the comment signaling: 1)
SELECT(SELECT sum(sd3_sub1.D3_QUANT)
FROM SD3010 AS sd3_sub1
WHERE sd3_sub1.D3_TM = '010'
AND YEAR(D3_EMISSAO) = YEAR(sd3.D3_EMISSAO) AND MONTH(D3_EMISSAO) = MONTH(sd3.D3_EMISSAO)
AND sd3_sub1.D3_LOCAL BETWEEN '01' AND '02'
AND sd3_sub1.D3_FILIAL = sd3.D3_FILIAL
/*AQUI UTILIZEI O LEFT*/
AND LEFT(sd3_sub1.D3_CC, 5) = LEFT(sd3.D3_CC,5)
AND sd3_sub1.D_E_L_E_T_ <> '*') AS producao
FROM SD3010 AS sd3
2)
SELECT(SELECT sum(sd3_sub1.D3_QUANT)
FROM SD3010 AS sd3_sub1
WHERE sd3_sub1.D3_TM = '010'
AND YEAR(D3_EMISSAO) = YEAR(sd3.D3_EMISSAO) AND MONTH(D3_EMISSAO) = MONTH(sd3.D3_EMISSAO)
AND sd3_sub1.D3_LOCAL BETWEEN '01' AND '02'
AND sd3_sub1.D3_FILIAL = sd3.D3_FILIAL
/*AQUI UTILIZEI O SUBSTRING*/
AND SUBSTRING(sd3_sub1.D3_CC,1,5) = SUBSTRING(sd3.D3_CC,1,5)
AND sd3_sub1.D_E_L_E_T_ <> '*) AS producao
FROM SD3010 AS sd3