Author |
Topic |
baburk
Posting Yak Master
108 Posts |
Posted - 2010-09-10 : 05:38:09
|
Hi All, DECLARE @Lock TABLE(LogInID INT, UserName VARCHAR(50), IpAddress VARCHAR(50), LogInDate DATETIME)INSERT INTO @LockSELECT 10, 'rkbabu', '111.111.111.111', 'Aug 24 2010 1:05AM' UNION ALLSELECT 10, 'rkbabu', '111.111.111.111', 'Aug 24 2010 2:00AM' UNION ALLSELECT 10, 'rkbabu', '111.111.111.111', 'Aug 24 2010 2:14AM' UNION ALLSELECT 10, 'rkbabu', '111.111.111.111', 'Aug 24 2010 3:02AM' UNION ALLSELECT 11, 'rkbabu', '111.111.111.111', 'Aug 24 2010 11:38PM'DECLARE @UnLock TABLE(LogInID INT, UserName VARCHAR(50), IpAddress VARCHAR(50), LogInDate DATETIME)INSERT INTO @UnLockSELECT 10, 'rkbabu', '111.111.111.111', 'Aug 24 2010 1:12AM' UNION ALLSELECT 10, 'rkbabu', '111.111.111.111', 'Aug 24 2010 2:03AM' UNION ALLSELECT 10, 'rkbabu', '111.111.111.111', 'Aug 24 2010 2:43AM' UNION ALLSELECT 10, 'rkbabu', '111.111.111.111', 'Aug 24 2010 3:02AM' UNION ALLSELECT 11, 'rkbabu', '111.111.111.111', 'Aug 24 2010 11:50PM'SELECT * FROM @LockSELECT * FROM @UnLock I want to get the result as difference between 'Aug 24 2010 1:05AM' in @Lock and 'Aug 24 2010 1:12AM' in @UnLock'Aug 24 2010 2:00AM' in @Lock and 'Aug 24 2010 2:03AM' in @UnLock and so on.We should use group by LogInDate, UserName, IpAddressFinnally the difference of time should be sumed for LogInDate, UserName, IpAddress Thanks,Babu Kumarasamy. |
|
Sachin.Nand
2937 Posts |
Posted - 2010-09-10 : 05:41:02
|
What should be your expected o/p?Limitations live only in our minds. But if we use our imaginations, our possibilities become limitless. PBUH |
 |
|
baburk
Posting Yak Master
108 Posts |
Posted - 2010-09-10 : 06:56:36
|
quote: Originally posted by Idera What should be your expected o/p?Limitations live only in our minds. But if we use our imaginations, our possibilities become limitless. PBUH
00.51 rkbabu 111.111.111.111 2010-08-24 00:00:00.000 |
 |
|
Sachin.Nand
2937 Posts |
Posted - 2010-09-10 : 07:25:24
|
00.51 rkbabu 111.111.111.111 2010-08-24 00:00:00.000How are you getting the red value?Limitations live only in our minds. But if we use our imaginations, our possibilities become limitless. PBUH |
 |
|
Sachin.Nand
2937 Posts |
Posted - 2010-09-10 : 08:21:23
|
Except for the Logindate part below is the querySELECT Sum(diff), username, ipaddress FROM (SELECT l.logindate, l.username, l.ipaddress, (Datediff(hh,l.logindate,u.logindate) * 60 + Datediff(MINUTE,l.logindate,u.logindate)%60) diff FROM (SELECT *, Row_number() OVER(ORDER BY logindate) AS lid FROM @Lock) l INNER JOIN (SELECT *, Row_number() OVER(ORDER BY logindate) AS ulid FROM @UnLock) u ON u.ulid = l.lid) t GROUP BY username, ipaddress Limitations live only in our minds. But if we use our imaginations, our possibilities become limitless. PBUH |
 |
|
CSears
Starting Member
39 Posts |
Posted - 2010-09-10 : 23:23:38
|
quote: Originally posted by Idera Except for the Logindate part below is the querySELECT Sum(diff), username, ipaddress, CONVERT(VARCHAR(10), logindate, 120) FROM (SELECT l.logindate, l.username, l.ipaddress, (Datediff(hh,l.logindate,u.logindate) * 60 + Datediff(MINUTE,l.logindate,u.logindate)%60) diff FROM (SELECT *, Row_number() OVER(ORDER BY logindate) AS lid FROM @Lock) l INNER JOIN (SELECT *, Row_number() OVER(ORDER BY logindate) AS ulid FROM @UnLock) u ON u.ulid = l.lid) t GROUP BY username, ipaddress, CONVERT(VARCHAR(10), logindate, 120) Limitations live only in our minds. But if we use our imaginations, our possibilities become limitless. PBUH
Bold faced sections are those that are added to include the date section of the code. This will be a text version that you will be able to convert to datetime format if required. The convert function will allow you to chop off the time portion so you are left with the ssum of lock times grouped by IP address, User, and Date. |
 |
|
|
|
|