有些日子不见了,每天平均每个月?

时间:2017-11-01 14:17:44

标签: sql sqlite android-sqlite

我有这个查询,它找到每月的每日平均值:

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天。

1 个答案:

答案 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');