0

2 つの結果を結合してシングル テーブルに表示する内部結合を使用しています。
遅刻と早退をチェックするクエリを作成しました。両方の出力を 1 つのテーブルとして表示します。
lateComing のクエリ:

    select k.EmpId,Min(k.DateofCheckin) as mindate,
    case 
    when min(time(DateofCheckin))<=@i then 'Ontime'
    when min(time(DateofCheckin)) between @i and @j then 'Late'
    when min(time(DateofCheckin)) between @j and @n then 'HalfDay'
    when DAYOFWEEK(Date(DateofCheckin)) =7 and  min(time(DateofCheckin)) >@j  then 'HalfDay'
    else 'Absent' end as LoginStatus
    from CheckInLogs k Join
    (select 
    @i:=time(r.LateComer) as LateComing,
    @j:=time(r.LateComingEndTime) as LateComingEnd,
    @m:=time(LunchStartTime),
    @n:=time(LunchEndTime),
    @o:=time(EarlyLeavingStartTime),
    @p:=time(EarlyLeavingEndTime), 
    e.EmpId
    FROM Employees e JOIN Profiles r ON e.LeaveProfile=r.ProfileName) h
    on k.EmpId=h.EmpId
    where k.DateofCheckin='in'  and  
    DAYOFWEEK(Date(k.DateofCheckin)) != 1 and
    k.BranchId=1 and date(DateofCheckin) between '2013-05-1' and '2013-05-29'
    group BY date(k.DateofCheckin),k.EmpId

EarlyLeaving のクエリ:

select 
    EmpId,Date(DateofCheckin) as LogoutDate,DateofCheckin,max(time(DateofCheckin)) as LogoutTime,
    case 
    when max(time(DateofCheckin))>=@p then 'Ontime'
    when DAYOFWEEK(Date(DateofCheckin)) =7 and  max(time(DateofCheckin)) < @m
    then 'HalfDay'
    when DAYOFWEEK(Date(DateofCheckin)) =7 and  max(time(DateofCheckin)) > @m  then 'Ontime'
    when max(time(DateofCheckin)) between @o and @p then 'EarlyLeaving'
    when max(time(DateofCheckin)) between @m  and @o then 'HalfDay'
    when max(time(DateofCheckin))<=@m then 'Absent'
    else 'Absent' end as LogoutStatus
    from CheckInLogs k1 Join
    (select 
    @i:=time(r.LateComingStartTime) as LateComing,
    @j:=time(r.LateComingEndTime) as LateComingEnd,
    @m:=time(LunchStartTime),
    @n:=time(LunchEndTime),
    @o:=time(EarlyLeavingStartTime),
    @p:=time(EarlyLeavingEndTime), 
    e.EmpId
    FROM Employees e JOIN Profiles r ON e.LeaveProfile=r.ProfileName) h1
    on k1.EmpId=h1.EmpId
    where k1.DateofCheckin='Out' 
    and k1.BranchId=1
    and DAYOFWEEK(Date(k1.DateofCheckin)) != 1 and  
    date(k1.DateofCheckin) between '2013-06-1' and '2013-06-29'
    group BY date(k1.DateofCheckin),k1.EmpId
4

1 に答える 1

1

以下は、クエリにどのようにアプローチするかの例です。これは簡単なコピーと過去の仕事だったので、構文エラーなどを修正する必要があります。主な違いは、クエリの開始時に従業員の詳細を 1 回取得するだけでよく、現在の 2 つのクエリ (従業員以外) に共通のフィールドを追加して結合する必要があることです。この場合、私は使用しましたlogoutdate

select *
from (select 
    @i:=time(r.LateComer) as LateComing,
    @j:=time(r.LateComingEndTime) as LateComingEnd,
    @m:=time(LunchStartTime),
    @n:=time(LunchEndTime),
    @o:=time(EarlyLeavingStartTime),
    @p:=time(EarlyLeavingEndTime), 
    e.EmpId
FROM Employees e JOIN Profiles r ON e.LeaveProfile=r.ProfileName) h
JOIN (select 
        EmpId, Date(DateofCheckin) as LogoutDate,
        DateofCheckin,max(time(DateofCheckin)) as LogoutTime,
        case 
            when max(time(DateofCheckin))>=@p then 'Ontime'
            when DAYOFWEEK(Date(DateofCheckin)) =7 and  max(time(DateofCheckin)) < @m
                then 'HalfDay'
            when DAYOFWEEK(Date(DateofCheckin)) =7 and  max(time(DateofCheckin)) > @m                  then 'Ontime'
            when max(time(DateofCheckin)) between @o and @p then 'EarlyLeaving'
            when max(time(DateofCheckin)) between @m  and @o then 'HalfDay'
            when max(time(DateofCheckin))<=@m then 'Absent'
        else 'Absent' end as LogoutStatus
      from CheckInLogs k1    
      where k1.DateofCheckin='Out' 
      and k1.BranchId=1
      and DAYOFWEEK(Date(k1.DateofCheckin)) != 1
      group BY date(k1.DateofCheckin), k1.EmpId) out
ON out.empid = h.empid
JOIN (select k.EmpId, Date(DateofCheckin) as LogoutDate,
                Min(k.DateofCheckin) as mindate,
                case 
                    when min(time(DateofCheckin))<=@i then 'Ontime'
                    when min(time(DateofCheckin)) between @i and @j then 'Late'
                    when min(time(DateofCheckin)) between @j and @n then 'HalfDay'
                    when DAYOFWEEK(Date(DateofCheckin)) =7 
                        and min(time(DateofCheckin)) >@j  then 'HalfDay'
                    else 'Absent' end as LoginStatus
      from CheckInLogs k Join
        where k.DateofCheckin='in'  and  
        DAYOFWEEK(Date(k.DateofCheckin)) != 1 and
        group BY date(k.DateofCheckin),k.EmpId) in
ON in.empid = out.empid
AND in.LogoutDate = out.LogoutDate

caseandgroup bysを最上位のクエリに移動することも検討します。

于 2013-07-01T09:45:02.163 に答える