Предположим, у вас есть следующая таблица ORDERS:
create table orders ( id, order_price )
as
select level, level * 100
from dual
connect by level <= 12 ;
ID ORDER_PRICE
1 100
2 200
3 300
4 400
5 500
6 600
7 700
8 800
9 900
10 1000
11 1100
12 1200
Использование CASE ... (как предложено @ fa06) даст вам следующий результат - что, вероятно, не совсем то, что вы изначально хотели:
--
-- eg
-- {1} count orders that have an order_price greater than 500
-- {2} find the sum of all orders that have an order_price greater than 900
-- {3} find the average value of orders that have an order_price < 300
--
select
id
, count( case when order_price > 500 then id end ) count_
, sum( case when order_price > 900 then order_price end ) sum_
, avg( case when order_price < 300 then order_price end ) avg_
from orders
group by id
;
-- result
ID COUNT_ SUM_ AVG_
1 0 NULL 100
6 1 NULL NULL
11 1 1100 NULL
2 0 NULL 200
4 0 NULL NULL
5 0 NULL NULL
8 1 NULL NULL
3 0 NULL NULL
7 1 NULL NULL
9 1 NULL NULL
10 1 1000 NULL
12 1 1200 NULL
12 rows selected.
Вы можете использовать CASE во встроенном представлении и агрегировать его результирующий набор следующим образом:
select
count( gt500 )
, sum ( gt900 )
, avg ( lt300 )
from (
select
id
, case when order_price > 500 then 1 end gt500
, case when order_price > 900 then order_price end gt900
, case when order_price < 300 then order_price end lt300
from orders
)
;
-- result
COUNT(GT500) SUM(GT900) AVG(LT300)
7 3300 150