I have a how do I do this in Oracle?
select a.field1 || '_' ||
b.field2 || '_' ||
sum(c.field3)
from table1 a,
table2 b,
table3 c
where a.field1 || '_' ||
b.field2 || '_' ||
sum(c.field3) not in (select d.field1 || '_' ||
e.field2 || '_' ||
sum(f.field3)
from table4 d,
table5 e,
table6 f
where conditional_info_to_join_the_tables
group by d.field1, e.field2)
and conditional_info_to_join_the_tables
group by a.field1, b.field2
The error I get is I can’t use the sum in the where clause
i have tried using
select a.field1 || '_' ||
b.field2 || '_' ||
sum(c.field3),
sum(c.field2) foo
from table1 a,
table2 b,
table3 c
where a.field1 || '_' ||
b.field2 || '_' ||
foo not in (select d.field1 || '_' ||
e.field2 || '_' ||
sum(f.field3)
from table4 d,
table5 e,
table6 f
where conditional_info_to_join_the_tables
group by d.field1, e.field2)
and conditional_info_to_join_the_tables
group by a.field1, b.field2
but that foo was not an identified variable.
Merely giving the
sumand alias (foo) does not help because the aggregate occurs after the filtering (thewhereclause.The
havingclause is like a filter that applies after the aggregation:You will still need to write out the aggregate in full rather than aliasing—the reason behind this is a bit less obvious and is discussed in this answer over on dba.se: https://dba.stackexchange.com/a/21982/1396.
Of course you can achieve the same with a subquery: