我有这个查询,它找到每月的每日平均值:
select avg(d.dayilyCount)
from (
select count(*) as dayilyCount, timestamp as dt, strftime('%d-%m-%Y', timestamp / 1000, 'unixepoch', 'localtime') as dmy
from T group by dmy
) d
group by strftime('%m-%Y', d.dt / 1000, 'unixepoch', 'localtime')
问题在于它代表空的日子。我该如何解决这个问题?
例如:如果1月份有28天,只有20天有数据。它只计算和平均20天,缺少8天。
答案 0 :(得分:1)
SELECT
SUM(d.dayilyCount) / (SELECT
julianday('now', 'start of month', '+1 month', '-1 day') - julianday('now', 'start of month') + 1) as mon_avg
FROM (SELECT
COUNT(*) AS dayilyCount,
timestamp AS dt,
strftime('%d-%m-%Y', timestamp / 1000, 'unixepoch', 'localtime') AS dmy
FROM T
GROUP BY dmy) d
GROUP BY strftime('%m-%Y', d.dt / 1000, 'unixepoch', 'localtime');