I frequently have to post-mortem analyse cases in a database. This involves running a series of queries filtering for the set of values, several times with a different set of values each time.
I found very useful to have a "variables table" with one column per "variable" needed, where I set the values I need each time, and having the series of queries not change every time.
PRAGMA temp_store = 2; -- store temp table in memory, not on disk
CREATE TEMP TABLE IF NOT EXISTS _vars (_var1 INTEGER, _var2 INTEGER);
DELETE FROM _vars WHERE TRUE; -- run this when changing set of values
INSERT OR REPLACE INTO _vars VALUES (1492, 1112); --new values
SELECT * FROM _vars; -- check values are set and only one row is present
-- here comes the series of queries I need to run for each set of values
SELECT *
FROM my_table_name
WHERE one_column_name = (SELECT _var1 FROM _vars)
AND other_column_name = (SELECT _var2 FROM _vars);
If you ensure your "variables" have unique names (that do not occur in the tables you are querying), you may simplify even more the queries:
SELECT *
FROM my_table_name, _vars
WHERE one_column_name = _var1
AND other_column_name = _var2;
When I finish with one set of values I go back to the DELETE
and set a new set of values for my "variables" and keep going.
At the end, clean up by means of
DROP TABLE IF EXISTS _vars;
WARNING
Keep in mind that all of this is made while PRAGMA temp_store = 2;
is in force, so any data creation will try to go on memory.
Also beware of database/connection changes. The tables created in memory are coupled with the active database or connection. If you change database (think "USE ") or in you database editor/GUI you change connection, the temporal tables in memory will not follow: you will have to create them again for the next database or connection. And that implies that if you are changing database to compare query results from more than one database, keep in mind that your queries are using the values from the tables in memory currently associated with the database you are using. It follows that if you change them to run a query in one database and then you change to run the same query in another database, you need change the values in memory before running the query or you will not be running the same query.