简体   繁体   中英

SQL Query - Get results from multiple rows with same ID

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: 在此处输入图片说明

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!

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!!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM