当多个条件满足时,如何分组计算平均值
when more than one condition satisfies, how to group them to calculate average
我使用的是 Oracle 数据库,想计算不同地区的平均产品价格。
示例:
prod,amt,price
X,100,1
X,180,2
X,250,3
Y,90,2
Y,170,3
Y,280,3
此处产品 X 在一个地区售价 100,在另一个地区售价 180..等等
现在,对于某些分析,这些被分组到重叠的不同范围内,我需要根据范围计算价格的平均值
要求的输出是
prod,rang(Amt),mean(price),
X,[0-200],1.5,
X,[150-300],2.5,
Y,[0-200],2.5,
Y,[150-300],3,
请注意,范围有很多,为了清楚起见,我只给出了 2 个。
我试过如下,但 case
匹配第一个条件,只为 [0-200] 给出一个 X 值,我需要 2 条记录
select prod, amt, price,
case
when amt between 0 and 200 then amt
when amt between 150 and 300 then amt
end as rng
from tablea
如何在 case 语句中获取匹配的 2 条记录?
使用单一条件进行两个查询并将它们合并。这不是一个完美的解决方案,但会奏效。
SELECT prod, '0-200' rang, AVG( price )
FROM table_name
WHERE amt BETWEEN 0 AND 200
GROUP BY prod
UNION ALL
SELECT prod, '150-300' rang, AVG( price )
FROM table_name
WHERE amt BETWEEN 150 AND 300
GROUP BY prod
您可以使用 conditional aggregation
然后 unpivot
作为
select prod_a as "Prod",
prod as "Range of Amounts",
value as "Mean Price"
from
(
with tablea(prod,amt,price) as
(
select 'X',100,1 from dual union all
select 'X',180,2 from dual union all
select 'X',250,3 from dual union all
select 'Y',90, 2 from dual union all
select 'Y',170,3 from dual union all
select 'Y',280,3 from dual
)
select prod as prod_a,
avg(case when amt between 0 and 200 then price end) as avg_0_200,
avg(case when amt between 150 and 300 then price end) as avg_150_300
from tablea a
group by prod
) b
unpivot(value for prod in(avg_0_200,avg_150_300))
order by prod_a;
Prod Range of Amounts Mean Price
---- ---------------- ----------
X AVG_0_200 1.5
X AVG_150_300 2.5
Y AVG_0_200 2.5
Y AVG_150_300 3
我使用的是 Oracle 数据库,想计算不同地区的平均产品价格。
示例:
prod,amt,price
X,100,1
X,180,2
X,250,3
Y,90,2
Y,170,3
Y,280,3
此处产品 X 在一个地区售价 100,在另一个地区售价 180..等等
现在,对于某些分析,这些被分组到重叠的不同范围内,我需要根据范围计算价格的平均值
要求的输出是
prod,rang(Amt),mean(price),
X,[0-200],1.5,
X,[150-300],2.5,
Y,[0-200],2.5,
Y,[150-300],3,
请注意,范围有很多,为了清楚起见,我只给出了 2 个。
我试过如下,但 case
匹配第一个条件,只为 [0-200] 给出一个 X 值,我需要 2 条记录
select prod, amt, price,
case
when amt between 0 and 200 then amt
when amt between 150 and 300 then amt
end as rng
from tablea
如何在 case 语句中获取匹配的 2 条记录?
使用单一条件进行两个查询并将它们合并。这不是一个完美的解决方案,但会奏效。
SELECT prod, '0-200' rang, AVG( price )
FROM table_name
WHERE amt BETWEEN 0 AND 200
GROUP BY prod
UNION ALL
SELECT prod, '150-300' rang, AVG( price )
FROM table_name
WHERE amt BETWEEN 150 AND 300
GROUP BY prod
您可以使用 conditional aggregation
然后 unpivot
作为
select prod_a as "Prod",
prod as "Range of Amounts",
value as "Mean Price"
from
(
with tablea(prod,amt,price) as
(
select 'X',100,1 from dual union all
select 'X',180,2 from dual union all
select 'X',250,3 from dual union all
select 'Y',90, 2 from dual union all
select 'Y',170,3 from dual union all
select 'Y',280,3 from dual
)
select prod as prod_a,
avg(case when amt between 0 and 200 then price end) as avg_0_200,
avg(case when amt between 150 and 300 then price end) as avg_150_300
from tablea a
group by prod
) b
unpivot(value for prod in(avg_0_200,avg_150_300))
order by prod_a;
Prod Range of Amounts Mean Price
---- ---------------- ----------
X AVG_0_200 1.5
X AVG_150_300 2.5
Y AVG_0_200 2.5
Y AVG_150_300 3