【发布时间】:2015-07-08 10:04:01
【问题描述】:
我有一个由 2 列组成的表格:
Tested_Object | Result
A | OK
A | NOT OK
A | NOT OK
B | OK
我需要有如下输出:
Tested_Object | SUM | OK | NOT_OK
A | 3 |1 | 2
B | 1 |1 | 0 (or empty)
我尝试使用:
SELECT
t1.Tested_Object,
Count(t1.Result) AS SUM,
Count(t2.Result) AS OK,
Count(t3.Result) AS NOT_OK
FROM
(t1 LEFT JOIN (t1 AS t2) ON t1.Tested_Object=t2.Tested_Object)
LEFT JOIN (t1 AS t3) WHERE t1.Tested_Object=t3.Tested_Object
GROUP BY t1.Tested_Object
现在如果我说:
WHERE (t2.Result="OK" AND t3.Result="NOT_OK")
或
WHERE (t2.Result="OK" OR t3.Result="NOT_OK")
或
t1 LEFT JOIN (t1 AS t2 WHERE t2.Result="OK") ON t1.Tested_Object=t2.Tested_Object
我为每列或错误得到相同的计数。
我设法获得了不同的列号,仅将所有 OK 保存在一个表中,而将 NOT OKs 手动保存在另一个表中。但我需要一个从输入到输出表完全自动进行计算的查询。
如果不清楚,我是 SQL 新手 :) 提前致谢
【问题讨论】: