在sqlite中按月分组

时间:2012-01-27 08:55:51

标签: android mysql sql sqlite

我有一张桌子,我对groupby月份没什么问题

CREATE TABLE BabyData (BabyId INT, BabyName TEXT, _id INT PRIMARY KEY, Date DATE, Height TEXT, Weight TEXT);

从这张表中我想要一年中每个月的平均体重

当我提供'2011-2-2'的日期时,我希望每个月的平均体重来自 '2011-2-2'到'2012-2-2'

我这样给了

SELECT strftime('%Y',Date) AS year, 
   strftime('%m',Date) AS month, 
   Avg(Weight) As Amount 
FROM BabyData 
Group By  strftime('%Y',Date),strftime('%m',Date)

但我只得到1个平均

1 个答案:

答案 0 :(得分:5)

SELECT strftime('%m',`Date`) AS `month`,
       Avg(Weight) As `Amount `
FROM BabyData
WHERE `DATE` BETWEEN DATE('2011-2-2') 
                       AND 
                     date('2011-2-2','+12 month')
GROUP BY `Month`