2015-06-05 27 views
1

我们有一组来自SQL查询的数据,我们希望使用该数据创建折线图。用定义的逻辑创建折线图

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 
| 
| 

第二列是不是一个计算值(形成数据库) 第三个还 第四列的第一行是一个计算值 的逻辑是:

617,5 - 52 = 565,5 
565,5 - 35 = 530,5 and so on 

如何我们要用SQL来实现这个逻辑吗?

回答

1

这里是sql来计算累加减法。

SELECT @prev_sets := t.sprint AS sprint,@prev_set := t.VALUE AS VALUE, 
      ROUND(IF(0<@i,@i:[email protected] , @i:[email protected]),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  | 
    ---------------------------------------------------- 

我认为它会帮助你。

谢谢。