同一个表中的最小和最大日期时间

时间:2015-01-16 19:36:10

标签: sql-server sql-server-2008 sql-server-2012

select WorkDay,
MIN(WorkDay) as dayOn,
MAX(WorkDay) as DayOff
from Records
where YEAR(WorkDay)=2014 and MONTH(WorkDay) =10 and EnNo=10
group by WorkDay

查询显示如下结果

WorkDays                          DayOn                     Dayoff
2014-10-04 08:13:00.000     2014-10-04 08:13:00.000    2014-10-04 08:13:00.000
2014-10-04 17:19:00.000     2014-10-04 17:19:00.000    2014-10-04 17:19:00.000

但我需要类似下面的数据

WorkDays                       DayOn                     Dayoff Oct
2014-10-04            2014-10-04 08:13:00.000       2014-10-04 17:19:00.000

有人请整理这个查询 谢谢,

2 个答案:

答案 0 :(得分:0)

select WorkDay = cast(floor(cast(WorkDay as float)) as datetime),
MIN(WorkDay) as dayOn,
MAX(WorkDay) as DayOff
from Records
where YEAR(WorkDay)=2014 and MONTH(WorkDay) =10 and EnNo=10
group by  cast(floor(cast(WorkDay as float)) as datetime)

答案 1 :(得分:0)

您也可以尝试以下

select CONVERT( date, WorkDay),
MIN(WorkDay) as dayOn,
MAX(WorkDay) as DayOff
from Records
where YEAR(WorkDay)=2014 and MONTH(WorkDay) =10 and EnNo=10
group by CONVERT( date,  WorkDay)