| Author |
Topic |
|
jafrywilson
Constraint Violating Yak Guru
379 Posts |
Posted - 2011-12-21 : 05:26:20
|
| I am having these two tablesmonitor_infoTranID Created_By Created_Date1 G165 2011-12-20 15:26:01.5602 G165 2011-12-20 16:22:20.7203 G165 2011-06-30 13:20:17.9939 S394 2011-06-30 16:48:02.7274 S394 2011-06-30 16:55:53.3132 S394 2011-06-30 16:56:09.43311 G165 2011-12-20 15:26:01.56011 G165 2011-07-05 17:54:31.36011 G165 2011-12-20 16:22:20.72011 G165 2011-12-20 16:22:20.72012 G165 2011-12-20 15:26:01.56012 G165 2011-12-20 16:22:20.720srl_no emp_id login_date login_time logout_time15 G165 2011-12-20 14:48:22.760 14:48:22.0000000 14:49:54.000000016 G165 2011-12-20 14:53:55.050 14:53:55.0000000 15:09:51.000000017 G165 2011-12-20 15:20:21.603 15:20:21.0000000 15:25:52.000000018 G165 2011-12-20 15:26:01.560 15:26:01.0000000 15:34:02.000000019 G165 2011-12-20 15:38:03.920 15:38:03.0000000 15:38:40.000000020 G165 2011-12-20 16:13:49.247 16:13:49.0000000 16:15:02.000000021 G165 2011-12-20 16:15:12.090 16:15:12.0000000 16:16:25.000000022 G165 2011-12-20 16:16:38.597 16:16:38.0000000 16:20:21.000000023 G165 2011-12-20 16:20:48.257 16:20:48.0000000 16:21:28.000000024 G165 2011-12-20 16:22:20.720 16:22:20.0000000 16:23:16.000000025 G165 2011-12-20 16:23:58.863 16:23:58.0000000 16:24:23.0000000I need to findout the number of (count(tranID)) processin a particular loginneed to check the monitor_info table created_date field based on login and logout time |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-12-21 : 12:07:52
|
| so what should be output out of above data?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
jafrywilson
Constraint Violating Yak Guru
379 Posts |
Posted - 2011-12-26 : 05:32:24
|
| Emp_id Login Time Logout_time Processed applicationG165 14:48:22 1 4:49:54 0G165 14:53:55 15:09:51 0G165 15:20:21 15:25:52 0G165 15:26:01 15:34:02 1G165 15:38:03 15:38:40 0G165 16:13:49 16:15:02 0G165 16:15:12 16:16:25 0G165 16:16:38 16:20:21 0G165 16:20:48 16:21:28 0G165 16:22:20 16:23:16 1G165 16:23:58 16:24:23 0 |
 |
