Result of a SELECT in Postgresql is not the same in Sqlite

Asked

Viewed 76 times

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

  • 1

    The bases are equal ?

  • 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 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 ?

  • It is exactly the same in both Postgresql and Sqlite databases, after all it is the same data, same columns etc.

  • @Magno: Not knowing what are the types of each column of the table anomalias it is difficult to understand and reproduce your problem.

  • @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.

  • 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?

  • 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?

Show 3 more comments

1 answer

1

The difference may be in how each of the databases is interpreting the strings containing dates in the format DD/MM/YYYY.

The SQLite3 interprets strings containing dates in the format ISO8601, that is to say: YYYY-MM-DD, as long as the PostgreSQL uses an environment variable called DateStyle to make this control.

If you intend to use strings containing dates in the format DD/MM/YYYY in the SQLite3, you must use the function strftime(), look at you:

INSERT INTO
    export
VALUES
    (
      '31992387535',
      'TROCA DE APARELHOS_REEMBOLSO - REBATE',
      'LIBERADO',
      strftime('%d/%m/%Y','27/02/2019'),
      strftime('%d/%m/%Y','27/02/2019'),
      strftime('%d/%m/%Y','01/03/2019')
    );

Or adjust your strings to the format expected by SQLite3:

INSERT INTO
    export
VALUES
    (
      '31992387535',
      'TROCA DE APARELHOS_REEMBOLSO - REBATE',
      'LIBERADO',
      '2019-02-27',
      '2019-02-27',
      '2019-03-01'
    );

I also noticed that you use special characters in your strings, for example:

...
ex.status = 'ANOMALIA TEMPORÁRIA'
...

The strings containing these special characters were saved correctly in the database where you are running your SELECT ?!

  • I can’t use this strftime('%d/%m/%Y','27/02/2019') by passing it right on SELECT to do this conversion no? because I would have a big problem doing it this way because this data comes from an Excel spreadsheet, and I use Python to create the Sqlite Database and it already imports all the contents of the Spreadsheet into the table, I don’t need to use INSERT INTO to put the contents in the spreadsheet I use a loop for this with a function of a Library called Pandas (Python), the columns of dates are in the format DD/MM/YYYY but would it really be the hindrance to that outcome?

  • Have you checked whether SELECT COUNT(*) FROM anomalias; returns the same amount of records in both databases ?

  • yes the same amount in both

  • I climbed every database to a website that wheel Sqlite, if you can help me bro, sincerely need really, had a deadline for delivery tomorrow honestly do not know what to do :/

  • @Locupos Yes special characters work perfectly, because at the end of the query with SELECT I take all this result and export to Excel again using the Pandas Library, in relation to Date, and Special Characters work perfectly, the problem is the wrong result that the Query I showed in the Post is bringing, it brings 3 more results, and it works perfectly in Postgresql

  • The data may be being corrupted during the import/export process. The error may be related to encoding strings or date format.

  • I don’t think so, because if that were the other 2 filters I have here would be a problem, in all I have 3 filters, both were created in Postgresql, and I passed them to Sqlite, the first 2 worked perfectly, but this third brought me 3 more data, The export and import occurs without errors, at the end of the export I check each record to see if it didn’t get weird or anything, and it’s all right, can you rewrite this query to a more objective way? I do not have much knowledge with Database, I have worked hard to do this Query, if you can help I thank!

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.