提问人:Siva Ram 提问时间:10/6/2019 最后编辑:William RobertsonSiva Ram 更新时间:10/6/2019 访问量:71
子查询中的分组依据未按预期工作
Group by in subquery not working as expect
问:
我有一个包含多个类型和输出条件的表,这些条件决定了通过还是失败,现在我想计算总行数和通过行数。
create table test
(
type varchar2(4),
val number(5),
output number(1)
);
insert into test values('AB',2,0);
insert into test values('AB',5,0);
insert into test values('AB',6,1);
insert into test values('AB',2,1);
insert into test values('BC',1,0);
insert into test values('BC',4,1);
insert into test values('BC',6,0);
select t.type,ti.total as totalcount,count(*) as passcount
,'PASS' as status
from test t,
(select type,count(*) as total
from test
group by type) ti
where t.output=0 and t.type=ti.type
group by t.type;
抛出以下错误。 ORA-00979:不是 GROUP BY 表达式。
我期待按组划分的总计数,并在 Fiddle 链接 http://sqlfiddle.com/#!4/9ef3f7/23 输出中传递计数
答:
0赞
Serg
10/6/2019
#1
查找至少具有一个 (“good”) 的类型,计算总数和通过1
output
select
type, count(*) as total, sum(output) as passedCount
, 'PASS' as status
from test t
having max(output)=1
group by t.type;
0赞
Gordon Linoff
10/6/2019
#2
我想计算总行数和通过行数。
我认为您想要一个简单的聚合查询:
select t.type, count(*) as total,
sum(t.output) as num_passed -- presumably "1" is pass
from test t
group by t.type;
这假设“1”表示“通过”。如果“0”表示通过,则可以使用:
sum(1 - t.output) as num_passed
如果逻辑更复杂,则使用:case
sum(case when t.output = 1 then 1 else 0 end) as num_passed
评论
select t.type,ti.total
与 Add to 冲突或将其聚合到 中。group by t.type;
ti.total
group by
select
ti.total