|
|
jafrywilson
Constraint Violating Yak Guru
379 Posts |
Posted - 2011-12-26 : 05:45:59
|
| Is it possible ? to compare time duration in a column (that contains date and time)(created_date in monitor_info) with other two columns (login,log_out in login_table)Im expecting the followin result No of enteries in monitor_info table (based on created_date field) between login,logout times of each rows in login table |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-12-26 : 05:51:20
|
| [code]SELECT e.Emp_id, e.Login Time, e.Logout_time,CASE WHEN m.Cnt> 0 THEN 1 ELSE 0 END AS [Processed application] FROM EMployeeLoginTable eOUTER APPLY (SELECT COUNT(*) AS Cnt FROM monitor_info WHERE Created_By = e.Emp_Id AND Created_Date = e.Login_Date )m[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
jafrywilson
Constraint Violating Yak Guru
379 Posts |
Posted - 2011-12-26 : 06:04:33
|
| Thanks for your replyI lastly executed the above query against my db which contains the following rowsTran_ ID Created_By Created_Date 1552 TB0012 2011-12-26 16:28:33.613 1551 TB0012 2011-12-26 16:28:50.733 1549 TB0012 2011-12-26 16:29:12.013 srl_no emp_id login_date login_time logout_time53 TB0012 2011-12-26 16:27:43.217 16:27:43.0000000 16:29:18.0000000but i got this resultEmp_id login_time Logout_time Processed applicationTB0012 16:27:43.0000000 16:29:18.0000000 0 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-12-26 : 06:39:47
|
| i gave suggestion based on your posted query in first post. Is that not your real scenario?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
jafrywilson
Constraint Violating Yak Guru
379 Posts |
Posted - 2011-12-26 : 06:48:13
|
| Both are same scenarios.. Latest one is directly copied from the output .. TB0012 is also an Employee id |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-12-26 : 06:51:50
|
| ok..and is that not your expected result?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
jafrywilson
Constraint Violating Yak Guru
379 Posts |
Posted - 2011-12-26 : 06:54:20
|
| Lastly I tested my application in this login time (16:27:43.0000000 16:29:18.0000000 ) and I have worked out 3 process (in monitor_info) .. I need the count that I worked in the last login |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-12-26 : 07:01:53
|
| so what should be the output?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
jafrywilson
Constraint Violating Yak Guru
379 Posts |
Posted - 2011-12-26 : 07:05:52
|
| Emp_id login_time Logout_time Processed applicationTB0012 16:27:43.0000000 16:29:18.0000000 3This is my most expected outputThanks for your response |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-12-26 : 07:19:35
|
| what are datatypes of login_time, logout_time fields?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
jafrywilson
Constraint Violating Yak Guru
379 Posts |
Posted - 2011-12-26 : 07:22:02
|
| both are in time (sql 2008) |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-12-26 : 11:12:57
|
| then my logic should work fine.------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
jafrywilson
Constraint Violating Yak Guru
379 Posts |
Posted - 2011-12-27 : 01:09:22
|
quote: Originally posted by visakh16
SELECT e.Emp_id, e.Login Time, e.Logout_time,CASE WHEN m.Cnt> 0 THEN 1 ELSE 0 END AS [Processed application] FROM EMployeeLoginTable eOUTER APPLY (SELECT COUNT(*) AS Cnt FROM monitor_info WHERE Created_By = e.Emp_Id AND Created_Date = e.Login_Date )m ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
Your logic is fine ..Created_Date = e.Login_Date these two dates are not matched ..Because both are date&time fields... It checks for the time alsoIf we compare only the dates means it will work.. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-12-27 : 02:59:06
|
quote: Originally posted by jafrywilson
quote: Originally posted by visakh16
SELECT e.Emp_id, e.Login Time, e.Logout_time,CASE WHEN m.Cnt> 0 THEN 1 ELSE 0 END AS [Processed application] FROM EMployeeLoginTable eOUTER APPLY (SELECT COUNT(*) AS Cnt FROM monitor_info WHERE Created_By = e.Emp_Id AND Created_Date = e.Login_Date )m ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
Your logic is fine ..Created_Date = e.Login_Date these two dates are not matched ..Because both are date&time fields... It checks for the time alsoIf we compare only the dates means it will work..
but your sample data showed both of them as having same date and time parts.anyways use below modified suggestionSELECT e.Emp_id, e.Login Time, e.Logout_time,CASE WHEN m.Cnt> 0 THEN 1 ELSE 0 END AS [Processed application] FROM EMployeeLoginTable eOUTER APPLY (SELECT COUNT(*) AS Cnt FROM monitor_info WHERE Created_By = e.Emp_Id AND DATEADD(dd,0,Created_Date) = DATEADD(dd,0,e.Login_Date) )m ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
jafrywilson
Constraint Violating Yak Guru
379 Posts |
Posted - 2011-12-28 : 02:36:18
|
| It also returns the same result ...I tried this DATEADD(D, 0, DATEDIFF(D, 0, Created_Date)) = DATEADD(D, 0, DATEDIFF(D, 0, e.Login_Date)) instead of DATEADD(dd,0,Created_Date) = DATEADD(dd,0,e.Login_Date)Then it results the no of applications as 1.. But my expected result is 3 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-12-28 : 03:39:49
|
| [code]SELECT e.Emp_id, e.Login Time, e.Logout_time,m.Cnt AS [Processed application] FROM EMployeeLoginTable eOUTER APPLY (SELECT COUNT(*) AS Cnt FROM monitor_info WHERE Created_By = e.Emp_Id AND DATEADD(dd,0,Created_Date) = DATEADD(dd,0,e.Login_Date) AND CAST(Created_Date as time) >= e.login_time AND CAST(Created_Date as time) <= e.logout_time)m[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
jafrywilson
Constraint Violating Yak Guru
379 Posts |
Posted - 2012-01-10 : 00:15:18
|
| I modified the query as like this ... SELECT e.Emp_id, e.Login_Time, e.Logout_time,m.Cnt AS [Processed application] FROM login_history eOUTER APPLY (SELECT COUNT(*) AS Cnt FROM monitor_info WHERE Created_By = e.Emp_Id and Year(Created_Date) = Year(Login_Date) AND Month(Created_Date) = Month(Login_Date) AND Day(Created_Date) = Day(Login_Date) )mthank you so much visakh.... |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-01-10 : 04:01:06
|
| wc------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
Next Page
|