1
I need to get some information from the postgresql database through R .
However, the filters are not downloading what I want. I would like to download only the cnaes of companies with cadastral situation "2" and date "02/2020". As I did, he is pulling other dates.
Following example:
library(RPostgreSQL)
...
Cnae_selecionada <- dbGetQuery(con,"
SELECT *
FROM cnpj.cnae
WHERE
(cnae_fiscal BETWEEN 3700000 AND 3999999)
OR (cnae_fiscal BETWEEN 4900000 AND 5399999)
OR (cnae_fiscal BETWEEN 5500000 AND 5699999)
OR (cnae_fiscal BETWEEN 5800000 AND 6399999)
OR (cnae_fiscal BETWEEN 6600000 AND 6699999)
OR (cnae_fiscal BETWEEN 6800000 AND 7599999)
OR (cnae_fiscal BETWEEN 7700000 AND 8299999)
OR (cnae_fiscal BETWEEN 9200000 AND 9699999)
OR (cnae_fiscal BETWEEN 160000 AND 169999)
OR (cnae_fiscal BETWEEN 230000 AND 239999)
OR (cnae_fiscal BETWEEN 4520000 AND 4529999)
OR (cnae_fiscal BETWEEN 4543000 AND 4543999)
OR (cnae_fiscal BETWEEN 8550000 AND 8559999)
OR (cnae_fiscal BETWEEN 8590000 AND 8599999)
AND (cnae_fiscal NOT BETWEEN 6912000 AND 6912999)
AND (cnae_fiscal NOT BETWEEN 7010000 AND 7019999)
AND (cnae_fiscal NOT BETWEEN 8112000 AND 8112999)
AND (cnae_fiscal NOT BETWEEN 9400000 AND 9499999)
AND situacao_cadastral LIKE '2'AND data LIKE '%02/2020%'
")
I tried some other ways too, but without success.
Check the priority of the AND and OR operators. Maybe a parenting is necessary for the correct evaluation. In your case all these Dnas apply only to
(cnae_fiscal BETWEEN 8590000 AND 8599999)
or should apply to all tracks?– anonimo
what type of field data
data
?– Ricardo Pontual