0
╔═══════╦═══════════╦═══════╦══════╦═══════════════╗
║ EMPID ║   DATE    ║ TIME  ║ TYPE ║    STATUS     ║
╠═══════╬═══════════╬═══════╬══════╬═══════════════╣
║     5 ║ 2012-03-1 ║ 09:30 ║ in   ║ Ontime        ║
║     5 ║ 2012-03-1 ║ 14:00 ║ out  ║ Early Leaving ║
║     6 ║ 2012-03-1 ║ 09:30 ║ in   ║ Ontime        ║
║     6 ║ 2012-03-1 ║ 17:40 ║ out  ║ Ontime        ║
║     7 ║ 2012-03-1 ║ 10:30 ║ in   ║ LateComing    ║
║     7 ║ 2012-03-1 ║ 13:00 ║ out  ║ EarlyLeaving  ║
╚═══════╩═══════════╩═══════╩══════╩═══════════════╝

我现在正在让员工签入并检查状态我需要的是,我想比较状态列的值并显示结果

在 select 中使用 case 语句的示例:

when Min(status)='Ontime' and max(status)='Ontime' then 'Present'

when Min(status)='Ontime' and max(status)='Early Leaving' then 'Left Early'

我需要最终输出为:

╔═══════╦═══════════╦═══════╦══════╦═══════════════╦════════════╗
║ EMPID ║   DATE    ║ TIME  ║ TYPE ║    STATUS     ║ DAYSTATUS  ║
╠═══════╬═══════════╬═══════╬══════╬═══════════════╬════════════╣
║     5 ║ 2012-03-1 ║ 09:30 ║ in   ║ Ontime        ║            ║
║     5 ║ 2012-03-1 ║ 14:00 ║ out  ║ Early Leaving ║ Left Early ║
║     6 ║ 2012-03-1 ║ 09:30 ║ in   ║ Ontime        ║            ║
║     6 ║ 2012-03-1 ║ 17:40 ║ out  ║ Ontime        ║ present    ║
║     7 ║ 2012-03-1 ║ 10:30 ║ in   ║ LateComing    ║            ║
║     7 ║ 2012-03-1 ║ 13:00 ║ out  ║ EarlyLeaving  ║ Absent     ║
╚═══════╩═══════════╩═══════╩══════╩═══════════════╩════════════╝
4

3 回答 3

3
SELECT  a.*,
        CASE WHEN a.type = 'IN' THEN ''
            ELSE
            CASE WHEN b.IN = 'Ontime' AND b.Out = 'Ontime' THEN 'Present'
                WHEN b.IN = 'Ontime' AND b.Out = 'Early Leaving' THEN 'Left Early'
                ELSE 'Absent'
            END
        END DAYSTATUS
FROM    tableName a
        INNER JOIN
        (
            SELECT  EmpID,
                    MAX(CASE WHEN Type = 'IN' THEN status END) `IN`,
                    MAX(CASE WHEN Type = 'OUT' THEN status END) `OUT`
            FROM    TableName
            GROUP   BY EmpID
        ) b ON a.EmpID = b.EmpID 

输出

╔═══════╦═══════════╦═══════╦══════╦═══════════════╦════════════╗
║ EMPID ║   DATE    ║ TIME  ║ TYPE ║    STATUS     ║ DAYSTATUS  ║
╠═══════╬═══════════╬═══════╬══════╬═══════════════╬════════════╣
║     5 ║ 2012-03-1 ║ 09:30 ║ in   ║ Ontime        ║            ║
║     5 ║ 2012-03-1 ║ 14:00 ║ out  ║ Early Leaving ║ Left Early ║
║     6 ║ 2012-03-1 ║ 09:30 ║ in   ║ Ontime        ║            ║
║     6 ║ 2012-03-1 ║ 17:40 ║ out  ║ Ontime        ║ Present    ║
║     7 ║ 2012-03-1 ║ 10:30 ║ in   ║ LateComing    ║            ║
║     7 ║ 2012-03-1 ║ 13:00 ║ out  ║ EarlyLeaving  ║ Absent     ║
╚═══════╩═══════════╩═══════╩══════╩═══════════════╩════════════╝
于 2013-05-30T06:37:50.343 回答
1

尝试使用案例和变量

     select @empId:=EmpId as EmpId,@date:=date as Date, time, type,@status:=status as status,
   case type 
     when 'out'
        then
         case(select status from mytable where empid = @empId AND date = @date AND type = 'in')
         when 'Ontime' then
            case(@status)
              when 'EarlyLeaving' then 'EarlyLeaving'
              when 'ontime'       then 'Present'
             end
          when 'LateComing' then
             case(@status)
              when 'EarlyLeaving' then 'Absent'
             end
         end 
     when 'in' then ''
  end as daystatus    
from mytable

SQL 小提琴演示

输出

╔═══════╦═══════════╦═══════╦══════╦══════════════╦══════════════╗
║ EMPID ║   DATE    ║ TIME  ║ TYPE ║    STATUS    ║  DAYSTATUS   ║
╠═══════╬═══════════╬═══════╬══════╬══════════════╬══════════════╣
║     5 ║ 2012-03-1 ║ 09:30 ║ in   ║ Ontime       ║              ║
║     5 ║ 2012-03-1 ║ 14:00 ║ out  ║ EarlyLeaving ║ EarlyLeaving ║
║     6 ║ 2012-03-1 ║ 09:30 ║ in   ║ Ontime       ║              ║
║     6 ║ 2012-03-1 ║ 17:40 ║ out  ║ Ontime       ║ Present      ║
║     7 ║ 2012-03-1 ║ 10:30 ║ in   ║ LateComing   ║              ║
║     7 ║ 2012-03-1 ║ 13:00 ║ out  ║ EarlyLeaving ║ Absent       ║
╚═══════╩═══════════╩═══════╩══════╩══════════════╩══════════════╝
于 2013-05-30T09:04:52.603 回答
0

试试这个逻辑

when TYPE = 'out' and STATUS ='Early Leaving' then 'Left Early'

when TYPE = 'out' and STATUS ='Ontime' then 'present'

Else 'Absent'
于 2013-05-30T06:40:18.290 回答