如何在MySQL

时间:2017-10-04 08:09:01

标签: mysql

我正在为Bug Metrics做一个报告。与此相关我需要一些自定义表,但我的查询没有给出预期的输出。

我的查询提供输出Current Output。所以,我希望 Oustanding 列值必须按项目分组,但它也在计算其他项目值。

**Current Output**
*********************************************
|Project   | Opened | Resolved| Outstanding |
*********************************************
|  A       |    1   |    0    |    1        |
*********************************************
|  A       |    2   |    1    |    2        |
*********************************************
|  A       |    1   |    2    |    1        |
*********************************************
|  B       |    3   |    2    |    2        |
*********************************************
|  B       |    2   |    1    |    3        |
*********************************************

我想要像Expected Output这样的输出, Outstanding 值应该为每个项目单独计算。比如准确地说,当谈到项目'B'时,它应该显示属于它的唯一未完成的值,而不是添加以前的项目未完成的值。

**Expected Output**
*********************************************
|Project   | Opened | Resolved| Outstanding |
*********************************************
|  A       |    1   |    0    |    1        |
*********************************************
|  A       |    2   |    1    |    2        |
*********************************************
|  A       |    1   |    2    |    1        |
*********************************************
|  B       |    3   |    2    |    1        |
*********************************************
|  B       |    2   |    1    |    2        |
*********************************************

1)我希望分组列 - 杰出

2)增量变量 - @runtot

3)用于未完成的计算 - (@ runtot:=(jtb.Opened + @runtot) - jtb.Resolved) AS Outstanding

我的查询:

SELECT jtb.Project,jtb.Opened,jtb.Resolved,
(@runtot :=  (jtb.Opened + @runtot) - jtb.Resolved) AS Outstanding FROM
(SELECT ji.project AS Project_Id, 
pr.pname AS Project_Name,
COUNT(DISTINCT(ji.ID)) AS Opened,
COUNT(IF(ji.issuestatus  = 5 OR ji.issuestatus = 6,ji.issuestatus,NULL)) AS Resolved  
FROM  jiraissue ji 
LEFT OUTER JOIN priority p ON (p.ID = ji.priority) 
LEFT OUTER JOIN project pr ON (ji.project = pr.ID)  
GROUP BY ji.project) jtb,
(SELECT @runtot:=0) c

请帮助我:))

1 个答案:

答案 0 :(得分:0)

最后,这个question给出了解决方案。我在查询中添加了一个全局变量来更新项目名称。因此,当新项目到达 @prev_project 时,它将刷新@ runtot = 0。

查询对我有用: -

SELECT jtb.Project,jtb.Opened,jtb.Resolved,
(@runtot :=  IF(jtb.Project = @prev_project, ((@runtot+jtb.Opened ) - 
jtb.Resolved),(jtb.Opened - jtb.Resolved))) AS Outstanding,
@prev_project := jtb.Project AS sample_project_name FROM
(SELECT ji.project AS Project_Id, 
pr.pname AS Project_Name,
COUNT(DISTINCT(ji.ID)) AS Opened,
COUNT(IF(ji.issuestatus  = 5 OR ji.issuestatus = 6,ji.issuestatus,NULL)) AS Resolved  
FROM  jiraissue ji  
LEFT OUTER JOIN project pr ON (ji.project = pr.ID)  
GROUP BY ji.project) jtb,
(SELECT @runtot:=0, @prev_project := NULL) c