SQL Distinct从某个日期范围开始计数?

时间:2013-12-04 10:59:21

标签: mysql sql count

为我的表设置了一个查询,我希望在前几周内显示某些反应,这在我写的时候效果很好

SELECT * 
FROM reactiondata 
WHERE reaction_time > DATE_SUB(NOW(), INTERVAL 1 WEEK) 
ORDER BY reaction_time ASC;

此查询还会选择唯一的促销ID字段和不同的用户名,以便您可以查看已发送了哪些促销ID的唯一用户名。

SELECT 
   reaction_promoID, 
   COUNT( DISTINCT reaction_username) AS reaction_username  
FROM reactiondata GROUP BY reaction_promoID

但是,我想这样,所以第二个查询在上周的日期范围内工作,就像第一个查询一样。当我向查询中添加Where等...时它不起作用。

任何帮助都非常明确!

A + TS

2 个答案:

答案 0 :(得分:1)

SELECT reaction_promoID, COUNT( DISTINCT reaction_username) AS reaction_username  
FROM reactiondata 
WHERE reaction_time > DATE_SUB(NOW(), INTERVAL 1 WEEK)
GROUP BY reaction_promoID

答案 1 :(得分:0)

我认为现在是HAVING子句的时候了:

http://www.w3schools.com/sql/sql_having.asp

SELECT reaction_promoID, COUNT( DISTINCT reaction_username) AS reaction_username  
FROM reactiondata 
GROUP BY reaction_promoID
HAVING  reaction_time > DATE_SUB(NOW(), INTERVAL 1 WEEK)