output name instead of id


output name instead of id



I'm trying to see how long it takes to resolve a bug based on the area that it affects. My result set looks like this


areaid Average Resolution/Closure Time (days)
1 425.57929613
2 510.00435347
3 342.90670739



I want to replace the area values with the name of the area rather than the id number. The name of the area is in another table (area table). the table i'm performing the code on is called bug table. So this is what I want.


area_name Average Resolution/Closure Time (days)
ABC 425.57929613
DEF 510.00435347
GHI 342.90670739



The area table looks like this


areaid area_name
1 ABC
2 DEF
3 GHI



i think i need to use a join (all bugs is tied to one area, but area categories may not be tied to a bug) so that my code is able to call the area name based on the primary key area id. My question is basically how do i use a join (or any other function) to replace the area key id with the name of the area. I've read about joins but I dont understand how you choose which table to call from. As well, i have other code and i'm worried about how this new addition would affect the existing code.


SELECT area, AVG(TIMESTAMPDIFF (second, Opened, GREATEST(COALESCE(Resolved , Closed), COALESCE(Closed, Resolved)))) /86400 AS "Average Resolution/Closure Time (days)"
FROM bug
WHERE Resolved IS NOT NULL AND Closed IS NOT NULL
AND Title <> "test"
GROUP BY area





does your second table has area id? on what basis one would say area 1 belongs to say "ABC" or "DEF" or others?
– SMA
Jun 29 at 17:30





@SMA the second table does have an area id, i amended the question to include a sample of the area table.
– hello world
Jun 29 at 17:35





Based on the WHERE clause, the COALESCE's seem rather pointless?
– LukStorms
Jun 29 at 17:38





Hint: Use JOIN
– Eric
Jun 29 at 17:42


JOIN





@LukStorms i think i made a mistake in my code. I wanted to include bugs that have either resolved and/or closed dates but not bugs that dont have either but i think the way i wrote it will only take bugs that have both.
– hello world
Jun 29 at 17:45





1 Answer
1



You will join the bug table to the [ReferenceTable] on the id of the areaName, then select the alias rt.areaName from the join:


SELECT
ar.area_name as Area,
AVG(TIMESTAMPDIFF (second, Opened, GREATEST(COALESCE(Resolved , Closed), COALESCE(Closed, Resolved)))) /86400 AS "Average Resolution/Closure Time (days)"
FROM bug as bg
JOIN Area as ar
on bg.areaid = ar.areaid
WHERE Resolved IS NOT NULL AND Closed IS NOT NULL
AND Title <> "test"
GROUP BY ar.areaName





MySQL uses backticks, not square brackets, around names. Square brackets are for SQL-Server.
– Barmar
Jun 29 at 18:35





Thanks @ian-fogelman! your solution worked! thanks again
– hello world
Jun 29 at 19:02







By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Opening a url is failing in Swift

Export result set on Dbeaver to CSV