Гость
Целевая тема:
Создать новую тему:
Автор:
Форумы / Oracle [игнор отключен] [закрыт для гостей] / Группировка. Как. / 7 сообщений из 7, страница 1 из 1
11.01.2017, 12:37
    #39382198
на градусе
Гость
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Группировка. Как.
Код: plsql
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
create table account_rest as 
  select 1 as account_id,1 as currency_id,to_date('01.01.2010','dd.mm.yyyy') as dfrom,1 as rest from dual
  union all
  select 1 as account_id,1 as currency_id,to_date('05.01.2010','dd.mm.yyyy') as dfrom,5 as rest from dual
  union all
  select 1 as account_id,1 as currency_id,to_date('10.01.2010','dd.mm.yyyy') as dfrom,10 as rest from dual
  union all
  select 1 as account_id,1 as currency_id,to_date('15.01.2010','dd.mm.yyyy') as dfrom,15 as rest from dual
  union all
  select 1 as account_id,1 as currency_id,to_date('20.01.2010','dd.mm.yyyy') as dfrom,20 as rest from dual
  
  union all
  select 2 as account_id,1 as currency_id,to_date('04.01.2010','dd.mm.yyyy') as dfrom,7 as rest from dual
  union all
  select 2 as account_id,1 as currency_id,to_date('20.01.2010','dd.mm.yyyy') as dfrom,8 as rest from dual

  union all
  select 3 as account_id,2 as currency_id,to_date('07.01.2010','dd.mm.yyyy') as dfrom,9 as rest from dual
  union all
  select 3 as account_id,2 as currency_id,to_date('12.01.2010','dd.mm.yyyy') as dfrom,0 as rest from dual;          




alter table account_rest add constraint account_rest_pk primary key(account_id,dfrom);

select * from account_rest order by account_id,dfrom;



Как бы правильно сложить остатки (rest) по счетам в разрезе валют (currency_id) с учетом действия интервала каждой записи.

Результат должен быть таким:


Код: html
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
currency_id      dfrom         rest
======================================
1	         01-янв-2010	1
1	         04-янв-2010	8
1	         05-янв-2010	12
1	         10-янв-2010	17
1	         15-янв-2010	22
1	         20-янв-2010	28
2	         07-янв-2010	9
2	         12-янв-2010	0
...
Рейтинг: 0 / 0
11.01.2017, 12:48
    #39382215
over()
Гость
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Группировка. Как.
на градусе,

sum() - sum()
...
Рейтинг: 0 / 0
11.01.2017, 12:56
    #39382222
На градусе
Гость
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Группировка. Как.
over(),

Идею не понял даже приблизительно. Sum(чего) over(partition by чем order by как) - Sum(чего) over(partition by чем order by как) ?
...
Рейтинг: 0 / 0
11.01.2017, 15:27
    #39382371
ora601
Участник
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Группировка. Как.
на градусе,

Код: plsql
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
with account_rest as (
  select 1 as account_id,1 as currency_id,to_date('01.01.2010','dd.mm.yyyy') as dfrom,1 as rest 
  union all
  select 1 as account_id,1 as currency_id,to_date('05.01.2010','dd.mm.yyyy') as dfrom,5 as rest 
  union all
  select 1 as account_id,1 as currency_id,to_date('10.01.2010','dd.mm.yyyy') as dfrom,10 as rest 
  union all
  select 1 as account_id,1 as currency_id,to_date('15.01.2010','dd.mm.yyyy') as dfrom,15 as rest 
  union all
  select 1 as account_id,1 as currency_id,to_date('20.01.2010','dd.mm.yyyy') as dfrom,20 as rest 
  
  union all
  select 2 as account_id,1 as currency_id,to_date('04.01.2010','dd.mm.yyyy') as dfrom,7 as rest 
  union all
  select 2 as account_id,1 as currency_id,to_date('20.01.2010','dd.mm.yyyy') as dfrom,8 as rest

  union all
  select 3 as account_id,2 as currency_id,to_date('07.01.2010','dd.mm.yyyy') as dfrom,9 as rest 
  union all
  select 3 as account_id,2 as currency_id,to_date('12.01.2010','dd.mm.yyyy') as dfrom,0 as rest          
)
,
dist 
as (SELECT distinct currency_id , dfrom from account_rest)
select currency_id, dfrom, (
SELECT SUM(rest) FROM (
SELECT rest, row_number() over (partition by account_id order by dfrom desc) rn FROM account_rest ar WHERE a.currency_id=ar.currency_id AND ar.dfrom<=a.dfrom
) sq WHERE rn=1 ) sq from dist a
order by 1, 2
...
Рейтинг: 0 / 0
11.01.2017, 16:40
    #39382464
на градусе
Гость
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Группировка. Как.
ora601,

Спасибо, но малоэффективно (примерно так я уже делал). Материализация исходного множества которое в реале содержит десятки миллионов записей :(

Таки пытаюсь вникнуть в идею бегло и завуалировано поданую чуть выше ( разница сум). Пока никак.
...
Рейтинг: 0 / 0
11.01.2017, 17:26
    #39382526
ora601
Участник
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Группировка. Как.
на градусеora601,
Материализация исходного множества которое в реале содержит десятки миллионов записей :(


Это вообще то пример на твоих данных, а не материализация. Ну и в большинстве случаев там все равно придеться сканить всю таблицу, так что подход через sum() over () ну вряд ли будет быстрее, да и еще работать он вряд ли будет :)
...
Рейтинг: 0 / 0
12.01.2017, 12:27
    #39383026
на градусе
Гость
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Группировка. Как.
Получилось. Сделал так:

Код: plsql
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
  with Q as
  (
  select 1 as account_id,1 as currency_id,to_date('01.01.2010','dd.mm.yyyy') as dfrom,1 as rest from dual
  union all
  select 1 as account_id,1 as currency_id,to_date('05.01.2010','dd.mm.yyyy') as dfrom,5 as rest from dual
  union all
  select 1 as account_id,1 as currency_id,to_date('10.01.2010','dd.mm.yyyy') as dfrom,10 as rest from dual
  union all
  select 1 as account_id,1 as currency_id,to_date('15.01.2010','dd.mm.yyyy') as dfrom,15 as rest from dual
  union all
  select 1 as account_id,1 as currency_id,to_date('20.01.2010','dd.mm.yyyy') as dfrom,20 as rest from dual
  
  union all
  select 2 as account_id,1 as currency_id,to_date('04.01.2010','dd.mm.yyyy') as dfrom,7 as rest from dual
  union all
  select 2 as account_id,1 as currency_id,to_date('20.01.2010','dd.mm.yyyy') as dfrom,8 as rest from dual
  ),
       

Z as
(
select
Q.*,
rest-coalesce(lag(rest) over(partition by account_id order by dfrom),0) as turn
from Q
)

select
distinct 
currency_id,
dfrom,
sum(turn) over(partition by currency_id order by dfrom) as rest
from Z
order by 
currency_id,
dfrom
...
Рейтинг: 0 / 0
Форумы / Oracle [игнор отключен] [закрыт для гостей] / Группировка. Как. / 7 сообщений из 7, страница 1 из 1
Найденые пользователи ...
Разблокировать пользователей ...
Читали форум (0):
Пользователи онлайн (0):
x
x
Закрыть


Просмотр
0 / 0
Close
Debug Console [Select Text]