【问题标题】:Display only 1 person if the data is more than 1 in a day一天数据超过1人时只显示1人
【发布时间】:2021-03-26 04:50:44
【问题描述】:

如果一天中有一个人签到超过 1 个,我正在努力编写一个查询来显示一个月内签到的人数。例如,如果一个人一天签到 3 次,数据应该只显示 1 次。

这是我的桌子

check_in table

id | checkin_date | user_id | office_id
1    13-12-2020     1         D01
2    13-12-2020     1         D02
3    13-12-2020     2         D01
4    14-12-2020     3         D01

基于 check_in 表,我想产生这样的结果:

checkin_date | num_checkin
13-12-2020     2          
14-12-2020     1

【问题讨论】:

    标签: sql sql-server count aggregate-functions distinct


    【解决方案1】:

    使用count(distinct):

    select checkin_date, count(distinct user_id) as num_checkin
    from mytable
    group by checkin_date
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-27
      • 1970-01-01
      • 2016-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多