仅计算给定日期的新记录数(如果它们是新的)
仅当记录日期是记录所有者的最小(日期)时,我才尝试计算给定日期的新记录数。这是我试图运行的查询:
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
错误。我缺少什么?
I am trying to count the number of new records for a given date only if the date of the record is the min(date) for the record owner. Here is the query I am trying to run:
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';
Basically I am trying to say, if this is the first date this user/merchant is encountered, count it as a new user for this merchant.
When I run this I get a Invalid use of group function
error. What am I missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须使用
HAVING
来过滤一组记录或聚合函数的条件,因此不要使用WHERE min(date(date_posted)) = '2012-02-07'
尝试HAVING min(date(date_posted)) = '2012-02-07'
you must use
HAVING
to filter a condition for a group of record or an aggregate function, so Instead ofWHERE min(date(date_posted)) = '2012-02-07'
tryHAVING min(date(date_posted)) = '2012-02-07'
尝试以下:
Try below: