2015-07-29 3 views
0

Я хочу получить итоговые цифры из таблицы cust_detail, если в invoice_detail отображается конкретное invoice_code.Как получить правильные резюме с аналитикой?

В этом примере я хотел бы сообщить об итогах cust_detail только для партий 10 и 20, так как они являются invoice_code='9999'. Но дублирование в таблице invoice_detail искажает мои номера.

with 
    invoice_detail as 
    (
    select '10' as invoice_batch, '9999' as invoice_code from dual union all 
    select '10' as invoice_batch, '9999' as invoice_code from dual union all 
    select '20' as invoice_batch, '1111' as invoice_code from dual union all 
    select '30' as invoice_batch, '9999' as invoice_code from dual 
), 
    cust_detail as 
    (
    select '1' as cust_id, '10' as invoice_batch, 40 as points_paid, 30 as points_earned, 30 as points_delivered from dual union all 
    select '1' as cust_id, '20' as invoice_batch, 10 as points_paid, 10 as points_earned, 10 as points_delivered from dual union all 
    select '1' as cust_id, '30' as invoice_batch, 20 as points_paid, 15 as points_earned, 5 as points_delivered from dual 
) 
select cust_id, 
     sum(points_paid) over (partition by c.invoice_batch 
            order by cust_id) batch_total 
    from cust_detail c 
inner join invoice_detail i on c.invoice_batch=i.invoice_batch 
where i.invoice_code = '9999';        

Желаемые результаты:

CUST_ID PAID EARNED DELIVERED TOT_PAID TOT_EARNED TOT_DELIVERED 
--------- ------ -------- ----------- ---------- ------------ --------------- 
1   40  30  30   60   45   40  
1   20  15  5   60   45   40 
+0

Вы пропускаете 'группы by' в запросе. –

+0

Когда я добавляю группу c.invoice_batch в конце запроса, я получаю сообщение выражения «не группа по». – zundarz

+1

вы также должны «group by cust_id» –

ответ

1

Вы можете удалить дупликации из invoice_detail с отчетливым до присоединения:

with invoice_detail as 
(
select '10' as invoice_batch, '9999' as invoice_code from dual union all 
select '10' as invoice_batch, '9999' as invoice_code from dual union all 
select '20' as invoice_batch, '1111' as invoice_code from dual union all 
select '30' as invoice_batch, '9999' as invoice_code from dual 
), 
cust_detail as 
(
select '1' as cust_id, '10' as invoice_batch, 40 as points_paid, 30 as points_earned, 30 as points_delivered from dual union all 
select '1' as cust_id, '20' as invoice_batch, 10 as points_paid, 10 as points_earned, 10 as points_delivered from dual union all 
select '1' as cust_id, '30' as invoice_batch, 20 as points_paid, 15 as points_earned, 5 as points_delivered from dual 
) 
select cust_id 
      ,points_paid 
      ,points_earned 
      ,points_delivered 
      ,sum(points_paid) over (partition by c.cust_id) as tot_paid 
      ,sum(points_earned) over (partition by c.cust_id) as tot_earned 
      ,sum(points_delivered) over (partition by c.cust_id) as tot_delivered   
from cust_detail c 
join (select distinct * from invoice_detail) i 
    on c.invoice_batch=i.invoice_batch 
where i.invoice_code = '9999'; 

Обратите внимание, что резюме включают партии и , потому что партия 20 с invoice_code = '1111'.

SQL Fiddle

0

Я не уверен, что ваши желаемые результаты должны делать с вашим запросом. Но, я бы ожидать, что ваш запрос, чтобы выглядеть следующим образом:

select cust_id, 
     sum(points_paid) over (partition by cust_id) as batch_total 
from cust_detail c inner join 
    invoice_detail i 
    on c.invoice_batch=i.invoice_batch 
where i.invoice_code = '9999' ; 
Смежные вопросы