# Sql-server – Joining 4 tables and 2 counts

countjoin;reportingsql server

I have four tables.

UserMaster
----------
UserKey - PK
UserID - UNIQUE

UserLogTransaction
------------------
UserID - FK:UserMaster.UserID
LogoutTime

CallCenter
----------
CallKey - PK
PickedUserKey - FK:UserMaster.UserKey
PickedTime
StartTime
EndTime

JobCardMaster
-------------
JCKey
RecordedUserKey - FK:UserMaster.UserKey
CreatedTime
CallKey - FK:CallCenter.CallKey


This is the scenario: Each user logs in and attends calls. Job cards are created for some of the calls not for all calls.

Problem: I want to get a report which has the following information

UserName | LoginTime | LogoutTime | NoOfCallsAnswered^ | NoOfJobCardsCreated^ | AverageCallDuration^


^ These columns are calculated per session per user. A session is a row from the UserLogTransaction table.

I tried with several attempts and they tend to take around 1 minute to run. So, I think there is some problem with my approachs.

SELECT UM.UserID, ULT.LogInTime, ULT.LogOutTime, COUNT(CC.PickedUserKey)
FROM dbo.UserMaster UM
JOIN dbo.UserLogTransactions ULT
ON ULT.UserID = UM.UserID
JOIN dbo.CallCenter CC
ON CC.PickedUserKey = UM.UserKey
JOIN dbo.JobCardMaster JC
ON JC.RecordedUserKey = UM.UserKey
AND CC.PickedTime < ULT.LogOutTime)
AND JC.RecordedUserKey < ULT.LogOutTime)
GROUP BY CC.PickedUserKey, UM.UserID, ULT.LogInTime, ULT.LogOutTime


I am stuck on this query for several days. Appreciate any helps or hints.

Can you try using subquery? I can better design it if I have the table and sample data created on my DB server. But below is just a conceptual model which you can try on your server first.

SELECT