| Author |
Topic |
|
amodi
Yak Posting Veteran
83 Posts |
Posted - 2009-07-06 : 05:06:21
|
| Hello Friends, I have a table named "UserTimeInOut" with the following columns:EmpId,LoginTime,LogoutTime,DepartmentId,diffSeconds, -- Deifference between LoginTime and LogoutTime by using (DateDiff(second, @LoginTime, @LogoutTime)) I want to write a stored procedure that take three input parameters( say @DepartmentId, @FromDate and ToDate) and return the list of Employees in a Department(@DepartmentId), with total time for each EmployeeData in the table is as follows:EmpId--LoginTime---------------LogoutTime-------------DepartmentId---diffSeconds2400----6/8/2009 11:00:00 AM----6/8/2009 12:09:11 PM----1----------------41512400----6/9/2009 10:17:55 AM----6/9/2009 11:09:11 AM----1----------------30762411----6/8/2009 07:07:55 AM----6/8/2009 12:59:11 PM----1----------------210762411----6/9/2009 11:07:55 AM----6/9/2009 11:09:11 AM----1----------------762400----6/12/2009 09:07:55 AM---6/12/2009 11:49:11 AM---1----------------9676I want output to be:EmpId---TotalTime2400-----04:41:432411-----05:52:32Thanks. |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-07-06 : 05:11:13
|
SELECT EmpID, CONVERT(CHAR(8), DATEADD(SECOND, SUM(DATEDIFF(SECOND, LoginTime, LogoutTime)), 0), 108)FROM UserTimeInOutGROUP BY EmpID Microsoft SQL Server MVPN 56°04'39.26"E 12°55'05.63" |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
|
|
amodi
Yak Posting Veteran
83 Posts |
Posted - 2009-07-06 : 05:23:21
|
| Thanks Peso. |
 |
|
|
amodi
Yak Posting Veteran
83 Posts |
Posted - 2009-07-06 : 05:56:53
|
| Hello Peso.I want the result between date range. How to do that? |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-07-06 : 05:59:44
|
[code]WHERE LoginTime >= @FromDate AND LogoutTime <= @ToDate[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
amodi
Yak Posting Veteran
83 Posts |
Posted - 2009-07-06 : 06:08:39
|
| Thanks khtan. |
 |
|
|
amodi
Yak Posting Veteran
83 Posts |
Posted - 2009-08-03 : 07:31:10
|
| Hello friends,The below query is working absolutely fine:SELECT EmpID, CONVERT(CHAR(8), DATEADD(SECOND, SUM(DATEDIFF(SECOND, LoginTime, LogoutTime)), 0), 108)FROM LogInsGROUP BY EmpIDbut when i want the selection between date range. as follows, its not working:SELECT EmpID, CONVERT(CHAR(8), DATEADD(SECOND, SUM(DATEDIFF(SECOND, LoginTime, LogoutTime)), 0), 108)FROM LogInsWHERE LoginTime between 06-22-2009 and 07-18-2009GROUP BY EmpID its not showing even a single record.Note: LoginTime is of type datetime contains records as follows:22-06-2009 10:42:02 AM22-06-2009 10:48:52 AM22-06-2009 12:35:13 PM22-06-2009 12:36:18 PM22-06-2009 12:38:15 PM |
 |
|
|
Mangal Pardeshi
Posting Yak Master
110 Posts |
Posted - 2009-08-03 : 07:56:05
|
| pass the dates in language independant format, and see whther thts u help u or notWHERE LoginTime >='20090622' and loginformat < '20090719'see http://www.karaszi.com/SQLServer/info_datetime.aspMangal Pardeshihttp://mangalpardeshi.blogspot.com |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-08-03 : 07:59:03
|
[code]SELECT EmpID, CONVERT(CHAR(8), DATEADD(SECOND, SUM(DATEDIFF(SECOND, LoginTime, LogoutTime)), 0), 108)FROM LogInsWHERE LoginTime between 06-22-2009 and 07-18-2009GROUP BY EmpID [/code]That is not a date, but integer subtraction 6 minus 22 minus 2009.Specify the date in ISO format YYYYMMDD and enclosed in single quote as what Mangal as suggested KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
Mangal Pardeshi
Posting Yak Master
110 Posts |
Posted - 2009-08-03 : 08:08:32
|
| I also forget to add this, be careful with these date calculations and BETWEENIf you are looking for getting the rows from 07-18-2009 also then your condition should look like thisWHERE LoginTime >='20090622' and loginformat <= '20090718 23:59:59 997'or add one day and do LESS THAN WHERE LoginTime >='20090622' and loginformat < '20090719'And whenever possible avoid using BETWEEN, instead use >= AND < operatorsMangal Pardeshihttp://mangalpardeshi.blogspot.com |
 |
|
|
amodi
Yak Posting Veteran
83 Posts |
Posted - 2009-08-03 : 08:25:29
|
| Thank you sir, i will try that and get back to you(both). |
 |
|
|
|