我使用此查询时收到错误
select dbresultsid, TestCase, BuildID, Analyzed,
Verdict,
(select count(Verdict) from results where BuildID = 'Beta1'
and Verdict = 'PASS') AS PASS,
(select count(Verdict) from results where BuildID = 'Beta1'
and Verdict = 'FAIL') AS FAIL,
(select count(Verdict) from results where BuildID = 'Beta1'
and Verdict = 'INCONC') AS INCONC,
(select count(Verdict) from results where BuildID = 'Beta1'
and Verdict = 'TIMEOUT') AS TIMEOUT
from results
where BuildID = 'Beta1'
group by TestCase,dbresultsid
order by Analyzed
它说
列'results.BuildID'无效 选择列表,因为它不是 包含在一个聚合中 函数或GROUP BY子句。'
相同的查询在MYSQL中运行良好。有人可以帮忙吗?
谢谢,
答案 0 :(得分:1)
如果在查询中使用Group By,则必须将所有非聚合函数的列添加到Group By子句中。因此,将BuildID添加到Group By子句和所有其他没有聚合的列(如判决等),它将起作用。
另请参阅GROUP BY documentation了解更多详情。
答案 1 :(得分:1)
将您的代码更改为:
select dbresultsid, TestCase, BuildID, Analyzed,
Verdict,
(select count(Verdict) from results where BuildID = 'Beta1'
and Verdict = 'PASS') AS PASS,
(select count(Verdict) from results where BuildID = 'Beta1'
and Verdict = 'FAIL') AS FAIL,
(select count(Verdict) from results where BuildID = 'Beta1'
and Verdict = 'INCONC') AS INCONC,
(select count(Verdict) from results where BuildID = 'Beta1'
and Verdict = 'TIMEOUT') AS TIMEOUT
from results
where BuildID = 'Beta1'
group by TestCase,dbresultsid, BuildID, Analyzed
order by Analyzed
虽然您将BuildID过滤为仅一个值,但SQL Server要求在逐个列表中指定它。
答案 2 :(得分:0)
SQL Server中的Group By子句允许Group By Clause中的列,如果我们需要其他列,那么我们需要通过MAX,MIN等聚合函数访问它们。
答案 3 :(得分:0)
您必须聚合BuildID和Analyzed列,或者将它们添加到组中。 还要注意,你现在这样做的方式非常低效。
看看here如何通过只进行一次表扫描来提高性能 几百个你将会拥有
答案 4 :(得分:0)
分组 - 按列出的字段对结果进行分组,并在其余字段上执行一些聚合函数(sum,avg ...)。没有count()或avg()或sum()的所有东西都必须分组。
但是,看起来您将两个不同的查询混合在一起。如果您对给定的BuildId的每个判定都需要多个记录,则查询只是:
select BuildID, Verdict, count(*)
group by BuildID, Verdict
如果那不是你想要的,你应该描述你想要实现的目标