0

我正在尝试在我们的门系统数据库上运行 SQL 查询,以查看每个人什么时候开始工作。目前我只是在使用语句

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

将结果限制在上午 5 点到 10 点之间,但 John Smith 可能已经到外面去接东西或休息一下,因此报告将在一天内为一个人提供多个条目。有没有一种方法可以让我每天为每个人安排第一个活动?我当前的查询如下

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]
4

2 回答 2

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);
于 2013-09-16T00:26:13.960 回答
0

您可以使用分组并获取 dEvent_Date 的最小值:

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]
于 2013-09-16T00:02:31.707 回答