【问题标题】:My sql query is not getting desier resultMysql查询没有得到想要的结果
【发布时间】:2016-10-04 03:02:55
【问题描述】:

这是我的考勤表检查类型 0 用作 intime 和 1 as out time, 我希望 checktype = 0 的时间为 intime,checktype = 1 的时间为 outtime 根据日期单次输入,所有 empid 订单按 intime 将升序 检查类型 0 和检查类型 1 的降序

我想要下面的类型结果

【问题讨论】:

    标签: sql sql-server-2008 sql-server-2008-r2


    【解决方案1】:

    这里有一种方法可以为每位员工每天选择最短的上班时间和最长的上班时间:

    select min(case when checktype = 0 then time end) min_intime,
        max(case when checktype = 1 then time end) max_outtime,
        empid,
        date
    from mytable
    group by empid, date
    

    【讨论】:

    • 非常感谢兄弟。
    【解决方案2】:

    在这种情况下,我们可以使用 OUTER APPLY 为选择进行不同的设置:

    select 
        min(t.time) inTime,
        max(t1.time) outTime,
        t.empid,
        t.date
    from table1 t
    outer apply(select time from table1 t1 where t.empid = t1.empid 
                and t1.checktype = 1) t1
    where t.checktype = 0
    group by t.empid, t.date
    

    【讨论】:

    • 我尝试了上面的查询,它给了我结果,但是如果特定的一天没有 intime 和 out time 存在,则使用上面的查询逻辑将不会显示在最终结果中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-03
    • 1970-01-01
    • 1970-01-01
    • 2016-03-14
    • 2022-09-29
    • 1970-01-01
    相关资源
    最近更新 更多