开发者

Problem in Getting the DateTime Variable from the Database

开发者 https://www.devze.com 2023-02-19 17:37 出处:网络
I used to get the Login Time of a user when he Logsin at the First Time to the Syatem, all the activities the user is doing will be stored under a seperate column called \'Activity\'. My Problem is th

I used to get the Login Time of a user when he Logsin at the First Time to the Syatem, all the activities the user is doing will be stored under a seperate column called 'Activity'. My Problem is the User can LogOn to the System many times in a Particular Day, but i want to retrieve only the First Logon Time. The LogOn timings are appearing when i write a query for getting the Timings based on Activity are Like below.

                Time             User                   Activity
          '3/23/2011 9:55:00AM'   kk                    LogOn
          '3/23/2011 5:30:00PM'   kk                    LogOn

I write a Query to 开发者_JS百科retrieve these values like this

Select StartDateTime,User,Activity from AgentLog where Activity='LogOn'. So, any one can give me Small help that how can i retrieve only the First LogOn time for the User given. I am Using Sql Server 2008, There will be Somany days, the User Logging On, so for every day i need to get the Logon Time


Get the earliest time for each day they logged in by using the following grouping:

Select min(StartDateTime)
from AgentLog 
where Activity='LogOn' and
  User = 'Username'
group by datepart(day, StartDateTime), 
   datepart(month, StartDateTime), 
   datepart(year, StartDateTime)


If you won't store times as a string but as a timestamp (in seconds) it would be easy to just select the data:

Select StartDateTime,Activity from AgentLog where Activity='LogOn' AND time>timeofday ORDER by time LIMIT 1

Where timeofday is the timestamp of the current day at midnight

0

精彩评论

暂无评论...
验证码 换一张
取 消