1
Good evening guys, I developed a Query in Postgresql to apply a SELECT in my data imposing some rules, and these rules are very important for me to get the right result at the end of the selection.
The filter I needed to develop was this:
GSM which is repeated, and has in both STATUS = 'TEMPORARY ANOMALY' and its MOTIVO_ENVIO is the same, only import one record, whose DATA_ALTERACAO is more recent.
His resolution to run on Postgresql was this:
SELECT ex.gsm, ex.motivo_envio, ex.status, ex.data_ativacao, ex.data_importacao, ex.data_alteracao
FROM (SELECT gsm, MAX(data_alteracao) AS last_date FROM anomalias GROUP BY gsm) lst
INNER JOIN anomalias ex ON (lst.gsm = ex.gsm) AND (ex.data_alteracao = lst.last_date)
WHERE ex.status = 'ANOMALIA TEMPORÁRIA'
AND EXISTS (SELECT tmp.gsm FROM anomalias tmp
WHERE tmp.gsm = ex.gsm
AND tmp.motivo_envio = ex.motivo_envio
AND tmp.status = ex.status
GROUP BY tmp.gsm, tmp.motivo_envio, tmp.status
HAVING COUNT(tmp.gsm) > 1)
This filter returns to me 9 records, and it’s exactly the right amount of records I needed.
My problem is being when I put this filter to turn on SQLite
he returns me 3 records the more, and this can not happen, I need much that it obeys this Query, I copied exactly the same way it is in Postgresql and put in Sqlite, but it is returning me undesirable data.
Can someone help me please, I do not know if this query I did is the best method to follow the rule I need, the rule as mentioned above is this:
GSM which is repeated, and has in both STATUS = 'TEMPORARY ANOMALY' and its MOTIVO_ENVIO is the same, only import one record, whose DATA_ALTERACAO is more recent.
If anyone needs the contents of the database to test here: https://ghostbin.com/paste/9uqnn
The bases are equal ?
– Motta
Yes @Motta the same, if you want I can make available the Sqlite Database file as well, I also made available the
INSERT
from the full contents of the database, I put in the ghostbin: https://ghostbin.com/paste/9uqnn if you can help me bro, really I had finished the project there when I went to check the result I got this surprise.– Magno
@Magno The structure of the table
anomalias
is also equal in both banks ? You could change your question by including the table structure in the issue ?– Lacobus
It is exactly the same in both Postgresql and Sqlite databases, after all it is the same data, same columns etc.
– Magno
@Magno: Not knowing what are the types of each column of the table
anomalias
it is difficult to understand and reproduce your problem.– Lacobus
@Lacobus in Postgresql have the data types as follows:
gsm = text | motivo_envio = text | status = text | data_alteracao = date | data_importacao = date | data_alteracao = date
and in Sqlite as well.– Magno
The fields you listed in the GROUP BY clause are not listed in the SELECT field list. Are you sure that this was the command you ran on Postgresql?
– anonimo
Yes @anonimo works perfectly in Postgresql actually if I add these fields that are "theoretically missing" in the query it gives me error, I tested to do this in Sqlite and gave no error, but anyway, keep bringing me this 3 more data, could you help me? type do not have much knowledge with SQL, you can rewrite this query for me?
– Magno