datetime
当我只选择日期时间时,我需要显示。因为我尝试运行 SQL 但显示所有datetime
.
表Emp
:
EmpNo fullName
00001 Midna
00002 Klog
00003 Porla
00004 Seka
00005 Mila
表tFile
:
EmpNo cDate cTime
00001 2012-10-29 00:00:00.000 2012-10-29 07:52:00.000
00001 2012-10-29 00:00:00.000 2012-10-29 19:00:00.000
00002 2012-10-29 00:00:00.000 2012-10-29 07:40:00.000
00002 2012-10-29 00:00:00.000 2012-10-29 19:32:00.000
00005 2012-10-29 00:00:00.000 2012-10-29 07:58:00.000
00005 2012-10-29 00:00:00.000 2012-10-29 18:35:00.000
这段代码
SELECT
em.EmpNo as 'EmpNo',
case
when tf.cDate <> null then tf.cDate
else coalesce(tf.cDate, '2012-10-29')
end as 'cDate',
Min(tf.cTime) as 'timeIn',
Max(tf.cTime) as 'timeOut'
FROM
tFile tf
Full Outer join
Emp em On tf.EmpNo = em.EmpNo
Group By
em.EmpNo,tf.cDate
Order By
'EmpNo'
返回此结果:
EmpNo cDate timeIn timeOut
-------------------------------------------------------------------------------------
00001 2012-10-21 00:00:00.000 2012-10-21 07:22:00.000 2012-10-21 17:35:00.000
00001 2012-10-24 00:00:00.000 2012-10-24 07:30:00.000 2012-10-24 19:00:00.000
00001 2012-10-29 00:00:00.000 2012-10-29 07:52:00.000 2012-10-29 19:00:00.000
00002 2012-10-25 00:00:00.000 2012-10-25 07:58:00.000 2012-10-25 18:35:00.000
00002 2012-10-22 00:00:00.000 2012-10-22 08:04:00.000 2012-10-22 17:55:00.000
00002 2012-10-24 00:00:00.000 2012-10-24 08:00:00.000 2012-10-24 18:45:00.000
00002 2012-10-29 00:00:00.000 2012-10-29 07:40:00.000 2012-10-29 19:32:00.000
00003 2012-10-29 00:00:00.000 NULL NULL
00004 2012-10-29 00:00:00.000 NULL NULL
00005 2012-10-28 00:00:00.000 2012-10-28 07:30:00.000 2012-10-28 19:20:00.000
00005 2012-10-27 00:00:00.000 2012-10-27 07:38:00.000 2012-10-27 19:30:00.000
00005 2012-10-29 00:00:00.000 2012-10-29 07:58:00.000 2012-10-29 18:35:00.000
但我需要这个结果:
我选择日期前。2012-10-29
那么我只需要显示所有行2012-10-29
。
但是有些Empno
没有数据,2012-10-29
它被设置为NULL。
EmpNo cDate timeIn timeOut
---------------------------------------------------------------------------------------
00001 2012-10-29 00:00:00.000 2012-10-29 07:52:00.000 2012-10-29 19:00:00.000
00002 2012-10-29 00:00:00.000 2012-10-29 07:40:00.000 2012-10-29 19:32:00.000
00003 2012-10-29 00:00:00.000 NULL NULL
00004 2012-10-29 00:00:00.000 NULL NULL
00005 2012-10-29 00:00:00.000 2012-10-29 07:58:00.000 2012-10-29 18:35:00.000
谢谢你的时间。:)