SQL查询以查找当天的第一个事件

时间:2013-09-15 23:36:23

标签: sql sql-server-2008

我正在尝试在我们的门系统数据库上运行SQL查询,以查看每个人上班的时间。目前我正在使用声明

Where DATEPART(hh, ev.dEvent_Date) between '5' and '10'

将结果限制在凌晨5点到10点之间,但约翰史密斯可能已经到外面接收某些东西或者休息一下,因此报告将在一天内为一个人提供多个条目。我有没有办法每天为每个人举办第一场比赛?我当前的查询在

之下
Select tFirstName as FirstName
    ,tLastName as LastName
    ,DATENAME(dw, ev.dEvent_Date) as [Day]
    ,CONVERT(varChar(11),ev.dEvent_Date) as [Date]
    ,CONVERT(varchar(2) ,DatePart(hh,devent_Date))+':'+ case when len(convert(varchar(5),DatePart(n,devent_Date))) = '1' then '0'+ convert(varchar(5),DatePart(n,devent_Date))else convert(varchar(5),DatePart(n,devent_Date)) end as [Time]

From dbo.tblevents as ev, dbo.tblEmployees as em
Where DATEPART(hh, ev.dEvent_Date) between '5' and '10'

AND ev.dEvent_Date between DATEADD(dd, -(DATEPART(dw, DATEADD(ww, -1, getdate()))-1), DATEADD(ww, -1, getdate())) and DATEADD(dd, 7-(DATEPART(dw, DATEADD(ww, -1, getdate()))), DATEADD(ww, -1, getdate()))
And em.iEmployeeNum = ev.iUserNum
AND tByte6 = 11
AND tByte8 = 22
Order by LastName, [Date]

2 个答案:

答案 0 :(得分:1)

你可以在一整天内完成这项任务。并简化您的查询:

Select tFirstName as FirstName, tLastName as LastName, DATENAME(dw, ev.dEvent_Date) as [Day]
        CONVERT(varChar(11), ev.dEvent_Date) as [Date],
        (CONVERT(varchar(2) , DatePart(hh, min(devent_Date)))+':'+
         case when len(convert(varchar(5), DatePart(n, min(devent_Date)))) = '1'
             then '0'+ convert(varchar(5), DatePart(n, min(devent_Date)))
             else convert(varchar(5), DatePart(n, min(devent_Date)))
         end
        ) as [Time]
From dbo.tblevents ev join
     dbo.tblEmployees em
     on em.iEmployeeNum = ev.iUserNum
Where ev.dEvent_Date between DATEADD(dd, -(DATEPART(dw, DATEADD(ww, -1, getdate()))-1), DATEADD(ww, -1, getdate())) and
                             DATEADD(dd, 7-(DATEPART(dw, DATEADD(ww, -1, getdate()))), DATEADD(ww, -1, getdate())) And 
tByte6 = 11 and tByte8 = 22
group by tFirstName, tLastName, cast(dv.dEvent_Date as date), DATENAME(dw, ev.dEvent_Date)
Order by LastName, [Date];

说实话,请使用以下内容将日期和时间格式化为单个列:

convert(varchar(16), min(ev.dEvent_date), 121);

答案 1 :(得分:0)

您可以使用分组并获取dEvent_Date的MIN:

Select tFirstName as FirstName
,tLastName as LastName
,DATENAME(dw, MIN(ev.dEvent_Date)) as [Day]
,CONVERT(varChar(11), MIN(ev.dEvent_Date)) as [Date]
,CONVERT(varchar(2) ,DatePart(hh, MIN(devent_Date)))+':'+ case when len(convert(varchar(5),DatePart(n, MIN(devent_Date)))) = '1' then '0'+ convert(varchar(5),DatePart(n, MIN(devent_Date)))else convert(varchar(5),DatePart(n, MIN(devent_Date))) end as [Time]

From dbo.tblevents as ev, dbo.tblEmployees as em
Where DATEPART(hh, ev.dEvent_Date) between '5' and '10'
AND ev.dEvent_Date between DATEADD(dd, -(DATEPART(dw, DATEADD(ww, -1, getdate()))-1), DATEADD(ww, -1, getdate())) 
AND DATEADD(dd, 7-(DATEPART(dw, DATEADD(ww, -1, getdate()))), DATEADD(ww, -1, getdate()))
AND em.iEmployeeNum = ev.iUserNum
AND tByte6 = 11
AND tByte8 = 22
GROUP BY tFirstName, tLastName
Order by LastName, [Date]