通过连接多个表来累计子总计和总计

时间:2016-07-22 18:10:46

标签: mysql

扩展Rolling up addition using mysql

我可以得到行https://stackoverflow.com/users/1529673/strawberry的基本内容的滚动总和,所以我试图通过连接多个表来扩展查询但是1_keyworddefs.name ='K2'不受影响。获得1_keyworddefs.name ='K1'和K2的相同答案。

工作查询,但直接指定1_bugs.bug_id ='2':

SELECT x.*, x.cf1 + x.cf2 sub_total, sum(y.cf1 + y.cf2) total FROM 1_bugs x INNER JOIN 1_bugs y ON y.bug_id <= x.bug_id INNER JOIN 1_keywords ON 1_keywords.bug_id = y.bug_id WHERE (x.bug_date BETWEEN '2016-07-19' AND '2016-07-22') AND (x.bug_id='2') AND (y.bug_status = 'VERIFIED' OR y.bug_status = 'CLOSED') AND (1_keywords.bug_id = x.bug_id) GROUP BY x.bug_id 

确切输出(但我想连接1_keywords.bug_id = 1_bugs.bug_id匹配的表,而不是直接指定1_bugs.bug_id ='2'):

bug_id  bug_date    cf1     cf2     bug_status  sub_total   total   
2       2016-07-19  2       1       VERIFIED    3           3

通过连接表进行非工作查询(期待上面的答案):

SELECT x.*, x.cf1 + x.cf2 sub_total, sum(y.cf1 + y.cf2) total FROM 1_bugs x INNER JOIN 1_bugs y ON y.bug_id <= x.bug_id LEFT JOIN 1_keywords ON 1_keywords.bug_id = y.bug_id LEFT JOIN 1_keyworddefs ON 1_keyworddefs.id=1_keywords.keywordid AND 1_keyworddefs.name='K2' and 1_keywords.bug_id = y.bug_id WHERE (x.bug_date BETWEEN '2016-07-19' AND '2016-07-22') AND (y.bug_status = 'CLOSED' OR y.bug_status = 'VERIFIED') GROUP BY x.bug_id; 

预期:

bug_id  bug_date    cf1     cf2     bug_status  sub_total   total   
2       2016-07-19  2       1       VERIFIED    3           3

实际:

 bug_id     bug_date    cf1     cf2     bug_status  sub_total   total   
 2          2016-07-19  2       1       VERIFIED    3           3
 3          2016-07-22  2       2       CLOSED      4           7

**这里有bug_id - &gt; 3行错误,因为1_bugs.bug.id = 1_keywords.bug_id不匹配且1_keywords表中没有1_keywords.bug_id ='3'。

的DDL:

-- 1_bugs table1 (master table) :

CREATE TABLE `1_bugs` (`bug_id` int(11) NOT NULL, `bug_date` date NOT NULL, `cf1` int(11) NOT NULL, `cf2` int(11) NOT NULL, `bug_status` varchar(200) NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=latin1;

INSERT INTO `1_bugs` (`bug_id`, `bug_date`, `cf1`, `cf2`, `bug_status`) VALUES (1, '2016-07-19', 3, 2, 'RESOLVED'), (2, '2016-07-19', 2, 1, VERIFIED'), (3, '2016-07-22', 2, 2, 'CLOSED');

-- 1_keywords table2 (having keyword ids):

CREATE TABLE `1_keywords` (`bug_id` int(11) NOT NULL, `keywordid` varchar(11) NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=latin1;

INSERT INTO `1_keywords` (`bug_id`, `keywordid`) VALUES (1, 'K1'), (2, 'K2');


-- 1_keyworddefs table3 (having keyword names according to keywordid):

CREATE TABLE `1_keyworddefs` (`id` int(11) NOT NULL, `name` varchar(200) NOT NULL, `description` varchar(200) NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=latin1;

INSERT INTO `1_keyworddefs` (`id`, `name`, `description`) VALUES (1, 'K1', 'My K1 item'), (2, 'K2', 'My K2 item');

有人可以指出我的错误吗?

1 个答案:

答案 0 :(得分:1)

显然,事情不明确......经过长时间的讨论后发现你想要的是从bugzilla数据库中提取特定关键字的错误列表,按日期和cf1 + cf2的总和以及仅相同的总和&# 39;开&#39;并且&#39;已验证&#39;错误。

我们走了

SELECT
  b.lastdiffed,
  d.name,
  d.description,
  SUM(IF(b.bug_status IN('CLOSED', 'VERIFIED'), b.cf1 + b.cf2, 0)) AS sub_total,
  SUM(b.cf1 + b.cf2) AS running
FROM
  bugs AS b
  JOIN keywords AS k
    ON ( k.bug_id = b.bug_id )
  JOIN keyworddefs AS d
    ON ( d.id = k.keywordid )
WHERE 
  1
  AND d.name = 'SONY'
GROUP BY (b.lastdiffed)
ORDER BY b.lastdiffed ASC

给出这个结果

+------------+------+-------------+-----------+---------+
| lastdiffed | name | description | sub_total | running |
+------------+------+-------------+-----------+---------+
| 2016-05-20 | SONY | sony items  |         7 |       7 |
| 2016-06-20 | SONY | sony items  |        11 |      17 |
| 2016-06-27 | SONY | sony items  |         5 |       5 |
| 2016-06-29 | SONY | sony items  |         5 |       5 |
+------------+------+-------------+-----------+---------+

希望这有帮助。