Im not quite sure this is the proper way to perform the task you are trying to accomplish, but I think it may work for you. Using a UNION
statement in your Sqlite query would combine two SQL statements. I think, after playing around with your test data and DB schema, you would definitely need to run two queries. One Query with the where
date range of 7 days and the second with the date range of 14 days, give this a try:
SELECT '7 Day Average', CategoryOfPerson, AptNo, FirstName, LastName, count(*) / 7 as Average from LocalDB
WHERE TimeStamp between date('now') and date('now','7 days')
GROUP BY AptNo
UNION SELECT '14 Day Average', CategoryOfPerson, AptNo, FirstName, LastName, count(*) / 14 as Average from LocalDB
WHERE TimeStamp between date('now') and date('now','14 days')
GROUP BY AptNo;
You will have to adjust the two where statements to select data within your range, one for 7 days worth of data and the other for 14 days worth of data. This will then product a count by AptNo, divided by 7 and 14 respectively, showing the average number of accesses per unit # for that range.
You will wind up with two entries per grouped AptNo, but I think this would be the only way to get it done in one query.
Updated / Additional Answer:
So, I have crafted a query that will give you a single line response with each average here:
select distinct AptNo, CategoryOfPerson, FirstName, LastName,
(select (count(*) / 7) as '7_day_avg' from LocalDB where AptNo = this.AptNo and TimeStamp between date('now') and date('now','7 days')),
(select (count(*) / 14) as ’14_day_avg’ from LocalDB where AptNo = this.AptNo and TimeStamp between date('now') and date('now','14 days'))
from LocalDB as this;
BUT The problem with this is that the CategoryOfPerson, FirstName, LastName
will probably not be correct as the query is seeking the DISTINCT AptNo
and the Category/Name associated with the first record it finds. So you could remove the name and category and just have a listing by AptNo with the averages, like this:
select distinct AptNo,
(select (count(*) / 7) as '7_day_avg' from LocalDB where AptNo = this.AptNo and TimeStamp between date('now') and date('now','7 days')),
(select (count(*) / 14) as ’14_day_avg’ from LocalDB where AptNo = this.AptNo and TimeStamp between date('now') and date('now','14 days'))
from LocalDB as this;