【问题标题】:How to get correct summaries with analytics?如何通过分析获得正确的摘要?
【发布时间】:2015-07-29 22:21:28
【问题描述】:

如果特定的invoice_code 出现在invoice_detail 中,我想从cust_detail 表中获取摘要编号。

在此示例中,我只想报告批次 1020 的 cust_detail 摘要,因为它们是带有 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

【问题讨论】:

  • 您在查询中缺少group by
  • 当我在查询末尾添加 group by c.invoice_batch 时,我收到 not a group by 表达式消息。
  • 你也应该group by cust_id
  • 我在这里使用分析函数。按invoice_batchcust_id 分组产生ORA-00979: Not a group by expression 消息。我认为group by 不适用于sum() 等分析函数

标签: sql oracle sum window-functions


【解决方案1】:

您可以在加入前使用 distinct 从 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';

请注意,摘要包括批次 1030,因为批次 20 的 invoice_code='1111'。

SQL Fiddle

【讨论】:

    【解决方案2】:

    我不确定您想要的结果与您的查询有什么关系。但是,我希望您的查询看起来更像这样:

    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' ;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多