简体   繁体   English

SQL查询-从具有相同ID的多行获取结果

[英]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. 我已经在这里阅读了一些内容,还使用Google来找到问题的答案,但没有找到对我有帮助的东西。 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. 我需要知道stamp_closed和stamp_opened之间的区别有多长时间,但是响应时间必须来自管理员,这是我的问题,因为在admin行中,stamp_opened未被记录。

How can I create SELECT-query to get: pers_id from Admin, stamp_opened from Admin and stamp_closed from User? 如何创建SELECT查询以获取:从admin获得pers_id,从admin获得stamp_opened,从用户获得stamp_closed?

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! 问题是我得到了用户的pers_id和用户的stamp_closed,而不是管理员的pers_id和管理员的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? 如何创建SELECT查询以获取:从admin获得pers_id,从admin获得stamp_opened,从用户获得stamp_closed?

you would need to use self-join as rightly mentioned by Conrad in his comment above. 您需要使用康拉德(Conrad)在上述评论中正确提及的自我加入。 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). 前述查询只会获取您针对该特定凭单编号的结果(54094)。 If you want your SQL query to fetch results for all ticket numbers, just remove the last condition from the WHERE clause: 如果希望您的SQL查询获取所有票证编号的结果,只需从WHERE子句中删除最后一个条件:

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. 您可以将WHERE子句替换为您拥有的任何业务规则(在SQL中),以唯一地区分用户和管理员。

Hope this helps!!! 希望这可以帮助!!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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