1

I read a few things already here and also used Google to find an answer to my question but I did not find something that helps me. I hope someone can give me an answer to my question.

I do have a database with a table called "tickets". Now if a user creates a new ticket, an entry to the database will be made. If an admin now gives an answer a new row in the table will be created, with the same "ticketnumber". This looks like this screenshot here: enter image description here

First row is a newly created ticket and second row is an answer from an admin! Now I need a query to get the following information: Response Time from Admin! I need to know how long the difference is between stamp_closed and stamp_opened but the response time must be from the admin, that is my problem because in admin´s row the stamp_opened is not logged.

How can I create SELECT-query to get: pers_id from Admin, stamp_opened from Admin and stamp_closed from User?

This is what I tried so far:

SELECT ticket.pers_id, ticket.stamp_created as starttime, ticket.stamp_closed as endtime, count(ticket.pers_id) as cnt, user.firstname, user.lastname, (sum(ticket.stamp_closed)-sum(ticket.stamp_created)) as average 
FROM tickets ticket
inner join userlist user on ticket.pers_id=user.pers_id
where ticket.ticketnumber=54094

Problem is that I get the user´s pers_id and user´s stamp_closed and not admin´s pers_id and admin´s stamp_closed!

Hopefully someone can help me out!

Thanks in advance!

3
  • 2
    Did you know you could join a table to itself... e.g. FROM tickets t inner join tickets tAnswer on t.ticketnubmer = tanswer.ticketnumber and t.stamp_reply =0 and tansewr.stamp_reply <> 0 Commented Feb 29, 2016 at 15:54
  • Thanks for your answer. Yes I know that but still get no query which gives me exactly that information that I need and described above in my question :( Commented Feb 29, 2016 at 16:12
  • Do you need to subtract stamp_opened (row1) from stamp_opened (row2) or stamp_closed (row1) from stamp_created (row1)? Commented Feb 29, 2016 at 16:41

1 Answer 1

2

I'm not sure I completely understand the question here, but to answer this specific question :

How can I create SELECT-query to get: pers_id from Admin, stamp_opened from Admin and stamp_closed from User?

you would need to use self-join as rightly mentioned by Conrad in his comment above. Here's a way you could do it:

SELECT          tAdmin.pers_id,
                tAdmin.stamp_opened AS starttime,
                tUser.stamp_closed AS endtime,

FROM            tickets tUser
INNER JOIN      tickets tAdmin ON tUser.ticketnumber = tAdmin.ticketNumber
WHERE           tAdmin.stamp_created = 0
AND             tUser.stamp_created <> 0
AND             tUser.ticketnumber = 54094

The aforementioned query will only fetch you results for this particular ticket number(54094). If you want your SQL query to fetch results for all ticket numbers, just remove the last condition from the WHERE clause:

SELECT          tAdmin.pers_id,
                tAdmin.stamp_opened AS starttime,
                tUser.stamp_closed AS endtime,

FROM            tickets tUser
INNER JOIN      tickets tAdmin ON tUser.ticketnumber = tAdmin.ticketNumber
WHERE           tAdmin.stamp_created = 0
AND             tUser.stamp_created <> 0

Lastly, I see you are trying to find the response time for the tickets. That is, the difference between the time when the ticket was created and when it was closed. You can do that as follows:

SELECT          tAdmin.pers_id,
                tAdmin.stamp_opened AS starttime,
                tUser.stamp_closed AS endtime,
                (tUser.stamp_closed - tUser.stamp_created) AS 'Current request Service Response Time'
FROM            tickets tUser
INNER JOIN      tickets tAdmin ON tUser.ticketnumber = tAdmin.ticketNumber
WHERE           tAdmin.stamp_created = 0
AND             tUser.stamp_created <> 0
AND             tUser.ticketnumber = 54094

Again, note that this for only the current record. If you want an average of all the records along with the current data, you could do this:

 SELECT         tAdmin.pers_id,
                tAdmin.stamp_opened as starttime,
                tUser.stamp_closed as endtime,
                (tUser.stamp_closed - tUser.stamp_created) AS 'Current request Service Response Time',
                (SELECT         (SUM(stamp_closed) - SUM(stamp_created))
                 FROM           Tickets) AS 'Average Service Response Time'
FROM            tickets tUser
INNER JOIN      tickets tAdmin ON tUser.ticketnumber = tAdmin.ticketNumber
WHERE           tAdmin.stamp_created = 0
AND             tUser.stamp_created <> 0
AND             tUser.ticketnumber = 54094

You can substitute the WHERE clause with whatever business rules you have (in your SQL) to uniquely distinguish between the user and the admin.

Hope this helps!!!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.