I have a query for sql server that returns this “table” below. The query uses some joins to link the data together. What I’m trying to do is add up each interviewers logged in time and in addition turning NULL into the time at this moment.
INTERVIEWER NUM_COMPLETES projectId loginDateTime logoutDateTime 002 2 43407 2011-09-20 22:59:15.193 NULL 004 1 43407 2011-09-20 23:06:32.287 NULL 007 1 43407 2011-09-20 23:02:30.930 NULL 007 1 43407 2011-09-20 00:37:01.213 2011-09-20 01:15:00.197 010 3 43407 2011-09-20 23:04:46.547 NULL 013 1 43407 2011-09-20 00:36:16.923 2011-09-20 01:14:59.440 029 3 43407 2011-09-20 22:58:55.083 2011-09-20 23:30:56.987 029 3 43407 2011-09-20 23:31:05.243 2011-09-21 00:02:27.560 029 3 43407 2011-09-21 00:02:55.527 NULL 032 1 43407 2011-09-20 22:59:19.590 NULL 036 1 43407 2011-09-20 23:02:33.497 NULL 041 1 43407 2011-09-20 22:59:55.020 NULL
Should turn into
INTERVIEWER NUM_COMPLETES projectId TotalTime 002 2 43407 8 004 1 43407 10 007 1 43407 4 008 1 43407 7
It’s like a date diff that is summed up per interviewer.
Here is a sample + sanitized tsql statement that I am using
SELECT tbl1.INTERVIEWER, tbl1.NUM_COMPLETES, tbl2.projectId, tbl2.loginDateTime, tbl2.logoutDateTime
FROM
(
SELECT k_Id, FirstName AS INTERVIEWER, COUNT(result) AS NUM_COMPLETES
FROM db1_table, db2_table
WHERE k_Id=Interviewer
GROUP BY k_Id, FirstName, result
) AS tbl1
INNER JOIN
(
SELECT projectId, loginDateTime, logoutDateTime, userId
FROM session_tbl
WHERE loginDateTime >= '2011-09-20 0:0:0.0' AND projectId = '43407'
) AS tbl2
ON tbl1.k_Id=tbl2.userId
ORDER BY INTERVIEWER
Thank you for any help — I’ve been struggling with this the past couple of days!
You should be able to use a standard
GROUP BYto do this.