【问题标题】:Oracle - How to use avg(count(*)) on having clause or where clause?Oracle - 如何在有子句或 where 子句上使用 avg(count(*))?
【发布时间】:2018-06-10 13:36:48
【问题描述】:

我有这样的数据:

CONCERT_ID   EVENT_ID ATTENDANCE AVG_ATTENDANCE_EACH_CONCERT
---------- ---------- ---------- ---------------------------
         1          1          1                         1,5
         1          2          2                         1,5
         3          5          2                           2
         3          6          2                           2
         5         11          4                           2
         5         12          1                           2
         5         13          1                           2

来自这个查询:

select concert_id, event_id, count(customer_id) attendance,
avg(count(*)) over (partition by concert_id) avg_attendance_each_concert
from booking
group by concert_id, event_id
order by event_id;

如何对该查询进行限制。我想做的是

如果出席率低于平均出席率显示结果

我已经尝试过 avg(count(*)) over (partition by Concert_id) to have 子句,但给了我一个错误 group function is too deep

【问题讨论】:

    标签: sql oracle oracle11g aggregate-functions


    【解决方案1】:

    只需应用一个嵌套即可轻松获得所需的结果:

    select * from
    (
     select concert_id, event_id, count(customer_id) attendance, 
            avg(count(*)) over (partition by concert_id) avg_attendance_each_concert
       from booking
      group by concert_id, event_id
      order by event_id
    )
    where attendance < avg_attendance_each_concert
    

    D e M o

    【讨论】:

      【解决方案2】:

      包括一个“中间”表,即在您的上一个问题中返回正确结果的查询。然后从中选择满足新条件的值。

      SQL> with booking (concert_id, event_id, customer_id) as
        2  (select 1, 1, 10 from dual union
        3   select 1, 2, 10 from dual union
        4   select 1, 2, 20 from dual union
        5   --
        6   select 3, 5, 10 from dual union
        7   select 3, 5, 20 from dual union
        8   select 3, 6, 30 from dual union
        9   select 3, 6, 40 from dual union
       10   --
       11   select 5, 11, 10 from dual union
       12   select 5, 11, 20 from dual union
       13   select 5, 11, 30 from dual union
       14   select 5, 11, 40 from dual union
       15   select 5, 12, 50 from dual union
       16   select 5, 13, 60 from dual
       17  ),
       18  inter as
       19  (select concert_id, event_id, count(customer_id) attendance,
       20          avg(count(*)) over (partition by concert_id) avg_attendance_each_concert
       21   from booking
       22   group by concert_id, event_id
       23  )
       24  select concert_id, event_id, attendance, avg_attendance_each_concert
       25  from inter
       26  where attendance < avg_attendance_Each_concert
       27  order by event_id;
      
      CONCERT_ID   EVENT_ID ATTENDANCE AVG_ATTENDANCE_EACH_CONCERT
      ---------- ---------- ---------- ---------------------------
               1          1          1                         1,5
               5         12          1                           2
               5         13          1                           2
      
      SQL>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-09-23
        • 2012-07-03
        • 1970-01-01
        • 1970-01-01
        • 2021-08-16
        • 2023-03-31
        • 2019-12-23
        相关资源
        最近更新 更多