有一个类似的表:
+-----------+---------+------------+
| uid | user_id | month |
+-----------+---------+------------+
| d23fsdfsa | 101 | 2017-01-02 |
| 43gdasc | 102 | 2017-05-06 |
| b65hrfd | 101 | 2017-08-11 |
| 1wseda | 103 | 2017-09-13 |
| vdfhryd | 101 | 2017-08-06 |
| b6thd3d | 105 | 2017-05-03 |
| ve32h65 | 102 | 2017-01-02 |
| 43gdasc | 102 | 2017-09-06 |
+-----------+---------+------------+
如果user_id出现在同一个月,而只计数一个,该如何计算每个user_id?
最终表应如下所示:(因为“ 101”在同一个月中有两个uid,因此它只计算一个)
+---------+-----------+
| user_id | count_num |
+---------+-----------+
| 101 | 2 |
| 102 | 3 |
| 103 | 1 |
| 105 | 1 |
+---------+-----------+
答案 0 :(得分:1)
如果我的理解正确,那么您希望每个用户有不同的月份数。如果是这样:
select user_id, count(distinct trunc(month, 'MONTH')) as count_num
from t
group by user_id;