问题 - 基于时间对结果进行分组

时间:2015-08-30 13:40:16

标签: mysql

我是mysql的新手。我试图获取特定密钥和特定时间段的重复记录计数,除以同一时期内可用的记录总数。公式看起来像count(特定时期的col1)/ count(特定时期col1中的记录总数)。

还要排除不重复的col1值。表可能看起来像

col1 date
1'2015-08-03'
1'2015-08-03'
1'2015-08-03'
2'2015-08-03'
2'2015-08-03'
3'2015-08-03'
3'2015-08-03'
2'2015-08-04'
3'2015-08-04'
3'2015-08-04'

结果将是

col1评价
1 0.3
2 0.3
3 0.4

(2的校正结果)

1 个答案:

答案 0 :(得分:0)

试试这个

SELECT col1, count(col1)/total 
FROM table1 ,(SELECT count(*) as total from table1 
WHERE date between '2015-08-03' and '2015-08-04'
 ) as temp 
WHERE date between '2015-08-03' and '2015-08-04' 
GROUP BY col1 
HAVING count(col1)>1