2016-08-24 1 views
0

Я хочу использовать аналитику для этого, потому что у меня есть несколько других статистик сопоставлений, которые я бы хотел сообщить .Это вопрос правильного использования «раздела»?

Вопрос: Использование 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 
+1

Попробуйте 'выбрать DISTINCT airport, ... ' –

+1

Если вы фильтруете одним затвором, то разделение по этому столбцу ничего не добавляет; если вы фильтруете по нескольким воротам (или всем), то вы будете иметь несколько значений 'a_g_weight', чтобы ничего не объяснить, почему. –

ответ

1

Как сказал Wernfried, ваш ищут DISTINCT ключевое слово.

Поскольку столбец "пассажир 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' 
0

Используйте простой GROUP BY запроса

select airport, 
     count(*) as airport_count, 
     sum(luggage_weight) as a_g_weight 
from detail_records 
where gate='E4' 
group by airport 

 Смежные вопросы

  • Нет связанных вопросов^_^