简体   繁体   English

sql datetime获取每天的第一个值

[英]sql datetime to get the first value of each day

i have a sql server table named 我有一个名为的SQL Server表

Datetime
=======
column UserID(int), DateTime(datetime)

it stores attendance data for users. 它存储用户的出勤数据。 each user have multiple datetime data for each day. 每个用户每天都有多个日期时间数据。 i need the first DateTime data for each day for a given user between a given date range then i need them to compare with a particular time say 08:00:00 to get the late attendance. 我需要给定日期范围内给定用户每天的第一个DateTime数据,然后我需要它们与特定时间(例如08:00:00进行比较,以获取较晚的出勤率。 how this can be done? 该怎么做?

If understood correctly here is the query for you 如果正确理解,这里是您的查询

SELECT  UserID,
        CONVERT(VARCHAR(10),[datetime],111) as [AttendaceDate],
        MIN([datetime]) as [Date In], DATEDIFF(mi, MIN([datetime]), Convert(Datetime, CONVERT(VARCHAR(10),[datetime],111) + ' 8:00 am')) as [Minutes Late]
  FROM  TestTable
GROUP BY UserID, CONVERT(VARCHAR(10),[datetime],111)
SELECT  UserID,
        CONVERT(VARCHAR(10),Dateime,111),
        MIN(Datetime)
  FROM  table
GROUP BY UserID, CONVERT(VARCHAR(10),Dateime,111)
WHERE   Datetime BETWEEN '1/1/2012' AND '1/5/2012'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM