我有一张桌子:
A B
---------
1 Date1
1 Date1
1 Date1
1 Date2
2 Date1
3 Date3
4 Date2
4 Date1
1 Date3
SELECT A, count(A) FROM `table` WHERE B BETWEEN 'Date1' AND 'Date2'
GROUP BY A
ORDER BY A
当我进行正常查询时:
A B
----------
1 1
1 1
1 1
2 1
3 1
4 1
4 1
需要算上几天?对此有任何简单的疑问吗?请帮忙
需要以下数据:
A B
----------
1 3
2 1
3 1
4 2
答案 0 :(得分:2)
似乎你需要数(不同的B)
SELECT A, count(distinct B)
FROM `table` WHERE B BETWEEN 'Date1' AND 'Date2'
GROUP BY A
ORDER BY A;
答案 1 :(得分:-2)
问题解决了:
SELECT T1.id as id, COUNT(COALESCE(T2.totalcountdepartmentuserwise, 0)) AS totalcountdepartmentuserwise
FROM (
SELECT DISTINCT id FROM users WHERE company_id = 33
) AS T1
LEFT JOIN (
SELECT user_id, COUNT(distinct user_id) AS totalcountdepartmentuserwise FROM userlogs WHERE created_at BETWEEN '2016-01-01' AND '2016-01-25' GROUP BY user_id, DATE(created_at) ORDER BY user_id
) AS T2 ON T2.user_id = t1.id GROUP BY id