为什么我的查询显示avg的多个结果?

时间:2016-07-06 22:34:02

标签: mysql sql

我试图在他活动的最后7天内找到平均用户,但不是为用户显示一个平均结果,而是我的查询显示每个用户的多次出现

SELECT temp_user_batches.user_id                AS 
                               user_id, 
                               temp_user_batches.activity_goal          AS goal, 
                               user_activities.activity_date            AS dates 
                               , 
                               Date_sub(Curdate( 
                               ), INTERVAL 7 day)      AS 
                               min_activity_date, 
                               Curdate()                                AS 
                               max_activity_date, 
                               ( Sum(user_activities.time_spent) / 60 ) AS 
                               total_time_spent 
                        FROM   temp_user_batches 
                               INNER JOIN user_activities 
                                       ON temp_user_batches.user_id = 
                                          user_activities.user_id 
                        WHERE  activity_date BETWEEN Date_sub(Now(), 
                                                     INTERVAL 7 day) 
                                                     AND 
                                                     Curdate() 
                        GROUP  BY user_id, 
                                  goal, 
                                  dates, 
                                  max_activity_date, 
                                  min_activity_date;

这是结果。

9122    10  2016-07-02  2016-06-30  2016-07-07  0.0000
9122    10  2016-07-05  2016-06-30  2016-07-07  0.0000
9122    10  2016-07-06  2016-06-30  2016-07-07  0.0000
9127    3   2016-07-01  2016-06-30  2016-07-07  0.4500
9127    3   2016-07-03  2016-06-30  2016-07-07  0.0000
9131    3   2016-07-01  2016-06-30  2016-07-07  135.3000
9131    3   2016-07-03  2016-06-30  2016-07-07  340.4500
9131    3   2016-07-04  2016-06-30  2016-07-07  0.0000
9134    3   2016-07-03  2016-06-30  2016-07-07  0.0000
9135    3   2016-07-03  2016-06-30  2016-07-07  9.1667
9136    3   2016-07-03  2016-06-30  2016-07-07  4.4000
9137    3   2016-07-03  2016-06-30  2016-07-07  0.0000

我的用户活动表有3个用户9122的条目

1   9122    10  0   2016-07-02
2   9122    10  0   2016-07-05
3   9122    10  0   2016-07-06

所以这个查询应该是avg但不是。

2 个答案:

答案 0 :(得分:1)

您有多行具有不同的activity_goal值,但您似乎希望将它们组合在一起。因此,将查询简化为您想要的内容:

SELECT b.user_id as user_id,  
       Date_sub(Curdate(), INTERVAL 7 day) as  min_activity_date, 
       Curdate() as max_activity_date, 
       ( Sum(user_activities.time_spent) / 60 ) as  total_time_spent 
FROM temp_user_batches b INNER JOIN
     user_activities a
     ON b.user_id = a.user_id 
WHERE a.activity_date BETWEEN Date_sub(Now(), INTERVAL 7 day) AND Curdate() 
GROUP  BY user_id, max_activity_date, min_activity_date;

答案 1 :(得分:0)

这类问题的答案实际上相当简单:"仔细查看 ... yield Request(url, callback=self.second_method, meta={'item': myitem, 'moreinfo': 'moreinfo', 'foo': 'bar'}) def second_method(self, response): previous_meta_info = response.meta # I can access the previous item with `response.meta['item']` ... 条款!"

毫无疑问,你在这个条款中包含了比你实际想要的更多的组字段。

首先从列表中删除最后一个组。 。