time表
刷卡时间1 刷卡时间2 刷卡时间3 刷卡时间4
9:06:00 12:19:00 15:08:00 17:33:00
7:08:00 12:24:00 14:06:00 17:39:00
14:10:00 17:37:00 NULL NULL
8:23:00 12:21:00 15:10:00 NULL
14:29:00 16:56:00 NULL NULL
我想计算有4次刷卡时间,并且上午和下午刷卡时间间隔大于150的为合格,做成这种效果
刷卡时间1 刷卡时间2 刷卡时间3 刷卡时间4 合格与否
9:06:00 12:19:00 15:08:00 17:33:00 不合格
7:08:00 12:24:00 14:06:00 17:39:00 合格
14:10:00 17:37:00 NULL NULL 不合格
8:23:00 12:21:00 15:10:00 NULL 不合格
14:29:00 16:56:00 NULL NULL 不合格
我做了两步
第一步:
select * ,datediff(N,刷卡时间1,刷卡时间2) as morning,datediff(N,刷卡时间3,刷卡时间4) as afternoon into time1 from time
第二步才能做出结果:
select *,case when morning>150 and afternoon>150 then 'yes' else 'no' end [合格与否] from time1
我想这样实现却不行,怎么才能一步实现呢,不用一个表做中转:
select * ,datediff(N,刷卡时间1,刷卡时间2) as morning,datediff(N,刷卡时间3,刷卡时间4) as afternoon,
case when morning>150 and afternoon>150 then 'yes' else 'no' end [是否合格] from time
------解决思路----------------------
select * ,datediff(N,刷卡时间1,刷卡时间2) as morning,datediff(N,刷卡时间3,刷卡时间4) as afternoon,
case when morning>150 and afternoon>150 then 'yes' else 'no' end [是否合格] from time
你上面的morning和afternoon是計算出來的,不是現有表time中的字段,會報錯的。如果想一步處理的話,可以這樣。
select * ,datediff(N,刷卡时间1,刷卡时间2) as morning,datediff(N,刷卡时间3,刷卡时间4) as afternoon,
case when datediff(N,刷卡时间1,刷卡时间2)>150 and datediff(N,刷卡时间3,刷卡时间4)>150 then 'yes' else 'no' end [是否合格] from time