SQL Service Level Query - sql

I am teaching myself SQL and came across this service level question and was stumped. I was hoping for some assistance on it.
I am given a table with three fields.
TicketNumber (Number) ex. 53055
CreatedAt (timestamp field) ex 2015-09-16 12:47
Sender (User or Agent) ex User
The goal is to calculate service level completion. A ticket is created by a user and given a number, and a response by an agent must be given within 6 hours.
Using the formula:
n_agents_reply_back_to_user_within_6hrs / n_contacts_from_user
Now I understand that the denominator in this formula is simply
Select COUNT(Sender)
From Service_Table
Where Sender Like 'User'
The numerator is giving me a lot of issues, and I was hoping someone could walk me through it. I understand I need to identify rows with the same Ticket Number, identify what time the user sent the ticket, and identify what time the agent responded and do a difference of it, and if it is <=6 then count it, otherwise don't.
As a beginner I am having quite a bit of trouble grasping how to write such a query. Any help is appreciated. Thank you

I am not sure what exactly you are trying to achieve but you can start with something like this.
select user.TicketNumber,user.CreatedAt-agent.CreatedAt from
(Select TicketNumber ,CreatedAt ,Sender from Service_Table user
Where Sender Like 'User') user left outer join
(Select TicketNumber ,CreatedAt ,Sender from Service_Table agent Where Sender Like 'Agent') agent
on user.TicketNumber =agent.TicketNumber

Related

MS Access - Take hourly rate and employee name from another table and multiply by time taken

I am not too sure how to approach this method and was wondering if anyone could assist me.
Basically, I have a table (tblLogInspection) where the data is input via the user from separate form (frmAddInspectionRecord). I have used vba so that upon a button click all data that has been input will transfer to the table.
What I have is another table (tblemployeedetails), i.e. their name and hourly rate. On (frmAddInspectionRecord) there is a combo box that is quiered from (tblemployeedetails) where the user can select which employee has conducted the work. Upon this data being added to (tblLogInspection) I would like a column which takes the hourly rate of the employee that has conducted the work from (tblemployeedetails) and multiply it by the TIMETAKEN column in (tblLogInspection).
I hope that I have explained this with enough detail. If anyone could help guide me in the right direction I would be so grateful!
I think, the most straightforward solution to this would be a query (as already suggested by Stewart Ross)
Imagine your tables are set up like this:
tblemployeedetails:
Employee_ID | Employee_Name | HourlyRate
tblLogInspection:
Inspection_ID | Employee_ID | TimeTaken
Now, set up a query with a calculated field:
SELECT q2.Inspection_ID AND q2.TimeTaken * q1.HourlyRate
FROM tblemployeedetails AS q1
INNER JOIN tblLogInspection AS q2 ON q1.Employee_ID = q2.Employee_ID

Access/SQL Select Query - Return "Most Like" Value Only

