1

仅当记录日期是记录所有者的最小(日期)时,我才尝试计算给定日期的新记录数。这是我要运行的查询:

SELECT COUNT(*) 
  FROM user_total_spends 
 WHERE user_id IN (SELECT user_id 
                     FROM user_total_spends 
                    WHERE MIN(DATE(date_posted)) = '2012-02-07')
   AND merchant_location_id = '4f39b201-4a50-40ff-9cdf-cec51506eaf2' 
   AND date_posted = '2012-02-07'; 

基本上我想说的是,如果这是第一次遇到此用户/商家,请将其视为此商家的新用户。

当我运行它时,我得到一个Invalid use of group function错误。我错过了什么?

4

2 回答 2

2

您必须使用HAVING来过滤一组记录或聚合函数的条件,所以不要WHERE min(date(date_posted)) = '2012-02-07'尝试HAVING min(date(date_posted)) = '2012-02-07'

于 2012-02-17T05:31:50.873 回答
0

试试下面:

 SELECT count(*) FROM user_total_spends 
    WHERE user_id IN (SELECT user_id FROM user_total_spends GROUP BY user_id HAVING min(date(date_posted)) = '2012-02-07')
    AND merchant_location_id = '4f39b201-4a50-40ff-9cdf-cec51506eaf2' AND 
    date_posted = '2012-02-07'; 
于 2012-02-17T05:34:25.093 回答