My database (sqlite3), has two tables with the following schemas:
CREATE TABLE log(d date, usr text, tag text, bytes int);
CREATE TABLE valid(tag text, filesize int);
Let’s say I have the following sample data:
d | usr | tag | bytes
--------------------------------
2012-01-19 | bob | foo | 990
2012-01-18 | bob | foo | 1000
2012-01-17 | joe | bar | 2000
2012-01-16 | joe | bar | 1800
2012-01-15 | joe | baz | 0
tag | size
-----------
foo | 1000
bar | 2000
I would like to retrieve a list of the most recent events that resulted in a user failing to access all the bytes in a valid file. In the example above, the select should yield 2012-01-19 | bob | foo | 990.
Right now, I’m using two select statements to get the results. The first gets the most recent event per user, and the second checks whether all the bytes were accessed.
CREATE VIEW tmp AS
SELECT * FROM log JOIN (SELECT max(d) AS maxd, usr FROM log GROUP BY usr) AS
tmplog ON (tmplog.usr=log.usr and tmplog.maxd=log.d);
SELECT usr,d FROM tmp
WHERE tag IN (SELECT tag FROM valid) AND bytes NOT IN (SELECT size FROM valid);
Is there a way to do this with one select or more efficiently?
Update:
In the example above, the query shouldn’t retrieve user “joe” since his most recent log entry shows a complete file being accessed.
Find the most recent
dperusrandtag(wherebytes < filesize), then select thoselogentries matching those most recentd/usr/tagvalues: