Having some difficulties coming up with the most accurate way of calculating total login/logout times. Structure: CREATE TABLE dbo.AUDIT_EVENT( EVENT_ID numeric(19, 0) NOT NULL, PROFILE_ID numeric(19, 0) NULL, EVENT_TIMESTAMP datetime NULL, EVENT_TYPE_ID numeric(19, 0) NULL, PID numeric(19, 0) NULL, SDID numeric(19, 0) NULL, USER_ROLE_ID numeric(19, 0) NULL, PIDLINK numeric(19, 0) NULL, OUTCOME smallint NULL, EXPORTED varchar(1) CONSTRAINT [DF__AUDIT_EVENT__EXPORTED] DEFAULT 'N' NULL, UserIsRequestor bit NULL, CONSTRAINT PK_AUDIT_EVENT PRIMARY KEY CLUSTERED (EVENT_ID), CONSTRAINT RefPatientProfile356 FOREIGN KEY (PID) REFERENCES dbo.PatientProfile(PID), CONSTRAINT FK_AUDIT_EVENT_AUDIT_EVENT_TYPE FOREIGN KEY (EVENT_TYPE_ID) REFERENCES dbo.AUDIT_EVENT_TYPE(EVENT_TYPE_ID) ON UPDATE CASCADE, CONSTRAINT FK_AUDIT_EVENT_AUDIT_PROFILE FOREIGN KEY (PROFILE_ID) REFERENCES dbo.AUDIT_PROFILE(PROFILE_ID) ON UPDATE CASCADE, CONSTRAINT RefDOCUMENT359 FOREIGN KEY (SDID) REFERENCES dbo.DOCUMENT(SDID))go
CREATE TABLE dbo.AUDIT_EVENT_DETAIL( EVENT_ID numeric(19, 0) NOT NULL, EVENT_DETAIL_ID numeric(19, 0) NOT NULL, EVENT_PARAM varchar(60) NULL, EVENT_VALUE_1_PARAM varchar(60) NULL, EVENT_VALUE_1 varchar(4000) NULL, EVENT_VALUE_2_PARAM varchar(60) NULL, EVENT_VALUE_2 varchar(4000) NULL, EVENT_NOTE varchar(4000) NULL, CONSTRAINT PK_AUDIT_EVENT_DETAIL PRIMARY KEY CLUSTERED (EVENT_ID, EVENT_DETAIL_ID), CONSTRAINT RefAUDIT_EVENT375 FOREIGN KEY (EVENT_ID) REFERENCES dbo.AUDIT_EVENT(EVENT_ID))go
CREATE TABLE dbo.AUDIT_EVENT_TYPE( EVENT_TYPE_ID numeric(19, 0) NOT NULL, EVENT_TYPE_DESCRIPTION varchar(60) NULL, REPORT_GROUP numeric(19, 0) NULL, ENABLED bit CONSTRAINT [DF__AUDIT_EVENT_TYPE__ENABLED] DEFAULT 1 NOT NULL, ACTIONTYPE varchar(12) NULL, EXPORT bit CONSTRAINT [DF__AUDIT_EVENT_TYPE__EXPORTED] DEFAULT 0 NOT NULL, ExportChanged datetime NULL, VISIBLE bit CONSTRAINT [DF__AUDIT_EVENT_TYPE__VISIBLE] DEFAULT 1 NOT NULL, CONSTRAINT PK_AUDIT_EVENT_TYPE PRIMARY KEY CLUSTERED (EVENT_TYPE_ID) WITH FILLFACTOR = 80)go
CREATE TABLE dbo.AUDIT_PROFILE( PROFILE_ID numeric(19, 0) NOT NULL, LOGINNAME varchar(32) NULL, PVID numeric(19, 0) NULL, OS_USER_NAME varchar(30) NULL, OS_MACHINE_NAME varchar(64) NULL, OS_TERMINAL_NAME varchar(16) NULL, OS_PROGRAM_NAME varchar(64) NULL, WORKSTATION_NAME varchar(128) NULL, LOCID numeric(19, 0) NULL, CONSTRAINT PK_AUDIT_PROFILE PRIMARY KEY CLUSTERED (PROFILE_ID), CONSTRAINT RefUSR376 FOREIGN KEY (PVID) REFERENCES dbo.USR(PVID), CONSTRAINT RefLOCREG377 FOREIGN KEY (LOCID) REFERENCES dbo.LOCREG(LOCID))go
Here is what I got so far SELECT ae.EVENT_TIMESTAMP as Login_time, ae1.EVENT_TIMESTAMP as Logout_time, datediff(hh,ae.EVENT_TIMESTAMP,ae1.EVENT_TIMESTAMP) AS timediff, aet.EVENT_TYPE_DESCRIPTION, aed.EVENT_VALUE_1_PARAM, aed.EVENT_VALUE_1, aed.EVENT_PARAM, aed.EVENT_VALUE_2_PARAM, aed.EVENT_VALUE_2, aed.EVENT_DETAIL_ID, l.ABBREVNAME, pp.searchname, pp.patientid, u.FIRSTNAME, u.MIDDLENAME, u.LASTNAME, u.LOGINNAME INTO #temp FROM AUDIT_PROFILE ap INNER JOIN AUDIT_EVENT ae ON ap.PROFILE_ID = ae.PROFILE_ID INNER JOIN AUDIT_EVENT ae1 ON ap.PROFILE_ID = ae1.PROFILE_ID LEFT OUTER JOIN USR u ON ap.LOGINNAME = u.LOGINNAME INNER JOIN LOCREG l ON u.HOMELOCATION = l.LOCID LEFT OUTER JOIN AUDIT_EVENT_DETAIL aed ON ae.EVENT_ID = aed.EVENT_ID and ae1.EVENT_ID = aed.EVENT_ID INNER JOIN AUDIT_EVENT_TYPE aet ON ae.EVENT_TYPE_ID = aet.EVENT_TYPE_ID INNER JOIN AUDIT_EVENT_TYPE aet1 on ae1.EVENT_TYPE_ID = aet1.EVENT_TYPE_ID LEFT OUTER JOIN vPatientProfile pp ON ae.PID = pp.pid and ae1.PID = pp.pid WHERE ae.EVENT_TIMESTAMP between '2013-11-19 00:00:00' and '2013-11-19 23:59:59' AND ae1.EVENT_TIMESTAMP between '2013-11-19 00:00:00' and '2013-11-19 23:59:59' AND u.PVID in (1667380756709620) AND ae.EVENT_TYPE_ID in (100) AND ae1.EVENT_TYPE_ID in (103) select a.login_time, a.logout_time, Hours = datepart(hour,DateDif), Minutes = datepart(minute,DateDif), Seconds = datepart(second,DateDif)from ( select DateDif = Logout_time-Login_time, aa.* from ( -- Test Data Select Login_time = convert(datetime,t.Login_time), Logout_time = convert(datetime,t.Logout_time) from #temp t ) aa ) a drop table #temp
resultslogin_time logout_time Hours Minutes Seconds2013-11-19 11:57:08.410 2013-11-19 13:20:52.327 1 23 432013-11-19 11:57:08.410 2013-11-19 13:20:54.757 1 23 462013-11-19 11:57:08.410 2013-11-19 13:20:55.193 1 23 462013-11-19 11:57:08.410 2013-11-19 15:16:01.800 3 18 532013-11-19 11:57:08.410 2013-11-19 15:16:03.710 3 18 552013-11-19 11:57:08.410 2013-11-19 15:16:04.193 3 18 552013-11-19 13:27:07.730 2013-11-19 13:20:52.327 23 53 442013-11-19 13:27:07.730 2013-11-19 13:20:54.757 23 53 472013-11-19 13:27:07.730 2013-11-19 13:20:55.193 23 53 472013-11-19 13:27:07.730 2013-11-19 15:16:01.800 1 48 542013-11-19 13:27:07.730 2013-11-19 15:16:03.710 1 48 552013-11-19 13:27:07.730 2013-11-19 15:16:04.193 1 48 56
its not showing accurate enough results to calculate totals. any suggestions?