2011-06-06 94 views
0

我想計算按項目重新打開的票的平均數量,但我無法通過單個SQL請求來完成此操作。 我已經取回車票的總數:如何使用兩個SQL請求(mysql)計算平均數

select count(jiraissue.id) as totalTicketByProj 
from jiraissue ,project 
where jiraissue.project=project.id 
group by project.pname; 

而且我已經retrived號重新開放門票:

select count(changeitem.id) as nbissueReopenByProject 
from changeitem,changegroup,jiraissue,project 
where changeitem.groupid=changegroup.id 
and changegroup.issueid=jiraissue.id 
and jiraissue.project= project.id 
and changeitem.oldstring="Resolved" 
and changeitem.newstring="Closed" 
group by project.pname; 

但我不知道如何計算重新門票的平均數量(nbissueReopenByProject/totalTicketByProj)使用單個請求!

任何想法!

回答

1
SELECT (nbissueReopenByProject/totalTicketByProj) 
    FROM (SELECT project.pname, COUNT(jiraissue.id) AS totalTicketByProj 
      FROM jiraissue, project 
     WHERE jiraissue.project = project.id 
     GROUP BY project.pname) ttbp, 
     (SELECT project.pname, COUNT(changeitem.id) AS nbissueReopenByProject 
      FROM changeitem, changegroup, jiraissue, project 
     WHERE changeitem.groupid = changegroup.id 
      AND changegroup.issueid = jiraissue.id 
      AND jiraissue.project = project.id 
      AND changeitem.oldstring = "Resolved" 
      AND changeitem.newstring = "Closed" 
     GROUP BY project.pname) nbirbp 
WHERE ttbp.pname = nbirbp.pname 
+0

感謝你的回覆也是非常有用 – Rym 2011-06-06 10:02:00

0

我不得不改革你的查詢使用顯式連接,使得一個LEFT OUTER JOIN更容易形成。

一旦你有了LEFT OUTER JOINs,你可以在條件語句中包含一個SUM ......(我的代碼可能在某些地方假設1:1關係,因爲你的問題沒有模式細節,但是我相信這有助於ATLEAST朝着你的目標前進。)

SELECT 
    COUNT(DISTINCT jiraissue.id) 
/
    SUM(CASE WHEN changeitem.oldstring="Resolved" and changeitem.newstring="Closed" THEN 1 ELSE 0 END) 
FROM 
    (
    jiraissue 
    INNER JOIN 
    project 
     ON jiraissue.project= project.id 
) 
LEFT JOIN 
    (
    changeitem 
    INNER JOIN 
    changegroup 
     ON changeitem.groupid=changegroup.id 
) 
    ON changegroup.issueid=jiraissue.id 
GROUP BY 
    project.pname 
; 
+0

非常感謝你這是如此的幫助:) – Rym 2011-06-06 09:45:49