We have a chargeback process in an AccessDB where Departments must approve the expenses entered by another department. We only want a single 'default' approver, but the way the data has been set-up and the query we currently use to fill in the approver returns multiple results.
In the tUserSec table, for example, we have two columns. Name(UserIDX) and UserCode
User1 - 550*
User2 - 55003*
The idea here being that User1 is the Director and so is a 'catchall' for everything in this department, while User2 is a Manager and is specifically assigned to a narrower division. Departments are always 7 characters total.
Say the Department is 5500309, the idea is that User2 should populate as the approver since their code is most closely matched to the Department ID. However, using the "Like" criteria returns both users and the form appears to select one of the two users at random with no rhyme or reason that I can determine. It always selects User1 for 5500309 but always selects User2 for 5500301, despite there being no further delineation - but ideally User1 shouldn't be populating at all unless no one else matches closer.
Below is a simplified version of the SQL, I cut out some other stuff that muddies the situation:
SELECT TDepts.Dept, TDepts.DDescr, tUserSec.UserIDX
FROM tUserSec, TDepts
WHERE (((TDepts.Dept) Like [usercode] & "*"));
How can I change this up so that I only pull in the UserID who is most like the usercode? I tried to figure out a way to pull in the UserID based on the length or max of the usercode, etc. but I wasn't able to find a way that worked. It's a safe assumption that if two users have usercodes that are "like" the department that the usercode that is longest is the one we want.
(This is my first question on here and a struggled with how to best explain this issue. Please be gentle :) )
First, I have to say that the main problem here is when a developer thought that they would be clever and build a lot of logic into the department and user IDs. Hiding this sort of information within a column is a big source of headaches in general (as you're just starting to see).
I don't develop with Access, so I'm not certain of the syntax, but hopefully you'll get the general idea. Please let me know if the syntax needs to be tweaked for future users who find this question:
SELECT
D.Dept,
D.DDescr,
U.UserIDX
FROM
TDepts D
LEFT OUTER JOIN
(
SELECT
SQ_D.Dept,
MAX(LEN(SQ_U.usercode)) AS max_len_usercode
FROM
TDepts SQ_D
INNER JOIN tUserSec SQ_U ON SQ_D.Dept LIKE SQ_U.usercode & "*"
GROUP BY
SQ_D.Dept
) SQ ON SQ_D.Dept = D.Dept
LEFT OUTER JOIN tUserSec U ON
D.Dept LIKE U.usercode & "*" AND
LEN(U.usercode) = SQ.max_len_usercode
The query gets a list of all of the departments along with the length of the longest usercode that matches for that department. Then it uses that to determine which user matches for the "most like" the department.

Access Query Help - Get records within a timeframe of another record

I'm looking for a way of querying a table to get events of a certain type, and all events that happen within the time-frame of the criteria event for the same person. That probably sounded like nonsense. Consider the following;
Imagine I want to get all "SHIFT"s for each person (A person could have multiple shifts per day) and it's associated breaks (But there could be other things as well) a way to query within a date range would be good as well. Eventually I'm going to be working with years worth of data, not all of which is necessary to everybody.
This example would return the first three rows, plus the last two. Row 5 is a BREAK, but it doesn't occur within a SHIFT for person 1.
I would love to provide some code but I honestly can't even think where to start with this one. I guess I'd need a sub query? Any help would be greatly appreciated!
I'm mostly using access 2003 so responses geared towards that would be ideal.
The way you've described the problem, it appears you want the shifts and related breaks as separate rows. To do this you can use union all to combine the two different types. A correlated sub query lets you find breaks that occur during shifts.
Select
*
From
Events
Where
Event_Name = 'SHIFT'
Union All
Select
*
From
Events e1
Where
Event_Name = 'BREAK' And
Exists (
Select
'x'
From
Events e2 -- find corresponding shift for break
Where
e1.Event_Owner = e2.Event_Owner And
e2.Event_Name = 'SHIFT' And
e1.Event_Start >= e2.Event_Start And
e1.Event_End <= e2.Event_End
)

SQL query to show only completed GROUPED BY results

I have a query which looks at results from a set of data in SQL Server 2008 R2. Essentially, a sample of people have been taking surveys a few times a day for a few months. In Visual Studio, I now want to write a report to show only those people who have never missed a survey for the entire period of time.
Let's say that the column structure is as follows:
ID | UserRef | ResultRef | Date | Time
Where:
ID is a unique identifier for the row
UserRef is a unique identifier for each participant
ResultRef indicated how the survey was completed (e.g. "A" would indicate that the survey was completed on time; "C" means that the participant missed the survey completely; "O" means that the survey was completed, but late; etc, etc, etc)
Date and Time are self explanatory
What I want to find is all of the people that never missed a survey (i.e. only ever recorded a "A" in their ResultRef).
It is also worth bearing in mind that not all participants were invited to respond to all of the surveys: some will have been, but some will only have been invited to some of them.
I have been considering a GROUP BY query, but can't figure out the WHERE clause for this.
Any suggestions?
Try this
SELECT * FROM yourTable WHERE ResultRef = 'A' and UserRef NOT IN
(SELECT DISTINCT UserRef FROM yourTable
WHERE ResultRef != 'A')

SQL Inner Join Descrepancies: Greedy Match Last Instance

I'm stumped. I have a table that contains information on trips over the last few years for people in my organization. Our goal is to determine the time between any given trip that the person was home "DwellAC". The trips may not have been entered in order. I know I need to narrow down the trips and select the most recent trip before the current lines trip and then subtract the dates. Im trying to use a self join because all the data is in only one table. This is my attempt at the SQL to calculate the Dwell for each trip.
UPDATE Deployments AS d1
INNER JOIN Deployments AS d2
ON d1.AC=d2.AC or d1.AC=d2.FP
SET d1.DwellAC = d1.DepartMcGuire-d2.ReturnMcGuire
WHERE d2.ReturnMcGuire<d1.DepartMcGuire And d1.AC<>'' And d1.ID<>d2.ID;
I thought the code was working just fine. It seemed to be calculating everything correctly. I started to run into trouble when new trips were added in the past. Meaning that we found new data to input an old trip. The index on the new-old trips is higher and seems to be throwing thing off.
I realized this morning that I obviously need to include more data. The table looks like this.
AC DwellAC DepartMcGuire ReturnMcGuire
Evan 1/1/2011 2/1/2011
Evan 3/10/2011 4/10/2011
Evan 1/1/2010 6/1/2010
The goal is to properly fill the DwellAC column with the time home between trips.
Is there any way I could use a nested where query to find the max date < the next depart date to use in the update query?
I know the pseudo code would look like this
For each AC
Select all trips where oldtrip.returnMcGuire < thistrip.departMcGuire and AC=AC
From these trips select MAX oldtrip.ReturnMcGuire
Update this.DwellAC = maxfound oldtrip.returnMcguire - thistrip.departMcGuire
Thanks for any help, I am not sure if I even need the inner join anymore
Ive found this query
SELECT CurrentTrip.AC, CurrentTrip.DwellAC, CurrentTrip.FP, CurrentTrip.DepartMcGuire,
(SELECT MAX(LastTrip.ReturnMcGuire)
FROM Deployments AS LastTrip
WHERE ((CurrentTrip.AC=LastTrip.AC OR CurrentTrip.AC=LastTrip.FP) AND (LastTrip.ReturnMcGuire<CurrentTrip.DepartMcGuire))) AS LastReturnMcGuire
FROM Deployments AS CurrentTrip
Gives me the data I need in the forms of CurrentTrip.DepartMcGuire-LastReturnMcGuire
I just need to turn it into an update statement.
Perhaps something like this? which is not workin
UPDATE Deployments AS CurrentTrip
SET CurrentTrip.DwellAC = CurrentTrip.DepartMcGuire-(SELECT MAX(LastTrip.ReturnMcGuire) FROM Deployments AS LastTrip WHERE CurrentTrip.AC=LastTrip.AC);