我有users
个桌子
id | email | website
1 test@gmail.com google.com
2 blah@hotmail.com google.com
3 fred@outlook.com yahoo.com
4 jeff@gmail.com yahoo.com
还有一个groups
表
id | group | user_id | created
1 group1 1 2018-06-06
2 group2 2 2018-06-06
3 group3 3 2018-09-09
4 group4 4 2018-06-06
我需要每website
天计算一次每个created
的计数,因此以上内容会给我 google.com 的 2 在 2018-06-06
即
website | count | date
google.com 2 2018-06-06
yahoo.com 1 2018-06-06
yahoo.com 1 2018-09-09
我将返回的行按日期分组,但是使用以下查询,每行的计数均为1
select count(distinct users.website), groups.created
from user_groups as groups
join users on users.id = groups.user_id
group by groups.created
所以我很简单
count(distinct users.website) | created
1 2018-06-06
1 2018-06-06
1 2018-06-06
1 2018-09-09
答案 0 :(得分:1)
尝试以下
select website,created,count(website) as cnt
from groups t2 join users t1 on t1.id = t2.userid
group by website, created
答案 1 :(得分:0)
尝试
Select distinct(groups.created),count(distinct(users.website))
from user_groups as groups
join users on users.id = groups.user_id
group by groups.created
应该工作。