创建具有已定义逻辑的折线图

时间:2015-06-05 11:25:42

标签: mysql

我们有一组来自SQL查询的数据,我们想要使用该数据创建折线图。

  <serverVariables>
    <set name="HTTP_PROTOCOL" value="something" />
  </serverVariables>

第二列不是计算值(来自数据库) 第三个也是 第四列的第一行是计算值 逻辑是:

SELECT REPLACE(req2.value,"Sprint 1", "sum") AS sprint  , SUM(val)  AS VALUE  FROM 
(SELECT issue AS iss,SUM(VALUE) AS val  FROM dataissue,issue WHERE FIELD LIKE 'poin%' AND dataissue.issue = issue.id AND VALUE IS NOT NULL AND issue IN ( SELECT issue FROM dataissue,issue WHERE FIELD = 'sprint' AND dataissue.issue = issue.id AND VALUE IS NOT NULL ) GROUP BY VALUE,issue) req1 JOIN (SELECT issue,VALUE FROM dataissue,issue WHERE FIELD LIKE 'sprint%' AND dataissue.issue = issue.id AND VALUE IS NOT NULL ) req2
WHERE iss = req2.issue 

UNION ALL 

SELECT main2.value AS sprint  , SUM(val) AS VALUE  FROM 
( SELECT issue AS iss,SUM(VALUE) AS val  FROM dataissue,issue 
  WHERE FIELD LIKE 'poin%' AND dataissue.issue = issue.id AND 
  VALUE IS NOT NULL AND issue IN ( SELECT issue FROM dataissue,issue 
                   WHERE FIELD = 'sprint' AND dataissue.issue = issue.id AND VALUE IS NOT NULL ) GROUP BY VALUE,issue) 
                   main1 JOIN (SELECT issue,VALUE FROM dataissue,issue 
                                WHERE FIELD LIKE 'sprint%' AND dataissue.issue = issue.id AND VALUE IS NOT NULL ) main2
WHERE iss = main2.issue GROUP BY main2.value

ORDER BY  CAST(SUBSTRING(sprint,LOCATE(' ',sprint)+1) AS SIGNED)

-----------------------------------
|                        617,5   
   sprint1   |    52   |    565,5    |     
   Sprint 2      35         530,5
| 
|

如何使用SQL实现此逻辑?

1 个答案:

答案 0 :(得分:1)

这是用于计算累加减法的sql。

 SELECT  @prev_sets := t.sprint AS sprint,@prev_set := t.VALUE AS VALUE,
           ROUND(IF(0<@i,@i:=@i-t.VALUE , @i:=t.VALUE+@i),2) AS `VALUE`
FROM (


SELECT req2.value AS sprint  , SUM(val)  AS VALUE  FROM 
(SELECT issue AS iss,SUM(VALUE) AS val  FROM dataissue,issue WHERE FIELD LIKE 'poin%' AND dataissue.issue = issue.id AND VALUE IS NOT NULL AND issue IN ( SELECT issue FROM dataissue,issue WHERE FIELD = 'sprint' AND dataissue.issue = issue.id AND VALUE IS NOT NULL ) GROUP BY VALUE,issue) req1 JOIN (SELECT issue,VALUE FROM dataissue,issue WHERE FIELD LIKE 'sprint%' AND dataissue.issue = issue.id AND VALUE IS NOT NULL ) req2
WHERE iss = req2.issue 

UNION ALL 


SELECT main2.value AS sprint  , SUM(val) AS VALUE  FROM 
( SELECT issue AS iss,SUM(VALUE) AS val  FROM dataissue,issue 
  WHERE FIELD LIKE 'poin%' AND dataissue.issue = issue.id AND 
  VALUE IS NOT NULL AND issue IN ( SELECT issue FROM dataissue,issue 
                   WHERE FIELD = 'sprint' AND dataissue.issue = issue.id AND VALUE IS NOT NULL ) GROUP BY VALUE,issue) 
                   main1 JOIN (SELECT issue,VALUE FROM dataissue,issue 
                                WHERE FIELD LIKE 'sprint%' AND dataissue.issue = issue.id AND VALUE IS NOT NULL ) main2
WHERE iss = main2.issue GROUP BY main2.value

ORDER BY  CAST(SUBSTRING(sprint,LOCATE(' ',sprint)+1) AS SIGNED)
)
t
                  JOIN (SELECT @prev_sets := NULL,@prev_set := NULL, @i := 0  ) i  

    ----------------------------------------------------
    sprint         |  value    |  cumulative_subtract  |
    ----------------------------------------------------
                   |           |        67.5           |
    ----------------------------------------------------
    sprint1        |   52      |        518.75         |
    ----------------------------------------------------
    sprint2        |   35      |         483.75        |
    ----------------------------------------------------

我认为它会帮助你。

谢谢。