获得平均出勤率?

时间:2018-03-20 00:11:01

标签: subquery

我正在尝试开发子查询以获得两张桌子的平均出勤率以及完整的详细信息

2 个答案:

答案 0 :(得分:0)

select EVENTID ,
EVENTNAME ,
STARTDATETIME ,
ENDDATETIME ,
CONCERT_CONCERTID ,
VENUE_VENUEID,COUNT(TIMEOFARRIVAL),AVG(TIMEOFARRIVAL) 
FROM EVENT INNER JOIN BOOKING ON BOOKING.EVENT_EVENTID = EVENT.EVENTID;

有一个错误不一致的数据类型预期数字得到时间戳

答案 1 :(得分:0)

您的第一步是获取每个活动的与会者人数:

SELECT e.eventid, COUNT(b.customer_customerid) AS customer_cnt
  FROM event e LEFT JOIN booking b
    ON e.eventid = b.event_eventid
 GROUP BY e.eventid;

现在我使用上面的LEFT JOIN因为一个事件可能会出现零参与,这些也应该被计算在内!

我们可以使用窗口函数(Oracle称之为分析函数)获得平均值:

SELECT eventid, attendance, AVG(attendance) OVER ( ) AS avg_attendance
  FROM (
    SELECT e.eventid, COUNT(b.customer_customerid) AS attendance
      FROM event e LEFT JOIN booking b
        ON e.eventid = b.event_eventid
     GROUP BY e.eventid
);

现在我们可以获得低于平均水平的所有活动:

SELECT eventid, attendance FROM (
    SELECT eventid, attendance, AVG(attendance) OVER ( ) AS avg_attendance
      FROM (
        SELECT e.eventid, COUNT(b.customer_customerid) AS attendance
          FROM event e LEFT JOIN booking b
            ON e.eventid = b.event_eventid
         GROUP BY e.eventid
    )
) WHERE attendance < avg_attendance;

上面将为您提供eventid的列表 - 您可以通过使用此列表查询event表来获取所需的其余列。

根据评论进行编辑 - 海报希望确定每场音乐会的活动,该音乐会的参加人数低于平均水平。

SELECT concertid, eventid, attendance, avg_attendance FROM (
    SELECT concertid, eventid, attendance, AVG(attendance) OVER ( PARTITION BY concertid ) AS avg_attendance
      FROM (
        SELECT c.concertid, e.eventid, COUNT(b.timeofarrival) AS attendance
          FROM concert c INNER JOIN event e
            ON c.concertid = e.concert_concertid
          LEFT JOIN booking b
            ON e.eventid = b.event_eventid
         GROUP BY e.eventid
    )
) WHERE attendance < avg_attendance;