这是正确使用 'partition by' 的问题吗?
Is this a matter of using 'partition by' correctly?
我想为此使用分析,因为我想要其他几个类似的统计数据
报告。
问: 使用 Oracle Analytics,我怎样才能得到每个机场的一行?
with detail_records as
(select 1 as passenger_id, 'ATL' as airport, 'E4' as gate, 10 as luggage_weight from dual union all
select 2 as passenger_id, 'ATL' as airport, 'E4' as gate, 25 as luggage_weight from dual union all
select 2 as passenger_id, 'SFO' as airport, 'E4' as gate, 20 as luggage_weight from dual union all
select 3 as passenger_id, 'SFO' as airport, 'E4' as gate, 30 as luggage_weight from dual union all
select 4 as passenger_id, 'SFO' as airport, 'E4' as gate, 40 as luggage_weight from dual
)
select airport,
count(*) over (partition by airport) as airport_count,
sum(luggage_weight) over (partition by airport, gate) as a_g_weight
from detail_records
where gate='E4'
结果
AIRPORT AIRPORT_COUNT A_G_WEIGHT
ATL 2 35
ATL 2 35
SFO 3 90
SFO 3 90
SFO 3 90
想要的结果
Airport Airport_count A_G_WEIGHT
ATL 2 35
SFO 3 90
正如 Wernfried 所说,您正在寻找 DISTINCT
关键字。
由于 "passenger ID" 列无关紧要,并且要避免重复,因此必须这样称呼它:
with detail_records as
(select 1 as passenger_id, 'ATL' as airport, 'E4' as gate, 10 as luggage_weight from dual union all
select 2 as passenger_id, 'ATL' as airport, 'E4' as gate, 25 as luggage_weight from dual union all
select 2 as passenger_id, 'SFO' as airport, 'E4' as gate, 20 as luggage_weight from dual union all
select 3 as passenger_id, 'SFO' as airport, 'E4' as gate, 30 as luggage_weight from dual union all
select 4 as passenger_id, 'SFO' as airport, 'E4' as gate, 40 as luggage_weight from dual
)
select distinct airport,
count(*) over (partition by airport) as airport_count,
sum(luggage_weight) over (partition by airport, gate) as a_g_weight
from detail_records
where gate='E4'
使用简单的 GROUP BY 查询
select airport,
count(*) as airport_count,
sum(luggage_weight) as a_g_weight
from detail_records
where gate='E4'
group by airport
我想为此使用分析,因为我想要其他几个类似的统计数据 报告。
问: 使用 Oracle Analytics,我怎样才能得到每个机场的一行?
with detail_records as
(select 1 as passenger_id, 'ATL' as airport, 'E4' as gate, 10 as luggage_weight from dual union all
select 2 as passenger_id, 'ATL' as airport, 'E4' as gate, 25 as luggage_weight from dual union all
select 2 as passenger_id, 'SFO' as airport, 'E4' as gate, 20 as luggage_weight from dual union all
select 3 as passenger_id, 'SFO' as airport, 'E4' as gate, 30 as luggage_weight from dual union all
select 4 as passenger_id, 'SFO' as airport, 'E4' as gate, 40 as luggage_weight from dual
)
select airport,
count(*) over (partition by airport) as airport_count,
sum(luggage_weight) over (partition by airport, gate) as a_g_weight
from detail_records
where gate='E4'
结果
AIRPORT AIRPORT_COUNT A_G_WEIGHT
ATL 2 35
ATL 2 35
SFO 3 90
SFO 3 90
SFO 3 90
想要的结果
Airport Airport_count A_G_WEIGHT
ATL 2 35
SFO 3 90
正如 Wernfried 所说,您正在寻找 DISTINCT
关键字。
由于 "passenger ID" 列无关紧要,并且要避免重复,因此必须这样称呼它:
with detail_records as
(select 1 as passenger_id, 'ATL' as airport, 'E4' as gate, 10 as luggage_weight from dual union all
select 2 as passenger_id, 'ATL' as airport, 'E4' as gate, 25 as luggage_weight from dual union all
select 2 as passenger_id, 'SFO' as airport, 'E4' as gate, 20 as luggage_weight from dual union all
select 3 as passenger_id, 'SFO' as airport, 'E4' as gate, 30 as luggage_weight from dual union all
select 4 as passenger_id, 'SFO' as airport, 'E4' as gate, 40 as luggage_weight from dual
)
select distinct airport,
count(*) over (partition by airport) as airport_count,
sum(luggage_weight) over (partition by airport, gate) as a_g_weight
from detail_records
where gate='E4'
使用简单的 GROUP BY 查询
select airport,
count(*) as airport_count,
sum(luggage_weight) as a_g_weight
from detail_records
where gate='E4'
group by airport