1
I have the following problem;
I’m trying to do some research and include the value of WHERE
major in a sub-select, but the result is always reported as 0, and there are records.
Part of query
that I’m in trouble is this:
SELECT
(SELECT COUNT(*) AS 'Colaboradores' FROM SRA010 WHERE RA_FILIAL = sd3.D3_FILIAL
AND (RA_SITFOLH NOT IN ('D', 'A') OR RA_DEMISSA > CAST(YEAR(sd3.D3_EMISSAO) AS VARCHAR)+CAST(REPLICATE('0', 2 - LEN(MONTH(sd3.D3_EMISSAO))) + RTrim(MONTH(sd3.D3_EMISSAO)) AS VARCHAR)+'31')
AND RA_CC IN (SELECT CTT_CUSTO FROM CTT010 WHERE sd3.D3_CC = CTT_CUSTO)) as Pessoas
FROM SD3010 AS sd3
WHERE sd3.D3_TM = '010'
AND sd3.D3_LOCAL IN ('01','02')
AND sd3.D3_FILIAL = '0301'
AND sd3.D3_CC in ('1350400', '1350403', '1350405', '1350408', '1350409', '1350410', '1350411', '1350412', '1350413', '1350414', '1350415', '1350416', '1350417', '1350418', '1350419', '1350420', '1350421', '1350422', '1350423', '1350424', '1350425', '1350426', '1350427', '1350428', '1350429', '1350430', '1350431', '1350432', '13604', '1360400', '1360401', '1360402', '1360499')
AND sd3.D_E_L_E_T_ <> '*'
AND sd3.D3_EMISSAO between '20161001' and '20161031'
group by sd3.D3_EMISSAO, sd3.D3_TM, sd3.D3_FILIAL, sd3.D3_CC
I need the condition of Sd3.D3_CC at the end of query, in the WHERE, go to the WHERE in Sd3.CC in subselect
Note: There is more subselects
that do not interfere with this information, however, I informed only this so that it can be clearer the understanding.
Obs2: Other conditions in Where
main, as the field Sd3.D3_FILIAL (in subselect condition 1), work properly.
Obs3: This query will turn into a view, q will serve as BI analysis, and the option Sd3.D3_CC can be changed in the filter, that is, the values of the field Sd3.D3_CC are not fixed, they are in the query only to simulate the BI filters
Update: Removing the condition AND RA_CC IN (SELECT CTT_CUSTO FROM CTT010 WHERE Sd3.D3_CC = CTT_CUSTO) in subselect, it displays results as shown below (1º Result with the above mentioned condition, 2º Result without the above mentioned condition)
Without the table structure and some data to test is very difficult. Add that to the question.
– Sorack
If you have a query that returns the values that are in the in, you can use as subquery in the WHERE of the external and internal query. Otherwise, you’ll have to repeat the in on both places.
– Cristiano Bombazar
Another thing: Not using the clause "IN" for performance reasons. Always seek to use the clause NOT EXISTS OR EXISTS.
– Cristiano Bombazar
@Cristianobombazar, in the above case the IN is more indicated, it has a list and not a select from another table.
– Marco Souza
@Marconciliosouza I ended up not finishing my comment. I spoke about EXISTS if the values can be obtained by sub-query. Sorry.
– Cristiano Bombazar
@Cristianobombazar, no problem.
– Marco Souza
@Thiagoalessandro, the construction "RA_CC IN (SELECT CTT_CUSTO FROM CTT010 WHERE Sd3.D3_CC = CTT_CUSTO))" seemed strange to me. In this case, IN has the EXISTS function?
– José Diz
@Josédiz This is just the IN...the idea would be to fetch RA_CC within this set of data brought by SELECT CTT_CUSTO FROM........
– Thiago Alessandro